PHP URL Encoding / Decoding

I used the solution made for this question to encrypt by identifier, for example, in /index.php?id=3 . The problem is that I cannot send the encrypted value as a URL, for example /index.php?id=dsf13f3343f23/23 = . Because sometimes it will have strange characters in the url, for example. note the = sign at the end

+8
url php url-encoding
source share
4 answers

Strange characters in the values ​​passed in the URL must be escaped using urlencode( ).


For example, the following part of the code:

 echo urlencode('dsf13f3343f23/23='); 

will provide you with:

 dsf13f3343f23%2F23%3D 

Which works great as a URL parameter.


And if you want to create an aquery chain with several parameters, look at http_build_query() .

For example:

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

will provide you with:

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

This function relates to the shielding and concatenation of the parameters themselves :-)

+20
source share

Use the PHP urlencode() function to encode a value before embedding it in a URL.

string urlencode ( string $str )
This function is useful when encoding a string to be used to request a part of a URL, as a convenient way of passing variables to the next page

This function converts "weird" characters, such as = , into a safe format for placing a URL. You can use it as follows:

 Header('Location: /index.php?id=' . urlencode($id)) 
+4
source share

If you use Base64 to encode a binary value for a URL, there is also an option with a safe URL alphabet and a safe file name .

You can use the strtr function to translate one alphabet from another:

 $base64url = strtr($base64, '+/', '-_'); $base64 = strtr($base64url, '-_', '+/'); 

Thus, you can use these functions to encode and decode base64url:

 function base64url_encode($str) { return strtr(base64_encode($str), '+/', '-_')); } function base64url_decode($base64url) { return base64_decode(strtr($base64url, '-_', '+/')); } 

See also my answer on what is a good way to create a short alphanumeric string from a long md5 hash?

+4
source share

There is no point in encrypting the parameters.
Send it as is:

 /index.php?id=3 

there's nothing bad about that.

+1
source share

All Articles