What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Help with PHP included files

Pages: (2): « First [ 1 ] 2 » Last »
Help with PHP included files
Author: Message:
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
O.P. Help with PHP included files
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;)

This post was edited on 09-14-2005 at 08:09 PM by Choli.
Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
09-14-2005 08:09 PM
Profile PM Find Quote Report
hmaster
Senior Member
****

Avatar

Posts: 716
Reputation: 24
33 / Male / Flag
Joined: Nov 2004
RE: Help with PHP included files
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:

This post was edited on 09-14-2005 at 08:24 PM by hmaster.
[Image: sig.png]
09-14-2005 08:17 PM
Profile PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
O.P. RE: Help with PHP included files
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?
Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
09-14-2005 08:42 PM
Profile PM Find Quote Report
KeyStorm
Elite Member
*****

Avatar
Inn-sewer-ants-pollie-sea

Posts: 2156
Reputation: 45
38 / Male / –
Joined: Jan 2003
RE: Help with PHP included files
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.
09-14-2005 08:45 PM
Profile E-Mail PM Web Find Quote Report
hmaster
Senior Member
****

Avatar

Posts: 716
Reputation: 24
33 / Male / Flag
Joined: Nov 2004
RE: Help with PHP included files
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

This post was edited on 09-14-2005 at 08:51 PM by hmaster.
[Image: sig.png]
09-14-2005 08:48 PM
Profile PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
O.P. RE: Help with PHP included files
thanks you, guys... I tihnk KS' solution will do the work ;)
Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
09-14-2005 09:29 PM
Profile PM Find Quote Report
surfichris
Former Admin
*****

Avatar

Posts: 2365
Reputation: 81
Joined: Mar 2002
RE: Help with PHP included files
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!

This post was edited on 09-14-2005 at 10:28 PM by surfichris.
09-14-2005 10:27 PM
Profile PM Find Quote Report
KeyStorm
Elite Member
*****

Avatar
Inn-sewer-ants-pollie-sea

Posts: 2156
Reputation: 45
38 / Male / –
Joined: Jan 2003
RE: Help with PHP included files
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.

This post was edited on 09-14-2005 at 10:32 PM by KeyStorm.
09-14-2005 10:29 PM
Profile E-Mail PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
O.P. RE: Help with PHP included files
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.

This post was edited on 09-14-2005 at 10:33 PM by Choli.
Messenger Plus! en espaņol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
09-14-2005 10:32 PM
Profile PM Find Quote Report
surfichris
Former Admin
*****

Avatar

Posts: 2365
Reputation: 81
Joined: Mar 2002
RE: Help with PHP included files
Okay, you could do:

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


That would work too :P

This post was edited on 09-14-2005 at 10:35 PM by surfichris.
09-14-2005 10:34 PM
Profile PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On