Jump to content

[SOLVED] Check if file is being viewed in browser?


LemonInflux

Recommended Posts

No, I mean like this (yay pseudo code :P):

 

if(is_being_viewed_in_browser) {

  do this;

} else {

  do that;

}

 

How do I do this? I was looking at this:

 

if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) {

  do this;

} else {

  do that;

}

 

But surely then if both files are called index.php, this would say they are the same file (even if one is /index.php, and the other is /folder/index.php)?

But surely then if both files are called index.php, this would say they are the same file (even if one is /index.php, and the other is /folder/index.php)?

 

That is correct, but why would you name a file index.php if its sole purpose was to be used as an included file? Name it something more usefull.

I'll explain a little more about my project..

 

I'm developing a site traffic monitor.

 

The root page is located in /sm/meter/index.php

 

If you view this page in your browser, you will get a control panel, statistics, graphs or whatever, etc etc.

If you include this page, the code will monitor the page you've included.

 

So as well as being an include, it is also a web page, and it would probably be convenient to call it index.php.

As well as this, I don't know what the user will be calling the pages they use it on, so I'd like a fool-proof method if possible.

C:/xampp/htdocs/index.php is $_SERVER['SCRIPT_FILENAME'];

C:\xampp\htdocs\index.php is __FILE__;

 

Would this work..

 

<?php

if (__FILE__ == str_replace("/", DIRECTORY_SEPARATOR, $_SERVER['SCRIPT_FILENAME'])) {
  // file caled directly
} else {
  // file included.
}

?>

 

I'm using directory separator because I'm guessing / and \ wouldn't work cross-platform? Or am I wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.