shorturl.php
Code (php)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?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);
?>
/* 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);
?>