0

I have done urlencode of the variable before passing to the URL

http://example.com/Restaurants?alias=F%26B

But when I try to print like in the page

 $alias =  rawurldecode($_GET['alias']);
 echo $alias;

it prints only F. How to solve this?

1
  • mod_rewrite can actually decode URLencoded ampersands. Commented Mar 17, 2010 at 19:50

5 Answers 5

3

I doubt that $_GET['alias'] exists when requesting a URL with the query aliasF%26B. It’s rather $_GET['aliasF&B'] that’s getting populated.

In this case you need to use $_SERVER['QUERY_STRING'] to get the full query.

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

Comments

1

It looks like you are not using the query string "correctly." It should be in key=value pairs. I would look at using $_SERVER['QUERY_STRING'] to get your information instead.

6 Comments

i tried to print like echo $_SERVER['alias']; but not printing anything
No no, QUERY_STRING is a keyword that defines the information after the ? in your URL. This would equal "alias=F%26B"
hey i have few things to explain over here . basically i have .htaccess entry like [CODE] RewriteRule ^Restaurants/(.*)$ /restaurant_rating.php?alias=$1 [L][/CODE] so i pass like [CODE]abc.com/Restaurants/F%26B [/CODE] which gets converted to [CODE]abc.com/restaurant_rating.php?alias=F%26B[/CODE] internally . so when i tried directly to use [CODE]abc.com/restaurant_rating.php?alias=F%26B[/CODE] and print alias . it prints properly but when i use [CODE]abc.com/Restaurants/F%26B [/CODE] and print alias it prints only F .how to solve this problem ?
Hmm. Sounds like the RegEX for your Rewrite Rule isn't working. This might be better as a separate question.
Wait, I think I know what is happening! What is the output of $_SERVER['QUERY_STRING'].
|
1

You don't need to urlencode the pair. You only need to urlencode name and a value as such:

Wrong:

urlencode('aliasF=B')

Correct:

urlencode('aliasF') . '=' . urlencode('B')

Comments

1

AFAIK $_GET are already decoded.

See php.net

The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.

Comments

0

It is possible to solve this problem by using a different encoding system specific for your situation:

function encode($string)
{
  $result = str_replace("|","||",$string);
  return str_replace("&","|20",$result);
}

function decode($string)
{
  $result = str_replace("|20","&",$string);
  return str_replace("||","|",$result);
}

This will basically create a separate escaping system using the '|' character. That character can be anything you normally don't use and isn't an field separator.

Here, Apache won't transform the URL to something different, thus voiding the conversion. Also browsers won't transform it.

Mind that you would decode($_GET['alias']) and encode() the url that the user is pressing or the script is following.

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.