Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If index.php, show "this", if not, show "this"

Tags:

php

Desperately hoping someone can assist with this. I'm a novice with php. I try and self teach myself through tutorials and I've searched high and low to no avail.

Basically I'm looking to implement an "If index.php page, show foo, if not on index.php page, show bar"

Any ideas?

I hope I explain this well enough...

index.php includes a sidebar:

require_once('./cache/templates/sidebar.php');

Every subsequent page is built uses what's defined in this index.php file, meaning the sidebar.php is a must.

I'm wanting to edit sidebar.php to contain an advert which displays solely on the index page.

At the moment, when I edit sidebar.php, so for instance, to display the letter "B", it will display on the homepage, and every other page like so;

Index Page: http://img.photobucket.com/albums/v684/nilsatis/1stack.jpg

Every other page: http://img.photobucket.com/albums/v684/nilsatis/2stack.jpg

How can I dictate one area of an included file to display on one page but exclude showing on others?

Any assistance would be very appreciated.

[Edit] This is the website in question: www.grandoldteam.com . You can see where I have the text "Test" - this was entered in sidebar.php. I'd like this text (future advert) to feature only on the index page, nowhere else.

[Edit 2] This is the point in which sidebar.php is called in the index.php file;

<p class="page_desc">'.$PAGE['subtitle'].'</p>
' );
            }

            if (isset($url[1])) require_once('./cache/html/'.$url[0].'/'.$url[1].'.php');
            else require_once('./cache/html/'.$url[0].'.php');
        }
    }

    require_once('./cache/templates/sidebar.php');



}


require_once('./cache/templates/footer.php');

And this is the but in which I can edit sidebar.php to display wanted text;

<div class="clear"></div>
test
        </div>
<p>
    </div>
like image 233
Dan Avatar asked Apr 22 '11 13:04

Dan


3 Answers

To do it the way you want, use the $_SERVER superglobal. The script's name is found in more than one place.

if (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false) // index page...

Another option is to have index.php set some variable like $show_ad before including the side bar, and the side bar page can check that variable.

like image 50
Tesserex Avatar answered Oct 29 '22 07:10

Tesserex


I would not recommend to retrieve the name of the caller script, because several pages can have the same name in different folders, and also because you may want to change page names in the future.

Use a global variable or a constant that says which page is the caller.

index.php:

<?php
  $GLOBALS['caller_page'] = 'index';
  require_once('./cache/templates/sidebar.php');
  ...

sidebar.php:

<?php
  ... 
  if ( isset($GLOBALS['caller_page']) && ($GLOBALS['caller_page']=='index') ) {
    ... // special action for the index page 
  }
  ...
like image 31
Skrol29 Avatar answered Oct 29 '22 08:10

Skrol29


Try this instead,

Create a session on the page where you only want to show "foo".

Then do this

if ($_SESSION['valid']) {

//if the session is present, then show

}

else {

//if not,

}

This is not only a better way of going about it as what happens if your filenames get changed? This way it doesn't matter as it is checking against a session, not something that could change :)

like image 1
Version1 Avatar answered Oct 29 '22 07:10

Version1