Jump to content

Detect the name of the php page?


foochuck

Recommended Posts

I'm working with two php files - index.php & news.php - I'm putting an include file in both pages.

 

Inside of the include file I would like to write a line of code that will check to see if the page is called 'index.php' if it is, it will execute something.

 

What function can I use to detect the name of the page?

 

if(thisPage = 'index.php') {
echo 'I am index.php';
}

 

Thanks...

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

just as a bit of extra info basename($_SERVER['PHP_SELF']) will remove the directories from the result and just give you index.php or news.php

 

if(basename($_SERVER['PHP_SELF']) = 'index.php') {
echo 'I am index.php';
}

or simply use the $_SERVER['SCRIPT_NAME'] variable instead.

  • 2 weeks later...

*bump*

 

I'm trying to use this code:

 

if (basename($_SERVER['SCRIPT_NAME'])) == "index.php") {
$bodyClass = "home";
}

 

and I keep getting this error:

 

Parse error: syntax error, unexpected T_IS_EQUAL in /home/.backstitching/user/mysite.com/index.php on line 19

 

I'm not sure why I'm getting this, when I echo:

 

echo basename($_SERVER['SCRIPT_NAME']);

 

I get 'index.php'

 

Any suggestions on what I'm doing wrong?

Hey Guys,

 

I have two more questions that go along the lines of this function / script...

 

1. I have several pages that are named:

 

bio-1.php, bio-2.php, bio-3.php

 

How can I detect if the string "bio" is contained in the name of the current PHP page?

 

2. I have a folder named "portfolio" with different pages in it just with random names - I want to detect if the pages are contained in the "portfolio" folder - can I do that with this script?

 

Thanks

 

FOO

How can I detect if the string "bio" is contained in the name of the current PHP page?

if (stripos($_SERVER['SCRIPT_NAME'],'bio') !== false) {
echo 'found bio<br />';
}

 

2. I have a folder named "portfolio" with different pages in it just with random names - I want to detect if the pages are contained in the "portfolio" folder - can I do that with this script?

$folderName = explode('/',pathinfo($_SERVER['SCRIPT_NAME'],PATHINFO_DIRECTORY));

if (array_pop($folderName) == 'portfolio') {
echo 'found portfolio<br />';
}

 

 

 

2. I have a folder named "portfolio" with different pages in it just with random names - I want to detect if the pages are contained in the "portfolio" folder - can I do that with this script?

$folderName = explode('/',pathinfo($_SERVER['SCRIPT_NAME'],PATHINFO_DIRECTORY));

if (array_pop($folderName) == 'portfolio') {
echo 'found portfolio<br />';
}

 

 

I'm getting this error from the 2nd script:

 

Warning: pathinfo() expects parameter 2 to be long, string given in /home/.backstitching/user/mysite.com/index.php on line 40

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.