[thelist] PHP - Passing Variable in URL with Anchor Trouble!

David Altherr einstein314 at hotmail.com
Fri Aug 17 20:26:52 CDT 2001


While that approach is a viable workaround, it may not be very efficient if
the 'index.php' script is intended to receive a 'main' variable which might
contain URL data with varying data and variable names from multiple
scripts... that is potentially a lot of code to write just for passing the
variables and their approriate values, and that creates another script that
you have to update if you add a variable, (at least the way I imagine it).
I'm assuming that the URL data is used to either redirect to the URL or to
grab the file, at it's specified location, into the current script, I may be
wrong but this should apply regardless.

    To maintain the architecture (i think you're on the right track) the
solution would be to simply encode the URL data to ensure that the browser
just treats the data as a string.  Thus your code on the 'submit' script
would be similar to:

$url_data =
    urlencode(
        'cleaningtips.php?room='.$clean_array['Room']
        .'&surface='.$clean_array['Surface']
        .'&stain='.$clean_array['Stain']
        .'&id='.$clean_array['ID']);

which would produce a similar string stored to $url_data :

cleaningtips.php%3Froom%3Djaskhf%26surface%3Dasdfasdf%26stain%3Dasdfsadfsdfs
ad%26id%3Dfooo

notice no special URL characters.  You then set your anchor as such:

$anchor =
    "<a href='http://$web_address/index.php?main=$url_data>view</a>";

Of course, once the data gets to the destination page, you need to
urldecode() it into the proper URL string:

$main = urldecode($main);

which will give you the URL to use as you please in index.php:

cleaningtips.php?room=jaskhf&surface=asdfasdf&stain=asdfsadfsdfsad&id=fooo

The nice thing about this solution is that you don't necessarily have to
setup a logic structure if 'main' is intended to accept both encoded and
decoded strings, an already decoded URL will decode to itself with one
specific exception that I am aware of: if you use any literal percent signs
preceding a valid two digit hexidecimal string (i.e. %00 - %ff), then this
three character string will be converted to it's one character Unicode
equivalent on a second urldecode().  If this is the case, or you just want
to be safe, then one option might be to have a 'decode' switch in the URL
and a logic structure in the receiving script. e.g.:

in the anchor:
$anchor =
    "<a
href='http://$web_address/index.php?main=$url_data&decode=1>view</a>";

in the receiving script:
if($decode==1){
    $main = urldecode($main);
}

or another format if you don't feel safe overwriting vars (same result):
$url_safe_main = ( $decode==1 ? urldecode($main) : $main);

Hopefully that will save you from changing a lot of your code.

-David Altherr
www.davidaltherr.net


> <snip>
> \********
> *******
>  print "<a
> href='http://$web_address/index.php?main=cleaningtips.php?room=".$c
> lean_arra
> y
> **
> ["Room"]."&surface=".$clean_array["Surface"]."&stain=".$clean_array
> ["Stain"]
> ."&id=".$clean_array["ID"]."' ** target='new'>view</a>\n";
> *******
> ********/
> </snip>
>
> You can only have one '?' per URL, logically enough -- it seperates
> the page to load from the data to send to it.
>
> Workaround: Send all of the variables to index.php and then have
> index.php send them to cleaningtips.php. ie:
>
> http://www.myweb.com/index.php?main=cleaningtips.php&room=KITCHEN&a
> nother=this&another=that
>
>
> Then have index.php load cleaningtips.php with all the variables
> (other than $main which it doesn't need).
>
> I would write the code out, but that is your job not mine :]
>
> Daryl
>





More information about the thelist mailing list