13

So I am trying to encode/decode a url that when decoded will return the encoded + symbols from teh url. For example, I encodewebsite.com/index.php?eq=1+12 which when encoded turns the + into %2B, as it should. When I retrieve the value from $_REQUEST['eq'] and use urldecode() it echo's as "1 12". I cannot seem to get the decode to bring back the + so to speak. Am I doing something wrong here, or is there a more efficient/better way to go about doing this? Here is the exact encode/decode lines I use.

Submit Page

<?php
$eq = "1+12";
$send = '<a href="website.com/index.php?eq='.urlencode($eq).'</a>';
echo $send;

Retrieval page

<?php
$eq = urldecode($_REQUEST['eq']);
echo $eq;
?>

3 Answers 3

21

Don't run urldecode, the data in $_REQUEST is automatically decoded for you.

A plus sign, in a URL, is an encoded space. PHP decodes the hex value to a + automatically. Then, by running the result through urldecode, you are manually (and incorrectly) decoding the + to a .

Sign up to request clarification or add additional context in comments.

5 Comments

so I removed the urldecode, and kept the urlencode. It still is registering the $_REQUEST as a space.
It doesn't when I copy/paste that code and remove the urldecode statement.
I'm not sure, maybe the php version is different and functions differently? Or could be other parts of my page conflicting. I figured a workaround by using str_replace(" ", "+", $eq). Probably not ideal, but since my URL will never need spaces, seems to work. Thank you for your help.
It probably is some other part of your code. You should trace through and find out what modifies it and where (starting with checking that the URL in the link is actually containing eq=1%2B12.
@Andrew — That is what the line "urldecode($_REQUEST['eq'])" in the question does. It is the problem.
18

Try using the function rawurldecode() instead of urldecode()

1 Comment

yes, it't the solution
2

I encode it in JavaScript with encodeURIComponent() and decode it in PHP with rawurldecode() and it encodes/decodes properly for me, including the "+", but NOT with urldecode()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.