Page 1 of 1

Make your own URL shortening website!

Posted: Sun Nov 27, 2005 11:11 am
by freak127
I wrote this script the other day for someone who requested it. If you put it on your website, then you can create your own URL shortening website, like my site, URLmini. I use a different script on my site though, one with MySQL ;) . This script only requires PHP support as it uses PHP files to do the redirection, not MySQL.
Anyway, copy this code into a page, and save it with a .php extension:

Code: Select all

<?

$r = $_GET["r"];
if($r != "") { header("Location: http://www.yoursite.com/sites/$r.php"); }

if(isset($_POST['tinyurl']))
{
   function gen_chars_no_dup($long=7)
   {
      $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
       mt_srand((double)microtime()*1000000);
       $i = 0;
       while ($i != $long) { $rand = mt_rand() % strlen($chars); $tmp = $chars[$rand]; $pass = $pass.$tmp; $chars = str_replace($tmp, "", $chars); $i++; }
       return strrev($pass);
   }
   $name = gen_chars_no_dup(6);
   $filename = "sites/$name.php";
   $text = "<? header('Location: $url'); ?>";
   $file = fopen($filename, "w");
   $chmod = chmod($filename, 0755);
   $write = fwrite($file, $text);
   fclose($file);
   echo "Tiny URL:

   [url=http://www.yoursite.com/?r=$name]http://www.yoursite.com/?r=$name[/url]

";
}
?>

<form method="POST" action="<? echo $_SERVER['REQUEST_URI']; ?>">
URL:

<input type="text" value="http://" name="url" size="25">
<input type="submit" value="Tiny URL" name="tinyurl">
</form>
However, to make it work properly, you must create a new folder in the same place as this script called 'sites' and CHMOD it to 0777. Also, remember to change 'yoursite.com' to your actual website ;) .
If you have any questions or anything then just let me know :) .