Shoutbox

Help with PHP included files - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Help with PHP included files (/showthread.php?tid=50504)

Help with PHP included files by Choli on 09-14-2005 at 08:09 PM

Hi, i need some help with PHP...

as you know, PHP allows you to include files in a similar way as C's #include......

Well, i want to know a way to detect if a file is being included or if it is called directly in the browser...

Example:

I have 2 files: a.php and b.php . In b.php i have:

code:
include 'a.php';


and I want to write some code to produce an error when the user goes to http://my_server/a.php but that everything goes well when the page is loaded through b.php


Thanks;)
RE: Help with PHP included files by hmaster on 09-14-2005 at 08:17 PM

in a.php

code:
<?
include('error.php');
// or insert your own error page in here
?>

in b.php
code:
<?
include('thepageyouwantthemtogoto.php');
?>

*-) Or do you mean when they access a.php from http://my_server.com/a.php its an error but when it is accessed from http://my_server.com/b.php it runs the correct a.php page, if so you might wanna try $_SERVER['HTTP_REFERER'] :undecided:
RE: Help with PHP included files by Choli on 09-14-2005 at 08:42 PM

quote:
Originally posted by hmaster
Or do you mean when they access a.php from http://my_server.com/a.php its an error but when it is accessed from http://my_server.com/b.php it runs the correct a.php page, if so you might wanna try $_SERVER['HTTP_REFERER']
i think it's something like that... a.php contains code needed by b.php, but it should produce an error when accessed alone.

how does that $_SERVER['HTTP_REFERER'] work? is it the url that the user is requesting to apache?
RE: Help with PHP included files by KeyStorm on 09-14-2005 at 08:45 PM

No, you have to compare the constant __FILE__ with $_SERVER['PHP_SELF'].

__FILE__ will always be the physical filename, and PHP_SELF the running filename to which all are included.


RE: Help with PHP included files by hmaster on 09-14-2005 at 08:48 PM

A friend (Zero1) has told me you need to create a session
I have no idea how to work it (im still figuring it out) if you wanna have a crack at it
http://php.net/session_start :)

Oh and he said example 1 should really do what you want to do :D


RE: Help with PHP included files by Choli on 09-14-2005 at 09:29 PM

thanks you, guys... I tihnk KS' solution will do the work ;)


RE: Help with PHP included files by surfichris on 09-14-2005 at 10:27 PM

quote:
Originally posted by hmaster
A friend (Zero1) has told me you need to create a session
I have no idea how to work it (im still figuring it out) if you wanna have a crack at it
http://php.net/session_start

Oh and he said example 1 should really do what you want to do
What kind of dodgy way is that? Why do something like that when it can be accomplised like this:

a.php
code:
<?php
if(!defined("IN_SCRIPT"))
{
    die("You can't call this file directly");
}
... your code here
?>


b.php
code:
<?php
define("IN_SCRIPT", 1);

.. your code here
?>


Easy!
RE: Help with PHP included files by KeyStorm on 09-14-2005 at 10:29 PM

quote:
Originally posted by hmaster
A friend (Zero1) has told me you need to create a session
quote:
Originally posted by hmaster
Oh and he said example 1 should really do what you want to do
I'm quite afraid your friend is kidding you. The example doesn't work as Choli expects, for sure. And sessions don't have anything to do with includes... :-/

quote:
Originally posted by Chris Boulton
Easy!
But a dirty workaround anyway :refuck:
There is also a function that tells you how many includes there have been, then break on zero.. but that's too much of a hassle.
RE: Help with PHP included files by Choli on 09-14-2005 at 10:32 PM

quote:
Originally posted by Chris Boulton
quote:
Originally posted by hmaster
A friend (Zero1) has told me you need to create a session
I have no idea how to work it (im still figuring it out) if you wanna have a crack at it
http://php.net/session_start

Oh and he said example 1 should really do what you want to do
What kind of dodgy way is that? Why do something like that when it can be accomplised like this:

a.php
code:
<?php
if(!defined("IN_SCRIPT"))
{
    die("You can't call this file directly");
}
... your code here
?>


b.php
code:
<?php
define("IN_SCRIPT", 1);

.. your code here
?>


Easy!
I tought on that, but i'd like not to modify the file where the include statment is. (also, because there are many files that include the "a.php" file, and those files may be changed by others than me :P)

Thanks anyway, Chris.
RE: Help with PHP included files by surfichris on 09-14-2005 at 10:34 PM

Okay, you could do:

code:
<?php
if($_SERVER['SCRIPT_FILENAME'] == "a.php")
{
  die("Go away");
}
?>


That would work too :P
RE: Help with PHP included files by KeyStorm on 09-14-2005 at 10:39 PM

code:
<?php
if($_SERVER['SCRIPT_FILENAME'] == __FILE__)
{
  header("HTTP/1.0 401 Unauthorized");
  die("Go away");
}
?>

Is a universal solution to the problem. (Y)
RE: Help with PHP included files by wj on 09-15-2005 at 04:20 PM

That would be a NICE way to protect CSS files.....