Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP URL Encoding / Decoding

I used the solution accepted for this question for encrypting by id for example in /index.php?id=3 . The problem is I cannot send the encrypted value as an url, example /index.php?id=dsf13f3343f23/23=. Because sometimes it will have weird characters in the url e.g. notice the = sign in the end

like image 927
Imran Omar Bukhsh Avatar asked Apr 14 '11 11:04

Imran Omar Bukhsh


2 Answers

The weird characters in the values passed in the URL should be escaped, using urlencode().


For example, the following portion of code :

echo urlencode('dsf13f3343f23/23=');

would give you :

dsf13f3343f23%2F23%3D

Which works fine, as an URL parameter.


And if you want to build aquery string with several parameters, take a look at the http_build_query() function.

For example :

echo http_build_query(array(
    'id' => 'dsf13f3343f23/23=',
    'a' => 'plop',
    'b' => '$^@test', 
));

will give you :

id=dsf13f3343f23%2F23%3D&a=plop&b=%24%5E%40test

This function deals with escaping and concatenating the parameters itself ;-)

like image 122
Pascal MARTIN Avatar answered Oct 05 '22 12:10

Pascal MARTIN


Use PHP's urlencode() function to encode the value before you put it into a URL.

string urlencode ( string $str )
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.

This function converts "weird" characters, such as =, into a format safe to put into a URL. You can use it like this:

Header('Location: /index.php?id=' . urlencode($id))
like image 27
Lucas Jones Avatar answered Oct 05 '22 13:10

Lucas Jones