URL Shortener Script

Create The Table

First you need to create a table where the links would be saved. To do so, you need to copy the following code and paste on the Query in your MyPhpAdmin.

CREATE TABLE `short` (
  `id` int(11) NOT NULL,
  `code` varchar(255) NOT NULL,
  `url` text NOT NULL,
  `created` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `short`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `short`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

Second: Create a new folder named “d”

Third: Create the file config.php

Create a new file named “config.php“. Then, copy and paste the following code inside that file.

<?php

//Connect

$DB = array(
    "server"=>"localhost",
    "dbname"=>"demo",
    "dbuser"=>"root",
    "dbpass"=>"",
);

$DBConnect = mysql_connect($DB['server'], $DB['dbuser'], $DB['dbpass']);
$DBSelect = mysql_select_db($DB['dbname']);

// Time Zone
date_default_timezone_set("America/New_York");

?>

Fourth: Create the file index.php

Create a new file and name it “index.php“. Then, copy and paste the following code inside that file.

<?php include("config.php"); ?>
<!doctype html>
<html lang="en">
<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">

  <title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<div class="container">
  <form action="create.php" method="post">
  <div class="form-row">
    <div class="col">
      <input type="url" name="content" class="form-control" placeholder="http://example.com/something">
    </div>
  </div>
  <div class="form-row">
    <div class="col">
      <button type="submit" name="save" class="btn btn-success">Save</button>
    </div>
  </div>
  </form>

<?php
if(isset($_POST['save'])){
  // Save text to file
  $rand = rand();
  $ext = '.html';
  $filename = $rand.$ext;
  $content = $_POST['content'];
  $date = date("m-d-Y");
  $inputString = "<meta http-equiv='refresh' content='.5; URL=../url.php?id=".$rand."'>";
  // On Windows platformu, use \n\r instead of \n

  $fp = fopen( "data/".$filename , "w" );
  if(mysql_query("INSERT INTO `short` (`id`, `code`, `url`, `created`) VALUES (NULL, '".$rand."', '".$content."', '".$date."');")){
    echo "<a href='data/".$filename."' target='_blank'>Short Link</a>";
    fwrite($fp, $inputString);
    fclose($fp);
  }

}
?>
</div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
  </body>
</html>

Lastly: Create the file url.php

Create a new file named “url.php“. Then, copy and paste the following code inside that file.

<?php include("config.php"); ?>
<?php
$id = $_GET['id'];
$selectData = mysql_query("SELECT * FROM short WHERE code='".$id."'");
$FetchData=mysql_fetch_assoc($selectData);
$url = $FetchData['url'];
header('Location: '.$url);
?>

Now, we are done with the coding, and I hope you enjoy it. See in another coding session.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart