Make your own URL shortener in PHP Part -3
This is third part
of our 3 tutorial series of URL shortener. In this tutorial I am going to show
you the last file needed to complete our project.
If you have missed
our first two tutorials than you can visit theme here.
- Part – I: CreateDb and get Long URL from user, insert that URL in Db and generate short URL.
- Part – II: Captureshort URL on server using .htaccess code lines and redirect it to the handlingscript, i.e redirect.php
Now let’s continue
with our Part –III.
Coding Redirect.php
- As shown in part-2, when a query of short URL such as tx01.comeze/urlR/abcXYZ is fired, it gets redirected as redirect.php?id=abcXYZ
- This means that our long url’s base 64 part (mentioned in part -1) i.e “abcXYZ” in this URL is sent as GETparameter to that page. Hence we can use the GET array on redirect.php script to get the base 64 part.
- We can do this in PHP using:
- After this, we can check whether the variable is having any value or not by PHP’s isset function. And if the link is not set (not have any value) then just redirect the page to a static page which shows that URL is invalid:
- header('Location: invalid_url.html'); //this is used to redirect user to invalid_url.html
- Now, as we have the base 64 value stored in a variable and if it has some value in it, we can now connect to the database as described in part -1.
- After that we just have to search this $link variable’s value inside our databse table urlinfo like this:
-
1 2 3
$query="SELECT `long_url` from `urlinfo` WHERE `short_url`=?"; $stmt=$db->prepare($query); $stmt->bindParam(1,$links);
- Up to this code we have not yet fired the query as you can see and now you are familiar with the flow of PDO in PHP that we have to call execute() to fire the query. So we will just use if condition to check whether query executes of not and if it executes than are there any rows matching our data:
-
1
if($stmt->execute() && $stmt->rowCount()>0)
- Now if the above check succeeds we will fetch long URL of that respective row from our database using fetch() method of PDO:
-
1 2
$row = $stmt->fetch(PDO::FETCH_ASSOC); $redirection_url=$row['long_url'];
- So Now we have our long URL stored in our variable $redirection_url. Hence we can redirect user to this url using PHP’s header function.
- header('Location: '.$redirection_url);
Be
Careful: If
anything gets echoed by mistake in this PHP page before header function, then
it won’t work. Because PHP or any other server side language sends header only
once in one request, hence if something gets echoed by mistake, then PHP page
automatically creates its own header and the echo text and sends it to the
client.
So, this is it. We
have successfully made our URL shortener in PHP. If you have any doubt
regarding the code, flow, or logic you can ask in the comment section. Also if
you want to improve my logic than I am always happy to learn something new!
Keep
reading, keep practicing. Happy coding!
Comments
Post a Comment