Jump to content


Checking a variable $url against the current page URL

  • Please log in to reply
2 replies to this topic

#1 ilh

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 March 2006 - 04:40 PM

I've started a little project in PHP and f00king hell its getting complicated now (for me lol). TBH i have done some stuff which i had thought was going to be too complicated and now i've got stuck
on something which i think should be easy to resolve but i can't figure it out!

First i'll mumble a bit about what i've done but there is a question at the
bottom for any clever ppl :)

So far i have created a template. I then create the page by calling the
template and supplying it a small amount of details (i.e. a query). It then
figures out how many columns there are in the table, names them and then draws
the table and fills in all of the data from the table. Each column name can be
clicked on to arrange the data by that particular column (both ascending and
descending) and each record can be clicked on to load a different page with lots
of other info on it.

This has taken me about a week of pure struggling. But since it is a template
it takes me about 2 mins to now create a page - i just copy the page, rename it
and change the query and its all done

Now the problem i'm having is with a function i kind of erm found :P

Here it is:

public function IsURLCurrentPage($url)
if(strpos($_SERVER['PHP_SELF'], $url )==false)
return false;
return true;

Basically you pass it an URL and it looks up the URL in the address bar and
tells you whether the address you gave it is the current page.

This is all well an good until you use the URL to pass data to the script (i
have used it to pass the column name and direction in order to be able to
arrange the data by any column). As soon as there is this extra data it then
says they don't match and it doesn't display the page quite as it should.

For example the URL might look like /findpc.php?col=Name&dir=asc and i only
want it to check it against the findpc.php bit of the address

So all i need to do is tell it to ignore everything after (and including) the ?
when comparing the addresses. But how?

I'm sure i should be able to figure it out myself but I think my brain has
overloaded. I'm always wary about thinking too much cos i might not have enough
thought power left for emergencies :P

#2 Kris

  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 01 March 2006 - 05:40 PM

This should do the trick:
public function IsURLCurrentPage($url) {
    $pos = strpos($url,"?");
    if($pos !== FALSE) $url = substr($url,0,$pos);
    if(strpos($_SERVER['PHP_SELF'],$url) === FALSE) {
        return FALSE;
    } else {
        return TRUE;

#3 ilh

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 March 2006 - 11:07 AM

taverymuchly! :)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users