How to Create Short URL using PHP

There are different methods to shorten the URL,very basic method is to Http redirect to a domain that contains shortened URL. In this tutorial we will show you how to create short URL using PHP. You may also like How to PHP Validate URL and How to Upload Image From URL Using PHP.


    <form method="post" action="save_url.php">
      <input type="text" name="url_value" placeholder="Enter URL">
      <input type="submit" name="short_url">
    <form method="post" action="">
        <input type="text" name="short_url_value" placeholder="Enter Short URL">
        <input type="submit" name="original_url">

PHP Code

    define('DB_SERVER', "localhost");
    define('DB_USER', "root");
    define('DB_PASS', "");
    define('DB_DATABASE', "ieltsmedidb");
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
    mysqli_query($con,"INSERT INTO urls (url, short_url) VALUES ('$url','$short_url')");
    echo "Your New URL Is :".$short_url."";


    $select = mysqli_query($con,"SELECT long_url FROM urls WHERE short_url = '$short_url'");

        echo $row['long_url'];	  

Leave a Reply

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