Scripts

[PHP MYSQL] Dalando's SHORT URL script!

Al mijn kennis is er in gestoken, met dit als resultaat: Dalando's SHORT URL script! tadaa... +) SQL code zit in de file! Je moet zelf 6 variables zetten! achtergrond info: Ik heb het voornamelijk gemaakt om te oefenen... Met succes! :P Voorbeeld: http://tinyurldalando.eu5.org/ Of natuurlijk: http://tinyurldalando.eu5.org/voorbeeld/?u=voorbeeld :P Komt nog: Kijken of short al bestaat

shorturl.php
[code]
<?php

/* Sql code:

CREATE TABLE `shorter` (
  `langeurl` text NOT NULL,
  `korteurl` varchar(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

*/


// Set these variables
$fullurl = ''; // Script location + filename with http:// and / at the end url.
$webm_email = ''; // Email adress webmaster for errors...

$dbhost = "localhost"; 
$dbuser = "xxxx"; 
$dbpass = "xxxx"; 
$dbname = "xxxx"; 


//////////////////////////////////////// DONT CHANGE ANYTHING BELOW //////////////////////////////////////// 


$db = mysql_connect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db);

if(isset($_GET['u'])){
	$query = "SELECT * FROM shorter WHERE korteurl = '".mysql_real_escape_string($_GET['u']) . "'";
	if($sql = mysql_query($query))
	{	
		$row = mysql_fetch_assoc($sql);

		if($row) 
		{
    			header('Location: '.$row['langeurl']);
    			exit;
		}
		else 
		{
    			echo 'URL not found!';
		}
	}
	else
	{
		echo 'Er is een fout in de query.<br />';
        	mail($webm_email,'website error',mysql_error()); 
	}


}
else
{ 

?>

<html>
<head>
<title>Url shortner by Dalando</title>
</head>
<body>
<form action="" method="post">
Long url:<br>
<input type="text" name="long" /><br/> 
Short:<br>
<input type="text" name="short" maxlength="10" /><br/>
<input type="hidden" name="gen" value="<?php echo rand(0, 9999999999); ?>" />
<input type="submit" value="Short!" />
</form>
<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){
	if(empty($_POST['long'])){
		echo 'Long url was not set!';
	}
	else
	{
		if(empty($_POST['short'])){
			$short = $_POST['gen'];
		}
		else
		{	
			$short = $_POST['short'];
		}
	}
	$long = $_POST['long'];
	
	if(stristr($long, 'http://') === FALSE) {
    		echo 'URL not complete! You must set "http://" !';
  	}
	else
	{

		$qry = "INSERT INTO shorter (langeurl, korteurl) VALUES ('".mysql_real_escape_string($long)."', '".mysql_real_escape_string($short)."' )";
           
		if($sql = mysql_query($qry))
		{
			$created_fullurl = $fullurl.'?u='.$short;
		
			echo 'Created! url: <br/>';
			echo '<a href="'.htmlentities($created_fullurl).'">'.htmlentities($created_fullurl).'</a>';
		}
		else
		{
			echo 'Er is een fout in de query.<br />';
			mail($webm_email,'website error',mysql_error());    
		}
	}
}
?>
</body>
</html>
<?php
}
mysql_close($db);
?>
[/code]
	

Reacties

0
Nog geen reacties.