Skip to content

Instantly share code, notes, and snippets.

/Index.php
Created Feb 15, 2017

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<title>Url Shortner - By HackerRahul.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<body>
<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">
</form>
</div>
<?php
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;
}else{
$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>
<br>
<a href='http://". $_SERVER['SERVER_NAME']."/urlshortner/".$title."'>
http://". $_SERVER['SERVER_NAME']."/urlshortner/".$title."
</a>
</div>
";
}
?>
</body>
</html>
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.