LemonInflux Posted June 21, 2008 Share Posted June 21, 2008 Hello, I was trying to think of a way to check if a file is being viewed in a browser, or if it's being included by a script. How is this done? I have no ideas :S Quote Link to comment Share on other sites More sharing options...
webent Posted June 22, 2008 Share Posted June 22, 2008 Just put in the script a mail function that emails you everytime it is called up... Or make a db insertion... Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted June 22, 2008 Author Share Posted June 22, 2008 No, I mean like this (yay pseudo code ): 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)? Quote Link to comment Share on other sites More sharing options...
trq Posted June 22, 2008 Share Posted June 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted June 22, 2008 Author Share Posted June 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 22, 2008 Share Posted June 22, 2008 Unfortunately, there is no fool proof method of finding out whether a page was called via a call to include or directly via the browser. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted June 22, 2008 Author Share Posted June 22, 2008 Is there a way of using __FILE__ and $_SERVER['SCRIPT_FILENAME'] (and replacing the / with \)? Quote Link to comment Share on other sites More sharing options...
trq Posted June 22, 2008 Share Posted June 22, 2008 Actually, thats an idea. Maybe... if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) { // file caled directly } else { // file included. } Will get you what you want? Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted June 22, 2008 Author Share Posted June 22, 2008 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? Quote Link to comment Share on other sites More sharing options...
trq Posted June 22, 2008 Share Posted June 22, 2008 Just use... <?php if (__FILE__ == str_replace("/", "\\", $_SERVER['SCRIPT_FILENAME'])) { // file caled directly } else { // file included. } ?> Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted June 22, 2008 Author Share Posted June 22, 2008 Great! It seems to work just fine Thanks very much, I'll be sure to keep this for future use as well Topic solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.