Bladephp: Now with 7+ codeigniter, php, and laravel project 7+ php project From $18/Year From Rs. 1000/Year

How to Create Short URL using PHP

by
Category:php
php mysql html
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.

HTML Code

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

PHP Code

<?php
    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);
    
if(isset($_POST['short_url']))
{
    $url=$_POST["url_value"];
    $short_url=substr(md5($url.mt_rand()),0,8);
    mysqli_query($con,"INSERT INTO urls (url, short_url) VALUES ('$url','$short_url')");
    echo "Your New URL Is : http://abc.com/url.php?u=".$short_url."";
}

if(isset($_POST['original_url']))
{
    $url=$_POST["short_url_value"];
    $short_url=substr($url,25);

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

    while($row=mysqli_fetch_assoc($select))
    {
        echo $row['long_url'];	  
    }
}
?>

0 Comments


Html code work in comment box


Related Post