[PHP] URL encryption and decryption (reversible)

URL encryption and decryption (reversible)

 

Encryption: urlencode

urlencode ( string $str ) : string

This function makes it easy to encode a string and use it in the request part of the URL, and it also makes it easy to pass variables to the next page.

Returns a string. All non-alphanumeric characters in this string except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as a plus sign (+). This encoding is the same as the encoding of WWW form POST data, and the same as the media type encoding of application/x-www-form-urlencoded. Due to historical reasons, this encoding is different from the RFC1738 encoding in that spaces are encoded as plus signs (+).

Example 1:

$userInput = "测试";
echo '<a href="mycgi?foo='. urlencode($userInput). '">';
// 结果:<a href="mycgi?foo=%E6%B5%8B%E8%AF%95">

 Because the output content is html tags, you need to view the source code of the web page when viewing, or use the htmlentities function.

$userInput = "测试";
//echo '<a href="mycgi?foo='. urlencode($userInput). '">';
echo htmlentities('<a href="mycgi?foo='. urlencode($userInput). '">');

Output result:

(At this time, it is output as a string)

Example 2:

When multiple parameters:

$foo = "测试1";
$bar = "测试2";
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';

echo '<a href="mycgi?' . $query_string . '">';

 

 

note:

Be careful with variables that match HTML entities. Things like &, © and £ will be parsed by the browser and the actual entity will be used instead of the expected variable name. This is obvious confusion, and the W3C has warned people for several years. Reference address: »http://www.w3.org/TR/html4/appendix/notes.html#hB.2.2 .

PHP supports changing the parameter separator to the semicolon recommended by W3C through the arg_separator.ini command. Unfortunately, most user agents do not send form data in semicolon separator format. The simpler solution is to use & instead of & as the separator. You don't need to modify PHP's arg_separator for this. Let it remain &, and only use  htmlentities()  or  htmlspecialchars()  to encode your URL.

 

Decryption: urldecode

urldecode ( string $str ) : string

Decode any %## in the given encoded string. The plus sign ('+') is decoded into a space character.

Example 1 (Encryption and decryption of the entire link):

$url = '<a href="http://www.xxx.com/test?foo=测试1&bar=测试2">';
$encodeUrl = urlencode($url);
echo $encodeUrl . "\n";
echo urldecode($encodeUrl);

result:

 

Example 2 (Encryption and decryption of parameters):

Just use urldecode to decrypt the received parameter value

//<a href="mycgi?foo=%E6%B5%8B%E8%AF%951&bar=%E6%B5%8B%E8%AF%952">
$param1 = '%E6%B5%8B%E8%AF%951';
$param2 = '%E6%B5%8B%E8%AF%952';
echo urldecode($param1) . "\n";
echo urldecode($param2) ;

result:

 

Guess you like

Origin blog.csdn.net/I_lost/article/details/105388252