Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Tutorials - Protecting Special Characters in Query String

By: FYICenter.com

(Continued from previous topic...)

How To Protect Special Characters in Query String?

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().

(Continued on next topic...)

  1. How To Create a Web Form?
  2. What Are Form Input HTML Tags?
  3. How To Generate a Form?
  4. Where Is the Submitted Form Data Stored?
  5. How To Retrieve the Submitted Form Data?
  6. What Happens If an Expected Input Field Was Not Submitted?
  7. How To Avoid the Undefined Index Error?
  8. How To List All Values of Submitted Fields?
  9. What Are Input Values of SELECT Tags?
  10. How To Specify Input Values for Radio Buttons?
  11. How To Specify Input Values for Checkboxes?
  12. How To Retrieve Input Values for Checkboxes Properly?
  13. How To Supply Default Values for Text Fields?
  14. How To Remove Slashes on Submitted Input Values?
  15. How To Support Multiple Submit Buttons?
  16. How To Support Hidden Form Fields?
  17. How To Generate and Process a Form with the Same Script?
  18. How To Submit Values without Using a Form?
  19. How To Retrieve the Original Query String?
  20. How To Protect Special Characters in Query String?
  21. How To Support Multiple-Page Forms?

Selected Developer Jobs:

More...