Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only show if not included?

Tags:

include

php

I have a php script, and i'd like it so that it will only show this certain text if you're viewing the page in your browser, and it's not included by another script..

For example

//foo.php
<?php
   if(!included){
      echo "You can only see this if this is the page you're viewing";
   }
?>

//bar.php
<?php
  include 'foo.php';
?>

Now, When you view "bar.php", you should not see the text.. But if you open foo.php instead, you will.. How would i do this..? If at all possible..

like image 739
Nathan F. Avatar asked Dec 12 '12 19:12

Nathan F.


3 Answers

Not possible per se, but if you are exposing the php page on a website, e.g. example.com/bar.php, you can check $_SERVER['SCRIPT_FILENAME'] if you are using apache.

if (basename(__FILE__) != basename($_SERVER['SCRIPT_FILENAME'])) {
   //this is included
}
like image 65
Explosion Pills Avatar answered Sep 27 '22 22:09

Explosion Pills


In bar.php:

<?php
    $included = true;
    include 'foo.php';
?>

In foo.php:

if(!isset($included)){
      echo "You can only see this if this is the page you're viewing";
}
like image 21
honyovk Avatar answered Sep 27 '22 20:09

honyovk


You should see about array get_included_files(void) http://php.net/manual/en/function.get-included-files.php

It gives you a list of the included files.

like image 24
GrandMarquis Avatar answered Sep 27 '22 21:09

GrandMarquis