urlencode: encoding data before sending through URL

While sending data through URL or through address bar we can't send them as it is. We need to encode the data to take care some special chars if present inside our data string. Let us try with one example.

Here is a string to send through URL to another page.
$var=" data 1 &  data 2, data 1 + data 2 = data '3. The number is #123 which is 10% of total";
echo $var;
After sending like this we won't receive the same data and it will be truncated from & char onwards. Let us create two PHP pages and to pass one string data from one to other.

Demo of urlencoding

demo-urlencode.php

<?php
$var=" data 1 &  data 2, data 1 + data 2 = data '3. The number is #123 which is 10% of total";
echo $var."<br>";
$var2=urlencode($var);
echo "<br>
<a href='demo-urldecode.php?var=$var'> This link does not use urlencode and post data as it is</a><br>";
echo "<br>
<a href='demo-urldecode.php?var=$var2'> This link does use urlencode and then post data</a><br>";
?>

demo-urldecode.

<?php
echo " <br><b>from url without decoding we get :</b> $_GET[var]<br>";
echo " <br><b>from url after decoding we get :</b> ".urldecode($_GET['var'])."<br>";
echo " <br><b>from url after decoding we get :</b> ".urldecode($_GET['var'])."<br>";
?>

Declaring variables Cookies to store Information Getting Type of Variable var_dump() empty()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    shabbir

    28-09-2016

    how to remove
    <a href='demo-urldecode.php?var=$var'> This link does not use urlencode and post data as it is</a><br>";
    .php from url when passing value
    smo1234

    02-10-2016

    Yes , to show the difference two links are shown, first one is without urlencode and second one is with encode. You can send data without encoding also. If you are sure about the data. For example if you are sending a numeric data like student id then no need to urlencode the data.

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer