Skip to content

Instantly share code, notes, and snippets.


Created Feb 15, 2017
What would you like to do?
<!DOCTYPE html>
<title>Url Shortner - By</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<div class="w3-content w3-center w3-display-topmiddle w3-margin-top">
<h1 class="w3-bottombar w3-center">Enter Url</h1>
<form method="post" action="">
<input type="text" name="url" class="w3-input w3-border" style="width:300px;" placeholder="Enter Your Url" required="required">
<input type="submit" name="submit" value="Submit" class="w3-margin-top w3-btn w3-cyan w3-round w3-ripple">
include 'db.php';
if (isset($_POST['submit'])) {
function generateRandomString($length = 7) {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-=_';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
return $randomString;
$url1 = $_POST['url'];
$title = generateRandomString();
if (substr($_POST['url'],0,7) !="http://" && substr($_POST['url'],0,8) !="https://"){
$url = "http://".$url1;
$url = $url1;
$query = "INSERT INTO url(url,title) VALUES('$url' , '$title')";
$run = mysql_query($query);
echo "<div class='w3-content w3-display-middle w3-margin-top'>
<h1>Here is your short Url</h1>
<a href='http://". $_SERVER['SERVER_NAME']."/urlshortner/".$title."'>
http://". $_SERVER['SERVER_NAME']."/urlshortner/".$title."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.