Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials
And Offer Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development Right Via Email.

Encode And Decode URL Using PHP

Saturday, Aug 20, 2016

Tags:- PHP HTML


In this tutorial we will show you how to encode and decode url using PHP.Encoding and decoding is required before sending and after sending the url to replace some special characters from url.


See Demo
Encode And Decode URL Using PHP


To Encode And Decode URL It Takes Only Three Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to encode and decode url
  3. Make a CSS file and define styling


Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name url.html

<html>
<head>
<link href="url_style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="wrapper">

<div id="url_div">
 <form method="post" action="encode_decode.php">
 <input type="text" name="url" placeholder="Enter URL To Encode">
 <input type="submit" name="encode_url" value="ENCODE">
 </form>

 <form method="post" action="encode_decode.php">
 <input type="text" name="url" placeholder="Enter URL To Decode">
 <input type="submit" name="decode_url" value="DECODE">
 </form>
</div>

</div>
</body>
</html>

In this step we create two forms one for to enter url for encoding and another is for url decoding and they send the url to 'encode_decode.php' page.



Step 2.Make a PHP file to encode and decode url

We make a PHP file and save it with a name encode_decode.php

<?php
if(isset($_POST['encode_url']))
{
 $url = $_POST['url'];
 $encodedUrl = urlencode($url);
 $converted_url=$encodedUrl;
}

if(isset($_POST['decode_url']))
{
 $url = $_POST['url'];
 $decodedUrl = urldecode($url);
 $converted_url=$decodedUrl;
}
?>

In this step we create two isset() conditions to encode and decode the urls.In first condition we get url value and encode url using php urlencode() function.In second condition we use urldecode() function to decode url.



Step 3.Make a CSS file and define styling

We make a CSS file and save it with a name url_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#D6EAF8;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#3498DB;
}
#wrapper h1 p
{
 font-size:18px;
}
#url_div input[type="text"]
{
 width:300px; 
 height:35px;
 border:1px solid #AED6F1;
 padding-left:10px;
 font-size:17px;
}
#url_div input[type="submit"]
{
 background-color:#3498DB;
 border:none;
 width:100px;
 height:35px;
 color:white;
}

That's all, this is how to encode and decode url using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email