note 86378 added to function.parse-url

From: Date: Wed, 15 Oct 2008 18:08:46 +0000
Subject: note 86378 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-145389@lists.php.net to get a copy of this message
What about using something like this to safely encoding all the values that are passed in the query portion? Example input: http://www.example.com/?first=john&last=smith&email=john@smith.com Result: http://www.example.com/?first=john&last=smith&email=john%40smith.com <?php function safe_url($url) { // Make sure we have a string to work with if(!empty($url)) { // Explode into URL keys $urllist=parse_url($url); // Make sure we have a valid result set and a query field if(is_array($urllist) && isset($urllist["query"])) { // Explode into key/value array $keyvalue_list=explode("&",($urllist["query"])); // Store resulting key/value pairs $keyvalue_result=array(); foreach($keyvalue_list as $key=>$value) { // Explode each individual key/value into an array $keyvalue=explode("=",$value); // Make sure we have a "key=value" array if(count($keyvalue)==2) { // Encode the value portion $keyvalue[1]=urlencode($keyvalue[1]); // Add our key and encoded value into the result array_push($keyvalue_result,implode("=",$keyvalue)); } } // Repopulate our query key with encoded results $urllist["query"]=implode("&",$keyvalue_result); // Build the the final output URL $url=(isset($urllist["scheme"])?$urllist["scheme"]."://":""). (isset($urllist["user"])?$urllist["user"].":":""). (isset($urllist["pass"])?$urllist["pass"]."@":""). (isset($urllist["host"])?$urllist["host"]:""). (isset($urllist["port"])?":".$urllist["port"]:""). (isset($urllist["path"])?$urllist["path"]:""). (isset($urllist["query"])?"?".$urllist["query"]:""). (isset($urllist["fragment"])?"#".$urllist["fragment"]:""); } } return $url; } ?> ---- Server IP: 66.207.199.35 Probable Submitter: 99.228.14.139 ---- Manual Page -- http://www.php.net/manual/en/function.parse-url.php Edit -- https://master.php.net/note/edit/86378 Del: integrated -- https://master.php.net/note/delete/86378/integrated Del: useless -- https://master.php.net/note/delete/86378/useless Del: bad code -- https://master.php.net/note/delete/86378/bad+code Del: spam -- https://master.php.net/note/delete/86378/spam Del: non-english -- https://master.php.net/note/delete/86378/non-english Del: in docs -- https://master.php.net/note/delete/86378/in+docs Del: other reasons-- https://master.php.net/note/delete/86378 Reject -- https://master.php.net/note/reject/86378 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145389) next »