Jump to content

Can PHP detect name of page


The14thGOD

Recommended Posts

I was wondering, I have a navigation on my site, that is pretty long, and it would probably change over time, so I wanted to make it an include. The problem is however, I need to detect on certain pages variables that I send to the URL. I need to do this in order to give a certain part of the navigation a new class so that it is highlighted.

 

So basically, is there a way for PHP to detect which page is loaded (ie. index.php, about.php, gallery.php)?

 

Could someone perhaps point me in the right direction?

 

Thanks,

 

The14thGOD

Link to comment
https://forums.phpfreaks.com/topic/40814-can-php-detect-name-of-page/
Share on other sites

You could either close and open php, or use heredoc.

 

Example for the "open & close":

<?php
//Some regular php...
$a = 1;
$b = $a + 1;

//Let's stop to print some html!
?>
<tag>
</tag>
<?php
//We can continue with our php 
echo $a;
?>

 

Heredoc

Heredoc is a way to output, just like you can use double quotes or single quotes. More info here.

Example:

<?php
//We want to echo some html!
echo <<<HEREDOC
<html>
<head>
<title>Hi!</title>
</head>
<body>

</body>
</html>
HEREDOC;
?>

 

 

Hope it helps,

Orio

i faced similar problem some time before and i solved it using the following code.

to access which page is being displayed use 

      $url=getenv("SCRIPT_NAME"); //this will simply assign the whole path of the page to URL variable

and to check the exact page i just used:

      if(strcmp(substr($url,-9,9),"index.php")==0)

      {

              //code goes here....

      }

but the 9 and -9 depends on the length of the url

 

it may help

Or you can simply do

 

<?php 

echo '<table >

<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>'.$some_variable.'</td></tr>
</table>'

?>

 

But I prefer to open and close php as in the first example.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.