foochuck Posted July 13, 2009 Share Posted July 13, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/ Share on other sites More sharing options...
phporcaffeine Posted July 13, 2009 Share Posted July 13, 2009 echo $_SERVER['PHP_SELF'] .... (assuming an Apache server) Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-874649 Share on other sites More sharing options...
foochuck Posted July 13, 2009 Author Share Posted July 13, 2009 Apache server is correct. Thank you Ryan. Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-874651 Share on other sites More sharing options...
Box Posted July 13, 2009 Share Posted July 13, 2009 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-874660 Share on other sites More sharing options...
foochuck Posted July 13, 2009 Author Share Posted July 13, 2009 Thanks Box, that comes in handy as well. Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-874661 Share on other sites More sharing options...
wildteen88 Posted July 13, 2009 Share Posted July 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-874662 Share on other sites More sharing options...
foochuck Posted July 22, 2009 Author Share Posted July 22, 2009 *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? Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-880574 Share on other sites More sharing options...
litebearer Posted July 22, 2009 Share Posted July 22, 2009 perhaps missing one opening parens before "(basename"? Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-880583 Share on other sites More sharing options...
foochuck Posted July 22, 2009 Author Share Posted July 22, 2009 ahhhh...lol litebearer..I had an extra parens in there...t/y Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-880585 Share on other sites More sharing options...
foochuck Posted July 23, 2009 Author Share Posted July 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-881306 Share on other sites More sharing options...
Mark Baker Posted July 23, 2009 Share Posted July 23, 2009 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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-881314 Share on other sites More sharing options...
foochuck Posted July 23, 2009 Author Share Posted July 23, 2009 Thank you Mark, that helps a lot! Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-881381 Share on other sites More sharing options...
foochuck Posted July 24, 2009 Author Share Posted July 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-882145 Share on other sites More sharing options...
foochuck Posted July 24, 2009 Author Share Posted July 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165823-detect-the-name-of-the-php-page/#findComment-882154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.