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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.