php_self and passing variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

php_self and passing variables

Post 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?
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

try this: $_SERVER['QUERY_STRING']
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post 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?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

take a look at

Code: Select all

print_r($_SERVER);
for all your needs.
Post Reply