Url shorten script problem

hello,

I got a script, a shorten link script and right now it works perfectly.

Onlything that i want to change is the input of the links, right now i can only imput 1 link at a time. I would like to do it whit a text box and the user then can imput all his links seperated by a new line. The script reads all of them, check if they are in the database and then let them display in a text box (so people can easly copy and past) and the max links would be 20 or 25 or something.

This is the script i got now for shorten one link (index.php):

[code]<?php

require_once(“config.php”);
require_once(“db.php”);
require_once(“templates.inc.php”);

//-------------------------------------------------------------------
// MAIN SCRIPT
//-------------------------------------------------------------------

if ($_POST[url]) $url = urldecode($_POST[url]); else $url = urldecode($_GET[url]);

// Check how the user reached this page

if ($_GET[‘r’]) // It is a redirect
{
// Get the ID and decode

$compos = explode("/", $_GET['r']);
$id = $compos[0];
$id = D1_base36_decode($id);

// Get the URL for ID //

$sql = "SELECT `url` FROM $table_urls WHERE `id` = $id LIMIT 0, 1";
$results = mysql_query($sql, $conn);

if ($rowfields = @mysql_fetch_array($results))		// URL Found
{
	// Update hit count

	$sql = "UPDATE `$table_urls` SET `hits` = `hits`+1 WHERE `id` = $id";
	mysql_query($sql, $conn);

	// Get the base URL		

	$url = $rowfields[url];

	// Generate the complete URL

	$compocnt = count($compos);

	for ($i=1; $i<$compocnt; $i++)
	{
		$url .= "\" . $compos[$i];
	}


	// Load the URL
	$tpl = D1_processTemplate("redirect");
	echo $tpl;

}

else		// URL Not Found
{
	$tpl = D1_processTemplate("notfound");
	echo $tpl;
}

}

elseif ($url) // Create URL
{
// Make the URL //

$sql = "SELECT `id` FROM `$table_urls` WHERE `url` = '$url' LIMIT 0, 1";
$results = mysql_query($sql, $conn);

if ($rowfields = @mysql_fetch_array($results)) // URL already present
{
	// Get the ID
	$id = D1_base36_encode($rowfields[id]);

	$tpl = D1_processTemplate("done");
	echo $tpl;

}

else		// ID not present
{

	// Create
	$sql = "INSERT INTO `$table_urls` (`url`) VALUES ('$url')";
	mysql_query($sql, $conn) or die(mysql_error());

	// Load ID of new entry
	$sql = "SELECT `id` FROM `$table_urls` WHERE `url` = '$url' LIMIT 0, 1";
	$results = mysql_query($sql, $conn);
	
	if ($rowfields = @mysql_fetch_array($results))
	{
		$id = D1_base36_encode($rowfields[id]);

		$tpl = D1_processTemplate("done");
		echo $tpl;

	}
	
	else
	{
		$tpl = D1_processTemplate("error");
		echo $tpl;
	}
}

}

else // Show welcome
{
$tpl = D1_processTemplate(“welcome”);
echo $tpl;
}

//-------------------------------------------------------------------
// FUNCTIONS
//-------------------------------------------------------------------

// Function: D1_base36_encode()
// Encodes a given number in a base 36 (0-9,a-z) format

function D1_base36_encode($num)
{
if ($num<10)
return strval($num);
elseif ($num<36)
return chr(ord(‘a’) + $num - 10);
else
return (D1_base36_encode((int)($num / 36)) . D1_base36_encode($num % 36));
}

// Function: D1_base36_decode()
// Decodes a base 36 number into decimal

function D1_base36_decode($str)
{

// Make all lowercase
$str= strtolower($str);

$num = 0;				// Result
$len = strlen($str);
$pwr = $len - 1;
$pos = 0;

while ($pos < $len)
{
	
	// Get digit and get equivalent number
	$digit = $str{$pos};
	$ascdigit = ord(strval($digit));
	$val = ($ascdigit >= ord("a")) ? 
		($ascdigit - ord("a") + 10) :
		($ascdigit - ord("0"));

	// Add to partial sum
	$num += $val * pow(36, $pwr);

	// Move to next digit
	$pos++;
	$pwr--;
}

// Done
return $num;

}
?>[/code]

Here is the imput from the user (header.tpl):

[code]

[/code]

And then the template function thingy:

[code]<?php

//===================================================================
// URL REDIRECTION SCRIPT Version 1.0
//===================================================================
// File: templates.inc.php
// Functions to process the templates
//-------------------------------------------------------------------

function D1_processTemplate($tplname)
{
global $site_root, $site_name, $url, $id;

$actual_url_link = "<a href="$url">".D1_splitlines($url)."</a>";
$short_url = "$site_root/?r=$id";
$short_url_link = "<a href="$short_url">$short_url</a>";

$tpl = implode("", file("$site_root/templates/$tplname.tpl"));
if ($tplname!="header" && $tplname!="footer" && $tplname!="redirect")
	$tpl = implode("", file("$site_root/templates/header.tpl")) . $tpl . implode("", file("$site_root/templates/footer.tpl"));
$tpl = str_replace("{@SITE_NAME}", $site_name, $tpl);
$tpl = str_replace("{@ACTUAL_URL}", $url, $tpl);
$tpl = str_replace("{@ACTUAL_URL_LINK}", $actual_url_link, $tpl);
$tpl = str_replace("{@SHORT_URL}", $short_url, $tpl);
$tpl = str_replace("{@SHORT_URL_LINK}", $short_url_link, $tpl);
$tpl = str_replace("{@TOOLBAR_LINK}", "<a href="javascript: document.URL='$site_root/?url='+escape(document.URL);">$site_name</a>", $tpl);
return $tpl;

}

// Function: D1_splitlines
// To format long urls
function D1_splitlines($str, $lim=50)
{
$len = strlen($str);
$res = “”;

for ($i=0; $i<$len; $i=$i+$lim)
{
	$res .= substr($str, $i, $lim);
	$res .= "<br>n";
}

return $res;

}
?>[/code]

And the display file (the file that display’s the shorten links):

[code]

The short URL

{@SHORT_URL_LINK} [/code]

If you need more info then please say so.
~Shub

coul’d it be that u are overdoing it? a script to process some links should be maximum of 10 lines!

what exactly do u mean with “shorten” a link? plz give us some examples.

what do u need this complex template structure for?

Sponsor our Newsletter | Privacy Policy | Terms of Service