Dan
Dan

Reputation: 115

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

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>

Upvotes: 5

Views: 17743

Answers (6)

fingerman
fingerman

Reputation: 2470

all of those answers are great, but i provide a different practice. It's not better, but in my feeling it's 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:

you want working code, fine...

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

now on sidebar.php you'll have somthing 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
{
}

....
?>

Upvotes: 1

Version1
Version1

Reputation: 657

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 :)

Upvotes: 1

Skrol29
Skrol29

Reputation: 5597

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 
  }
  ...

Upvotes: 2

SteAp
SteAp

Reputation: 11999

Test the value

$_SERVER[ 'REQUEST_URI' ]

against the literal

'/index.php'.

If it is identical. you index.php is executing.

Upvotes: 0

Rich Bradshaw
Rich Bradshaw

Reputation: 73045

How to work it out:

At the top sidebar.php, add the line:

print_r($_SERVER);

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

Once you've decided on what to use, you will need to check whether it includes the string index.php. A good way to do that would be to use:

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

}

This idiomatic line checks to see whether the position of the string index.php in the script name isn't false, that is, it is a value.

Upvotes: 0

Tesserex
Tesserex

Reputation: 17314

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.

Upvotes: 12

Related Questions