forked from yusha/Simple-PHP-URL-Shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
213 lines (172 loc) · 4.36 KB
/
Copy pathindex.php
File metadata and controls
213 lines (172 loc) · 4.36 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<!-- Simple PHP URL Shortner by Yusha Ibn Yakub
Website: https://yusha.me
-->
<head>
<title>Simple PHP URL Shortner</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style>
.tooltip {
position: relative;
display: inline-block;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 140px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -75px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
</head>
<body>
<script>
function myFunction() {
var copyText = document.getElementById("myInput");
copyText.select();
document.execCommand("copy");
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copied: " + copyText.value;
}
function outFunc() {
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copy to clipboard";
}
</script>
<?php
$servername = 'localhost';
$username = '';
$password = ''; // on localhost by default there is no password
$dbname = '';
$base_url=''; // it is your application url
if (isset($_GET['url']) && $_GET['url']!="")
{
$url=urldecode($_GET['url']);
if (filter_var($url, FILTER_VALIDATE_URL))
{
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$slug=GetShortUrl($url);
$conn->close();
//echo $base_url.$slug;
?>
<center>
<h1>Paste Your Url Here</h1>
<form>
<p><input style="width: 500px; height: 22px;" type="url" name="url" required /></p>
<p><input class="button" type="submit" /></p>
</form><br/>
<?php
echo 'Here is the short <a href="'; echo $base_url; echo"/"; echo $slug;
echo '" target="_blank">'; echo 'link</a>: ';
?><input type="text" value="<?php echo $base_url; echo"/"; echo $slug; ?>" id="myInput">
<div class="tooltip"><button onclick="myFunction()" onmouseout="outFunc()"><span class="tooltiptext" id="myTooltip" >Copy to clipboard</span>Copy URL</button></div></center>
<?php
}
else
{
die("$url is not a valid URL");
}
}
else
{
?>
<center>
<h1>Paste Your Url Here</h1>
<form>
<p><input style="width: 500px; height: 22px;" type="url" name="url" required /></p>
<p><input class="button" type="submit" /></p>
</form>
</center>
<?php
}
function GetShortUrl($url){
global $conn;
$query = "SELECT * FROM url_shorten WHERE url = '".$url."' ";
$result = $conn->query($query);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row['short_code'];
} else {
$short_code = generateUniqueID();
$sql = "INSERT INTO url_shorten (url, short_code, hits)
VALUES ('".$url."', '".$short_code."', '0')";
if ($conn->query($sql) === TRUE) {
return $short_code;
} else {
die("Unknown Error Occured");
}
}
}
function generateUniqueID(){
global $conn;
$token = substr(md5(uniqid(rand(), true)),0,3); // creates a 3 digit unique short id. You can maximize it but remember to change .htacess value as well
$query = "SELECT * FROM url_shorten WHERE short_code = '".$token."' ";
$result = $conn->query($query);
if ($result->num_rows > 0) {
generateUniqueID();
} else {
return $token;
}
}
if(isset($_GET['redirect']) && $_GET['redirect']!="")
{
$slug=urldecode($_GET['redirect']);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$url= GetRedirectUrl($slug);
$conn->close();
header("location:".$url);
exit;
}
function GetRedirectUrl($slug){
global $conn;
$query = "SELECT * FROM url_shorten WHERE short_code = '".addslashes($slug)."' ";
$result = $conn->query($query);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
// increase the hit
$hits=$row['hits']+1;
$sql = "update url_shorten set hits='".$hits."' where id='".$row['id']."' ";
$conn->query($sql);
return $row['url'];
}
else
{
die("Invalid Link!");
}
}
?>
</body>