Page 1 of 1

php_self and passing variables

Posted: Mon Apr 25, 2005 9:07 am
by bytte
I want to add one variable to the end of the page's current url.
I thought of doing:

Code: Select all

<a href=&quote;<?PHP echo $_SERVER&#1111;'PHP_SELF']?>?variable&quote;>link</a>
That works great in all cases except when the current url is something like:

http://www.url.com/start/index.php?ID=7

The first problem is that $_SERVER['PHP_SELF'] will not return the "?ID=7" part.
The second problem is that, even if it did, the final url would be:

http://www.url.com/start/index.php?ID=7?variable

That doesn't make sense either.

Is there a workaround so that PHP_SELF returns the part after the ? as well?

Posted: Mon Apr 25, 2005 10:07 am
by hongco
try this: $_SERVER['QUERY_STRING']

Posted: Mon Apr 25, 2005 10:35 am
by bytte
Strange, this is what it now links to:

http://www.url.com/start/?ID=7?variable

So, it forgets about the index.php part!
Any idea?

Posted: Mon Apr 25, 2005 10:42 am
by phpScott
first of all you have some quote issues with your the code you posted

Code: Select all

<a href="<?PHP echo $_SERVER['PHP_SELF']?>?variable">link</a>
try

Code: Select all

<a href="<?php echo $_SERVER['PHP_SELF'];?>?someVar=<?php echo $var; ?>">somelink</a>
as it seems to be following your pattern.

at the top of your page you will likely have to use

Code: Select all

$variableName = $_GET['someVar'];
to retrieve the value

Posted: Mon Apr 25, 2005 10:48 am
by bytte
I can't do that since the code is in my header that re-appears on every page.
There's different variable names and values passed on different sections on my site.

Actually, I use the code to switch to another CSS file.

Posted: Mon Apr 25, 2005 2:19 pm
by John Cartwright
take a look at

Code: Select all

print_r($_SERVER);
for all your needs.