How do I detect the URL in the address bar using PHP?

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
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

How do I detect the URL in the address bar using PHP?

Post by adders »

Hello All

I'm looking to decet the URL in the address bar of the browser and when it contains a url specified by the $nodisplay variable the code mosLoadModules ( 'header', -2); will not run. Basically I don't know how to reference the URL.

I think I've made some headway - can anyone fill in the blanks (blanks = ???????????????).

So I need to add code that detects the current URL in the place of the ?????????????.

Just to warn you there may be other errors too.

I use Joomla CMS by the way and for what I want to do the module publishing functions aren't sufficient, the frontpage url is just an example I actually have to stop code running for specific pages within individual components.

Code: Select all

<html> 
   <head> 
      <?php 
           $url = ?????????????????; 
           $nodisplay = "index.php?option=com_frontpage&Itemid=1"; 
       ?> 
   </head> 
   <body> 
      <div id="header"> 
         <?php 
            if 
            ( $nodisplay == $url ) 
            { 
            return; 
            } 
            else 
            { 
            mosLoadModules ( 'header', -2); 
            } 
            ?> 
         </div> 
    </body> 
</html>

Hope someone can help

Cheers

Paul
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

Code: Select all

$url = substr($_SERVER['REQUEST_URI'],1);
But it won't work if there will be additional parameter, different case or order.

My suggestion would be:

Code: Select all

$pos = strpos($_SERVER['REQUEST_URI'], '?');
    if ( $pos === false or !isset($_GET['option']) or !isset($_GET['Itemid']))
    {
        $url = '';
    } else {
        $url = substr($_SERVER['REQUEST_URI'],1, $pos); 
        $url .= 'option='.$_GET['option'].'&Itemid='.$_GET['Itemid'];
    }
Tried not to change anything else. Haven't tested, but it should work (if instead of "Itemid" will be "itemid" then it won't work).
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

My http client doesn't have an address bar...
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

Post by adders »

Thanks Kaszu - it's been bugging me for a while.

I've give it a go, there shouldn't be any additional parameters.

Cheers

Adders
adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

Post by adders »

Hello Again

Thought I'd let people that Kaszu's advice worked.

Now when $nodisplay matches the current URL the code I want skipped isn't run, cool.

By the way does any one know how to code this up for more than one URL or URL's that match a certain pattern?

So that the code doesn't run on several pages.

Code: Select all

<head>
$url = substr($_SERVER['REQUEST_URI'],1);;
$nodisplay = "vos_new/index.php?option=com_frontpage&Itemid=1";
</head>
<body>
   <?php 
				if ($nodisplay == $url) 
					{ 
					} 
				else 
					{ 
					mosLoadModules ( 'header', -2); 
					}  
   ?>
</body>
Cheers

Adders
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

adders
Forum Newbie
Posts: 9
Joined: Thu Oct 26, 2006 2:37 am

Post by adders »

Cheers mate - I'll check it out when I get a chance tomorrow.
Post Reply