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
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';
}

Link to comment
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.

Link to comment
Share on other sites

  • 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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />';
}

 

Link to comment
Share on other sites

 

 

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

Link to comment
Share on other sites

Mark,

 

FYI I came up with this code for #2 above and it does the trick...

 

$path_parts = pathinfo($_SERVER['SCRIPT_NAME']);

if (stripos($path_parts['dirname'],'portfolio') !== false) {
$bodyClass = "portfolioBG";
}

 

Thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.