PDA

View Full Version : [php] How do you encode a URL for a query string?



altonbr
December 3rd, 2008, 05:54 PM
I'm trying to encode a URL and pass it in a query string (I know there are better ways of performing this, but this is a must in my situation. Just pretend it is out of my control.)


<?php

$url = 'http://example.com?pid=12&uid=200'
echo 'http://test.com?url='.urlencode($url);

?>

The resulting URL is invalid as


http://test.com?url=http://example.com?pid=12&uid=200

because urlencode() doesn't seem to work. Neither does rawurlencode(). What am I supposed to use to produce this:


http://test.com?url=http%3A%2F%2Fexample.com%3Fpid%3D12%2 6uid%3D200

pp.
December 3rd, 2008, 06:19 PM
From the looks of it, urlencode() encodes a complete URL and not a part which is to be enclosed in an URL.

Therefore, I presume you should first form the complete URL and urlencode that.

altonbr
December 3rd, 2008, 06:44 PM
From the looks of it, urlencode() encodes a complete URL and not a part which is to be enclosed in an URL.

Therefore, I presume you should first form the complete URL and urlencode that.

Their documentation (http://ca.php.net/urlencode) shows it being used in parts to build a query string.

I think their is another function for the type of escaping I am trying... I just can't remember what it is called.

s.fox
December 3rd, 2008, 06:48 PM
That looks fine to me. You should be able to retrieve it just fine using :

$passedVar = $_GET['url'];

Why do you think its invalid?

Sydius
December 3rd, 2008, 07:59 PM
As an alternative, you can use base64_encode and replace the invalid characters with valid ones. See the first comment to http://us3.php.net/base64_encode

altonbr
December 3rd, 2008, 09:34 PM
That looks fine to me. You should be able to retrieve it just fine using :

$passedVar = $_GET['url'];

Why do you think its invalid?

It's invalid because their is a query string inside a query string and PHP gets confused. It can't pass it properly.


As an alternative, you can use base64_encode and replace the invalid characters with valid ones. See the first comment to http://us3.php.net/base64_encode

I wish I could use something as simple as that, but I'm actually passing this URL to another website via query string.

Does anyone know what it's called when you turn ':' into '%3A' and '//' into '%2F%2F'?

s.fox
December 3rd, 2008, 10:32 PM
you could just use string replace

<?php
$originalString = "I like red";
$originalString = str_replace("red","green",$originalString);
echo $originalString;
?>

hope this helps

altonbr
December 3rd, 2008, 10:54 PM
you could just use string replace

<?php
$originalString = "I like red";
$originalString = str_replace("red","green",$originalString);
echo $originalString;
?>

hope this helps

But then I have to know what characters are not allowed and what '%##' number to assign them to.

I have some resources on it:

You can use JavaScript's encodeURIComponent (http://weierophinney.net/matthew/archives/133-PHP-decoding-of-Javascript-encodeURIComponent-values.html) (unfortunately not an option for me)
A guy with a similar problem, but they don't touch on how he's encoding/decoding (http://www.jeroenwijering.com/?thread=9455)
RFC 1738 reference (http://www.blooberry.com/indexdot/html/topics/urlencoding.htm)
UTF16 version of urlencode() (http://ca.php.net/manual/en/function.urlencode.php#82483)


So I guess urlencode() is doing it's job, I just need ':', '/', '?', etc. to be encoded.

Maybe I will just make a manual function using 'str_replace()', thanks!