Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to turn a space into a %20?

Browser reads it as %2520 no matter what I do

         

MichaelBluejay

9:14 am on May 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So my script redirects to another page with a variable in the query string:

$variable = "boo boo";
print "Content-type: text/html\n";
print "Location:http://hithere.com?$variable";

Works fine in Safari and IE6/Win.but in Mac IE5 the browser location bar reads "http://hithere.com?boo%2520boo".

No problem, I thought, I'll just change the space to a %20:

$variable =~ s/ /%20/;

No luck, same prablem. Substituting \x20 for the %20 didn't help either. I tried various flavors of pack, but I was just guessing since I really don't understand what I'm doing there.

How the heck can I call example.com?boo%20boo?

Thanks for your help. Oh, by the way, the variable really, really has to have a space in it. That would be an easy workaround if I could use strings without spaces, but I can't. :(

ytswy

10:14 am on May 25, 2004 (gmt 0)

10+ Year Member



<edit>Sorry.. misread the forum title.. I don't know Perl at all..</edit>

My php is pretty basic, so I may have this wrong, but I think you need to use urlencode() and urldecode() - just urlencode your variable before putting it in the query string, and urldecode it before using it on the target page,

HelenDev

10:44 am on May 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you need to use urlencode() and urldecode()

I think the perl equivalents of these are

uri_escape($val)
uri_unescape($val)

MichaelBluejay

7:45 pm on May 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I tried this:

use URI::Escape;
uri_escape($userid);

But I still have the same problem: <filename.html?$userid> becomes <filename.html?word1%2520word2> in the browser. :(

timster

2:18 pm on May 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like instead of more encoding, you may need less. Note the encoded value of '%' is '%25'.

The CGI below mimics the problem you're having.

$\="\n";
my $var = "space space";
encode();

if ($ENV{'HTTP_USER_AGENT'}=~/Mozilla\/4.0 \(compatible; MSIE 5.\d+;/) {
encode(); # Oops, encoded too many
}
print "Content-Type: text/html; charset=ISO-8859-1";
print "Location:http://localhost/index.html?$var\n";

sub encode {
$var =~s/\W/ sprintf("%s%x", '%', ord($&)) /ge;
}

VectorJ

2:27 pm on May 26, 2004 (gmt 0)

10+ Year Member



I think the problem may be that you're encoding the space to the wrong value. A space in a URL query string is represented by a + (plus sign), not its hexadecimal value.

timster

3:12 pm on May 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



VectorJ is correct, by the book, spaces in the get querystring should be encoded as pluses. However, if you don't encode spaces in a query string, many browsers will do that automatically as '%20'

Note that if your cgi is encoding '%' as %25, it will probably encode '+' as '%2b'.

MichaelBluejay

10:00 am on May 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay, that makes sense to put plus signs in as word delimeters. Problem is that I was already using pluses as the delimiter for query string items, but I went ahead and switched to hyphens for that. I actually ran into more unexpected problems than I anticipated with all the converting back and forth between the "plussed" and the "spaced" values everywhere in my scripts and I almost abandoned it, thinking that those few users in Mac IE5 would just have to suffer, but I decided that my lack of programming prowess wasn't their fault so I stuck with it and I finally got everything to work. Thanks again for the tip that the way to get it to work was to abandon the use of spaces.

timster

1:44 pm on May 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I stuck with it and I finally got everything to work.

Well, don't keep us in the dark. How did you fix it?

MichaelBluejay

10:10 am on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry I wasn't clear. I used plus signs as word delimiters, and hyphens for query string item delimiters.

And then I changed all my various scripts to deal with that. :)