Forum Moderators: coopster

Message Too Old, No Replies

How to check path?

         

sodani

2:53 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



Hi, can someone tell me how to check a path and return TRUE if it matches? For example, checking if the path == domain.com/user

Additionally, can I add a wildcard to it? like domain.com/user/*?

Thanks!

barns101

3:52 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



By "path" do you mean URL? The URL can be accessed by using $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] together. Then you could check for the instance of a string with strpos() [php.net] or preg_match() [php.net] if you want to use regular expressions.

sodani

6:53 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



Yes, the URL. I have some paths (domain.com/myaccount, domain.com/user) and I want to check if the current page is "myaccount" and if it is, then print something. I just need a function that will out put a T or F depending on whether the current path is the one I specified.

barns101

9:47 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



In that case you should use strpos() [php.net].

coopster

9:54 pm on Sep 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



An example using preg_match():
$myPath = (preg_match("/\/myPath\//", $_SERVER['REQUEST_URI'])) ? 'T' : 'F';

sodani

10:51 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



Great. Thank you!