5

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>
Dan
  • 115
  • 1
  • 1
  • 6
  • 2
    If I have understood it correctly you can do this if $_SERVER['PHP_SELF'] == 'index.php') dothis else do that. Pseudo-code – Jatin Dhoot Apr 22 '11 at 13:22
  • By the way for novice use this link would be of great worth http://php.net/manual/en/reserved.variables.server.php – Jatin Dhoot Apr 22 '11 at 13:26
  • 1
    How do you know which page to load? I suspect you use some kind of $_GET variable. You should check if it is empty or something. If you give details on this, we could do more. – kapa Apr 22 '11 at 13:26

6 Answers6

11

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.

Tesserex
  • 17,166
  • 5
  • 66
  • 106
  • 2
    Use `$_SERVER['SCRIPT_NAME']`. Don't use `__FILE__`. If used inside the sidebar.php file, `__FILE__` will return the filename of the sidebar script, not index.php. – Michael Berkowski Apr 22 '11 at 13:26
  • Thanks very much for your help. I don't understand the first solution sorry. How can I set $show_ad? – Dan Apr 22 '11 at 13:57
2

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 
  }
  ...
Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • @Dan: you have to adapt by snippet to your code, but I know too few about it to give more detail. My "special action" probably means your "
    test

    ", nevertheless your snippet seems incomplete. It is the same for my "require_once", your corresponding snippet seems incomplete because of a non structured IF statement.

    – Skrol29 Apr 26 '11 at 12:23
1

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

Version1
  • 657
  • 5
  • 13
  • 1
    Quotes missing in if condition :) – Jatin Dhoot Apr 22 '11 at 13:27
  • This didn't seem to work, though I might have went about it wrong? This is the point in which sidebar.php is called in the index.php file;

    '.$PAGE['subtitle'].'

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

    – Dan Apr 22 '11 at 13:58
  • Edit you answer so I can see it properly bud, I will try to get this sorted for ya. – Version1 Apr 22 '11 at 14:00
  • Thanks - still no joy! If you get a moment it would be massively appreciated if you could detail it in an idiots guide way? – Dan Apr 22 '11 at 14:20
  • Ye no worries, email me the full details at carlv1@live.co.uk and I will try to sort it bud :) – Version1 Apr 22 '11 at 14:27
1

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

....
?>
fingerman
  • 2,440
  • 4
  • 19
  • 24
  • fingerman, that looked perfect and the easiest for me to follow. However, it displayed the actual code. – Dan Apr 22 '11 at 14:08
  • what do you mean "it displayed the actual code" ? do you have php installed and running? did you wrap it with php tags? – fingerman Apr 22 '11 at 14:24
  • Thanks for continued help. These screenshows show the file edits and the end result of them. http://img.photobucket.com/albums/v684/nilsatis/1.jpg & http://img.photobucket.com/albums/v684/nilsatis/2.jpg & http://img.photobucket.com/albums/v684/nilsatis/3.jpg – Dan Apr 22 '11 at 14:32
  • ok, the "//" is a comment sign - it won't do anything... you need to put the code for placing the ad there... http://php.about.com/od/learnphp/qt/php_comments.htm – fingerman Apr 22 '11 at 14:43
  • Yeah, but given the mashup of the entire website from the changes, I assumed commenting would just add some additional copy into the mashup? – Dan Apr 22 '11 at 14:45
  • comment doesn't do anything, It can't do anything be definition - it just tells you what the code does or what the coder wants to say to you. I gave a working example with a stackoverflow logo. – fingerman Apr 22 '11 at 14:52
  • I think you misunderstood me. Usuing your renewed example, you can see the file edits here http://img.photobucket.com/albums/v684/nilsatis/1s.jpg and here http://img.photobucket.com/albums/v684/nilsatis/2s.jpg . These file edits cause the website to go crazy, like so http://img.photobucket.com/albums/v684/nilsatis/3s.jpg - it should look like this www.grandoldteam.com . The same was true prior entering comments as it was when entering comments. Hope that makes sense? Thanks for the help. – Dan Apr 22 '11 at 15:06
  • comments shouldn't be doing this, but maybe a white space or something comes with them and messes the div's layout... just put my code inside the widget div and give it a try. – fingerman Apr 22 '11 at 20:48
  • No joy I'm afraid. Thanks for all the assistance though. – Dan Apr 22 '11 at 21:00
  • this should work... I really can't seem to understand what you are doing wrong. – fingerman Apr 22 '11 at 22:33
  • Anything I can do to assist troubleshooting? I've provided screens of the edits - not sure what else I can provide? – Dan Apr 23 '11 at 18:55
0

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.

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • Thanks a lot Rich. When I entered that line at the top of sidebar, it wasn't then visible on page load? How does it then show the contents? – Dan Apr 22 '11 at 14:06
0

Test the value

$_SERVER[ 'REQUEST_URI' ]

against the literal

'/index.php'.

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

SteAp
  • 11,853
  • 10
  • 53
  • 88