473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

passing & in url variable

pradeepjain
563 512MB
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
11 5476
Atli
5,058 Expert 4TB
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
563 512MB
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
5,058 Expert 4TB
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
563 512MB
hey the = i forgot while typing . but still its not working.
Mar 18 '10 #5
pradeepjain
563 512MB
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
5,058 Expert 4TB
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
5,058 Expert 4TB
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
563 512MB
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
5,058 Expert 4TB
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
563 512MB
apache version on hosted server is 1.3.33 .. how do i work around this problem!
Mar 19 '10 #11
Atli
5,058 Expert 4TB
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

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

Similar topics

2
by: jbatty | last post by:
I am trying to display an image on my page by using "img src= hyperlink". the hyperlink is stored in a c# variable. but i seem to have the syntax mixed up. This passing the image directly works...
1
by: Vikram | last post by:
i am adding a row in dataset , in which i want to add   (space chracter for html) and then want to bind dataset to asp.net dropdown list box. But & automatically converted to &amp chracter....
0
by: CES | last post by:
All, I was wondering is their a way of passing a variable into an eval() statement? I've created a custom eval() dll to use with my VB.net code. Every thing works fine until I try to pass a...
6
by: brian_harris | last post by:
I have a function that passes a string class pointer to a function, this function is then suppose to fill it and the outer function uses it. But I believe that I am running into problem due to...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
3
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on...
1
by: I.am.the.Buddha | last post by:
I am still learning asp and sql. I am having trouble with passing a variable to within an sql statement. I am sure it is something simple like misuse of quotes. Since it may help if i say what i...
2
satterfieldben
by: satterfieldben | last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would...
6
by: artev | last post by:
Parse error: syntax error, unexpected '=', expecting ')' on function so function pippo($var1, &$var2 = null, $var3 = null) { ....} but if remove & I not have the erros; I must change...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.