Tools, FAQ, Tutorials:
Protecting Special Characters in Query String in PHP
How To Protect Special Characters in Query String in PHP?
✍: FYIcenter.com
If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode():
<?php
print("<html>");
print("<p>Please click the links below"
." to submit comments about FYICenter.com:</p>");
$comment = 'I want to say: "It\'s a good site! :->"';
$comment = urlencode($comment);
print("<p>"
."<a href=\"processing_forms.php?name=Guest&comment=$comment\">"
."It's an excellent site!</a></p>");
$comment = 'This visitor said: "It\'s an average site! :-("';
$comment = urlencode($comment);
print("<p>"
.'<a href="/processing_forms.php?'.$comment.'">'
."It's an average site.</a></p>");
print("</html>");
?>
If you copy this script as submit_comments.php to your Web server, and click the first link, you will get:
query_string = name=Guest&comment=
I+want+to+say%3A+%22It%27s+a+good+site%21+%3A-%3E%22
Number of values: 2
name = Guest
comment = I want to say: "It's a good site! :->"
If you click the second link, you will get:
query_string
= This+visitor+said%3A+%22It%27s+an+average+site%21+%3A-%28%22
Number of values: 1
This_visitor_said:_\"It\'s_an_average_site!_:-(\" =
Now you know that urlencode() all special characters into HEX numbers. To translate them back, you need to apply urldecode().
⇒ Supporting a Multiple-Page Form in PHP
2022-09-09, ∼5673🔥, 1💬
Popular Posts:
Tools, FAQ, Tutorials: JSON Validator JSON-XML Converter XML-JSON Converter JSON FAQ/Tutorials Pytho...
Can Two Forms Be Nested? Can two forms be nested? The answer is no and yes: No. You can not nest two...
How to use the "set-backend-service" Policy Statement for an Azure API service operation? The "set-b...
How To Copy Array Values to a List of Variables in PHP? If you want copy all values of an array to a...
How to reinstall npm with a node version manager? I am getting permission errors with the current ve...