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

Desperately hoping someone could help with this. I am new to php. I try to train myself with textbooks myself, and I searched high and low, but to no avail.

Basically I am looking to implement the page "If index.php, show foo , if not on index.php, show bar "

Any ideas?

I hope I explain it quite well ...

index.php includes a sidebar:

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

Each subsequent page is built using what is defined in this index.php file, which means sidebar.php is required.

I want to modify sidebar.php to contain an ad that displays exclusively on the index page.

Currently, when I edit sidebar.php, for example, to display the letter "B", it will be displayed on the main page and on every other page, for example:

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

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

How can I specify one area of ​​the included file to display on one page, but exclude showing on others?

Any help would be greatly appreciated.

[Edit] This is a website: www.grandoldteam.com. You can see where I have the text "Test" - this was introduced in sidebar.php. I would like this text (future advertising) to be displayed only on the index page, nowhere else.

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

 <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 one, but in which I can edit sidebar.php to display the desired text;

 <div class="clear"></div> test </div> <p> </div> 
+4
source share
6 answers

To do this the way you want, use $ _ SERVER superglobal. The script name is in several places.

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

Another option is to set index.php as a variable of type $ show_ad before turning on the sidebar, and the sidebar page can check this variable.

+7
source

Try instead

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

Then do it

 if ($_SESSION['valid']) { //if the session is present, then show } else { //if not, } 

This is not only the best way to do this, but what will happen if your file names are changed? This way it doesn't matter as it checks the session and not something that might change :)

+1
source

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

Use a global variable or constant indicating 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 } ... 
+1
source

How it works:

At the top of sidebar.php add the line:

 print_r($_SERVER); 

This will show you the full contents of the $ _SERVER variable. From this, you should see several candidates that can be used, as well as other things that may be useful to you at some point.

Once you decide what to use, you will need to check to see if it includes the index.php line. A good way to do this is to use:

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

This idiomatic line checks to see if the position of the index.php line in the script name is incorrect, i.e. it is a value.

0
source

Check value

 $_SERVER[ 'REQUEST_URI' ] 

against literal

'/index.php'.

If it is identical. index.php is executed.

0
source

all of these answers are great, but I suggest a different practice. It is not better, but in my feeling it is more comfortable.

when you do this:

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

on index.php, do this instead:

 require_once('./cache/templates/sidebar.php?ad=1'); 

now, on sidebar.php, add this:

 if(isset($_GET['ad'])&& $_GET['ad']=='1') // display ad else //don't display ad 

EDIT:

need a working code, great ...

lets say that your ad is actually an image of the stackoverflow logo:

now on sidebar.php you will have something like:

 <?php .... if(isset($_GET['ad'])&& $_GET['ad']=='1') { ?> <div> <a href="http://stackoverflow.com"> <img src="http://blog.vicompany.nl/wp-content/uploads/2011/04/stackoverflow-logo-250.png" alt="ad"/> </a> </div> <?php } else { } .... ?> 
0
source

All Articles