tags:

views:

58

answers:

2

I want to check which URL someone is currently on. For example:

if(url=index.php?p=contact) {
    echo the code i want to run, 
}
else {
    do nothing 
}

So basically, I want to run a block of code when the user is on index.php?p=contact

+4  A: 

The current requested URI path plus query is available in $_SERVER['REQUEST_URI'] and the filename of the processing script in $_SERVER['SCRIPT_FILENAME'].

Gumbo
So, if($_SERVER['REQUEST_URI'] == "index.php?p=contact") {code}it should work ?
Andre
@Andre: The URI path does always begin with a `/`. So the value would rather be `/index.php?p=contact`. But that must be the actually requested URI. If you use some rewriting techniques the external/internal URI may differ.
Gumbo
Gumbo
+1  A: 

If you need to check the complete path, see Gumbo's answer. If index.php is only accessible by navigating to that name directly (that is, you know if index.php is being executed the user must've gone to index.php, and you're not using something like URL rewriting), it probably makes much more sense to just check:

if($_GET['p'] == 'contact')

within index.php. If the condition is being reached, index.php is executing and clearly that's the page the user is on

Michael Mrozek
Thanks, that worked for me!
Andre