1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
<HTML>
<HEAD>
<TITLE>
<?
$server = $_SERVER['SERVER_NAME'];
echo "$server ";
?>
NanoURL - Create a shorter link</TITLE>
</HEAD>
<BODY>
<h1> Create a NanoURL </h1>
<p>
<?php
include('nanourl.conf');
$newurl = $_POST['url'];
if($newurl)
{
if(! ($connection = @ mysql_connect($nanourlHost, $nanourlUser, $nanourlPassword)) ) {
echo mysql_error();
die("could not connect");
}
if(! (mysql_select_db($nanourlDB, $connection))) {
die("cannot select DB");
}
srand( ((int)((double)microtime()*1000003)) ); //seeding...
do {
$hash = '';
for($i = 0; $i< 5; $i++) {
$random = (rand()%36);
if($random <26) {
$random = $random + 97; //map to lowercase ASCII
} else {
$random = $random + 22; //map to number ASCII
}
$hash = $hash . chr($random);
}
$result = mysql_query("INSERT INTO urls VALUES ('$hash', '$newurl')");
//on the off chance this didn't work (collisions of hashes
//most likely...) do it all again
} while(! ($result));
//build the forwarding URL
$fwdurl = "$server/nanourl/".$hash;
//echo out the create signature
echo "<h1>NanoURL was created!</h1><BR>";
//echo out the stuff where we draw the hash out of
echo "<input type=hidden name=tinyurl value=\"" . $fwdurl . "\"><BR>";
//provide a link to click to.
echo "<a href=\"http://" . $fwdurl . "\">link to " . $fwdurl . "</a>";
}
?>
</p>
<BR><i>Create a url</i><BR>
<form action="create.php" method="post">
URL: <input type="text" size=50 name=url><BR>
<input type="submit" value="Create url">
</form>
</BODY>
</HTML>
|