einerwitzen has asked for the wisdom of the Perl Monks concerning the following question:

i am passing variables through a URL forward (yea i know but i gotta) but some of them have a space in the variable content and anything after the first space isn't being handled in the recieving script.
http://www.adomane.com?var=yes&var2=yes and no&var3=okay

if i then put in the recieveing script
print "$var <br>"; print "$var2 <br>"; print "$var3 <br>";
it comes out like this:
yes yes
after the first space nothing works! Is this normal, how can i get this to work?

anything will help!

thanks!

Replies are listed 'Best First'.
•Re: pass var in URL with spaces?
by merlyn (Sage) on May 25, 2002 at 07:23 UTC
    Well, let's see how to construct this the proper way:
    use URI; my $u = URI->new("http://www.adomane.com"); $u->query_form(var => 'yes', var2 => 'yes and no', var3 => 'okay'); print $u->as_string;
    which prints:
    http://www.adomane.com?var=yes&var2=yes+and+no&var3=okay
    Ahh, so that's the way to do it.

    -- Randal L. Schwartz, Perl hacker

Re: pass var in URL with spaces?
by Anonymous Monk on May 25, 2002 at 10:52 UTC
    What you need to do is called URLEncoding. You can read about why here
    btw- Your URL (urlencoded) should look something like this
    http://www.adomane.com?var=yes&var2=yes%20and%20no&var3=okay
Re: pass var in URL with spaces?
by arunhorne (Pilgrim) on May 25, 2002 at 19:02 UTC

    As an aside to your question I note that your parameters do not seem to be arbitrary. Can I suggest enumerating them, i.e. 0 for none, 1 for yes, 2 for no, 3 for yes and no.

    The benefits of this are 2 fold... firstly you won't have to use URL encoding as it is guaranteed there are no spaces and secondly it will be harder for an individual to tamper with your URL as parameter meanings will be less verbose- how important this second point is depends on your view of security/purpose of the script.

    If you create two subroutines: enum_param that takes a string (e.g. yes) and returns the number representing it and decode_param that reverses this. These will make it easy to use this process in more than one script.

    Its only a thought and in many cases it might just be easier to URL encode. Hope it helps.

    ____________
    Arun