note 56426 added to function.urlencode
| From: | bisqwit at iki dot fi | Date: | Fri, 02 Sep 2005 08:27:46 +0000 |
| Subject: | note 56426 added to function.urlencode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94640@lists.php.net to get a copy of this message | ||
Constructing hyperlinks safely HOW-TO:
<?php
$path_component = 'machine/generated/part';
$url_parameter1 = 'this is a string';
$url_parameter2 = 'special/weird "$characters"';
$url = 'http://example.com/lab/cgi/test/'.
rawurlencode($path_component) . '?param1=' . urlencode($url_parameter1) .
'¶m2=' . urlencode($url_parameter2);
$link_label = "Click here & you'll be <happy>";
echo '<a href="', htmlspecialchars($url), '">',
htmlspecialchars($link_label), '</a>';
?>
This example covers all the encodings you need to apply in order to create URLs safely without
problems with any special characters. It is stunning how many people make mistakes with this.
Shortly:
- Use urlencode for all GET parameters (things that come after each "=").
- Use rawurlencode for parts that come before "?".
- Use htmlspecialchars for HTML tag parameters and HTML text content.
----
Manual Page -- http://www.php.net/manual/en/function.urlencode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56426
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56426&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56426&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56426&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56426&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56426&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56426&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56426&report=yes
Search -- http://master.php.net/manage/user-notes.php