By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,035 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,035 IT Pros & Developers. It's quick & easy.

passing & in url variable

pradeepjain
100+
P: 563
hi,
I need to pass a variable like alias='F&B' in url. but when i try to get the value in next page using $_GET ,its breaking.

url like

Expand|Select|Wrap|Line Numbers
  1. http://example.com?alias=F&B
how do i pass it as entire data and get it in next page?
Mar 17 '10 #1
Share this Question
Share on Google+
11 Replies


Atli
Expert 5K+
P: 5,058
Hey.

The & character has special meaning in the query string of an URL. You need to urlencode() it before adding it.

If you can't do it via PHP, the URL encoded replacement for "&" is "%26".
Mar 17 '10 #2

pradeepjain
100+
P: 563
i have done that.i use ralurlencode
and the url looks like

Expand|Select|Wrap|Line Numbers
  1. http://example.com/Restaurants?aliasF%26B
but when i try to print like

Expand|Select|Wrap|Line Numbers
  1. $alias =  rawurldecode($_GET['alias']);
  2. echo $alias;
its prints only F. how to solve it?
Mar 17 '10 #3

Atli
Expert 5K+
P: 5,058
You forgot the = in the URL, between the key and the value.

And you don't need the rawurldecode() call. The query-string is decoded automatically.
Mar 17 '10 #4

pradeepjain
100+
P: 563
hey the = i forgot while typing . but still its not working.
Mar 18 '10 #5

pradeepjain
100+
P: 563
hey i have few things to explain over here . basically i have .htaccess entry like

Expand|Select|Wrap|Line Numbers
  1.  RewriteRule ^Restaurants/(.*)$ /restaurant_rating.php?alias=$1 [L]

so i pass like
Expand|Select|Wrap|Line Numbers
  1. http://abc.com/Restaurants/F%26B  
which gets converted to

Expand|Select|Wrap|Line Numbers
  1. http://abc.com/restaurant_rating.php?alias=F%26B
internally .

so when i tried directly to use
Expand|Select|Wrap|Line Numbers
  1. http://abc.com/restaurant_rating.php?alias=F%26B
and print alias . it prints properly but when i use

Expand|Select|Wrap|Line Numbers
  1. http://abc.com/Restaurants/F%26B  
and print alias it prints only F .how to solve this problem ?
Mar 18 '10 #6

Atli
Expert 5K+
P: 5,058
Ok, I see.

The problem here seems to be that by using a mod_rewrite re-route, you are essentially making two requests. - mod_rewrite internally re-directs you from the original URL to the re-written version.

So when you start out like this:
- http://example.com/Restaurants/F%26B
Apache takes the original URL and translates %26 to & (as per usual), and then hands it over to mod_rewrite, which re-writes the URL and redirects you, using & instead of %26.
- http://example.com/restaurant_rating.php?alias=F&B
Which again, makes the URL invalid (the & being read as a variable seperator, rather than a part of the value.)

What you need to do to overcome this, is to manipulate the original URL so that when it is re-written by mod_rewrite, the outcome of the redirect is a valid URL. - To that end, you can encode the % in the %26 value, so when it is rewritten it becomes just a normal %.
- http://example.com/Restaurants/F%2526B
Which is translated to
- http://example.com/restaurant_rating.php?alias=F%26B
From which PHP can extract "F&B".


P.S.
Also take into account, that if mod_rewrite does multiple re-writes (chain rewrites), then each of them would need to be accounted for.

... You may well be better of either using an ID rather than a name, or restrict names to non-special characters.
Mar 18 '10 #7

Atli
Expert 5K+
P: 5,058
It seems that a workaround for this problem exists in Apache 2.2. (See mod_rewrite - Apache HTTP server.)

If you add the "B" flag to the rewrite, mod_rewrite will escape special chars for you.
Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^(.*)$ index.php?show=$1 [L,B]
Mar 18 '10 #8

pradeepjain
100+
P: 563
i tused the option B like u said .


Expand|Select|Wrap|Line Numbers
  1.  RewriteRule ^(.*)$ index.php?show=$1 [L,B]
but apache is throwing internal server error
Mar 19 '10 #9

Atli
Expert 5K+
P: 5,058
Which version of Apache are you using? Like I said, this only works with Apache 2.2. If you are using an earlier version, you can't use that. You will need to double-escape instead. (Or just upgrade your server.)

In any case, check the Apache error logs and see what the problem is, exactly.
The Error log is usually located under: "logs\error.log" in your Apache install directory, or under: "/var/log/apache/" on Linux. (Can vary based on the type of install you used.)
Mar 19 '10 #10

pradeepjain
100+
P: 563
apache version on hosted server is 1.3.33 .. how do i work around this problem!
Mar 19 '10 #11

Atli
Expert 5K+
P: 5,058
There is no workaround. The workaround was added in 2.2, so if you are using an older version you can not use it.

Unless you can upgrade, you would just have to resort to double-escaping, like I explained in post #7.

P.S.
You may want to consider upgrading Apache, regardless. It's been twelve years since 1.3 was released, and the newer versions have been improved quite a lot.
Mar 19 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.