Jump to content

URL parameter values not being received when using rawurlencode


DLO2418

Recommended Posts

I am using rawurlencode for my urls, like this: 

 

$price = 100;

$product = "Ipad 5";

 

$url = "";

$url .= "&Price=$price";

$url .= "&Product=$product";

 

$encodedUrl = rawurlencode($url);  

 

when clicking on this URL … : 

<a href="URLencode-recieve.php?rawEncodedLink=<?php echo $encodedUrl; ?>">rawUrlEncode Link</a>

 

 

… it takes you to a page that has this coding:

$encodedUrl = trim($_GET["rawEncodedLink"]);

$decodedUrl = rawurldecode($encodedUrl);

 

Both the $encodedUrl and $decodeUrl variables above have these values: &Price=100&Product=Ipad 5.

 

However the GET method is NOT pulling the Price value from the parameter in the URL

$price = trim($_GET["Price"]);

echo "Price variable: " . $price; // this price variable is empty when echoing it.

Of course the parameters aren't read from the URL. They simply aren't there.

 

What you have is a single URL parameter called “rawEncodedLink”. This parameter contains a bunch of URL-encoded characters. Since they're encoded, they do not have any special meaning for the URL. It's just data. The original “&” is now a literal character.

 

It seems you want something entirely different. If you want to add actual parameters to the URL, you must not encode the “&” character. Use http_build_query() to create a proper list of URL parameters.

Of course the parameters aren't read from the URL. They simply aren't there.

 

What you have is a single URL parameter called “rawEncodedLink”. This parameter contains a bunch of URL-encoded characters. Since they're encoded, they do not have any special meaning for the URL. It's just data. The original “&” is now a literal character.

 

It seems you want something entirely different. If you want to add actual parameters to the URL, you must not encode the “&” character. Use http_build_query() to create a proper list of URL parameters.

Thanks! I thought once you decoded the string, the parameters can be read. I'll test with http build query! 

I thought once you decoded the string, the parameters can be read.

 

Not sure what you mean. PHP parses the URL parameters and sets the $_GET superglobal before the script runs. What you do within the script is your business. If you want to update $_GET with some new value, you'd have to do that yourself (but of course you shouldn't and just use the correct parameters from the beginning).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.