PHP Include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

PHP Include

Post by tecktalkcm0391 »

Is it possible for PHP to tell if it is included and if not display it displays a message?

I can't find anything in google. I really looked this time. :)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

If you wanna find whether the codes in include file is working correctly use require. If the include file is missing or something is wrong with that, then the whole process gets stopped where the error occured. include() produces a warning while require() produces a fatal error. Otherwise, both include() and require() behaves in the same way. http://in2.php.net/manual/en/function.include.php
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Thanks it work!

Question though is there anything to put on an included file so say someone goes to included.php. It would print("How did you find this"); otherwise if the file was included it wouldn't do anything. Or is this not possible? Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

<?php
if (! isset($a_variable_that_is_set_in_the_including_script)) {
     print("How did you find this");
     exit;
}
/ the rest of the include file
But if register_globals is ON this can be circumvented.
(#10850)
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

Just define a test....

Code: Select all

// main script....

/*
* include file test
*/

define ( 'IS_INCLUDE', true );


// place at the top of all include scripts!

/*
* file can only be included
*/

if ( ! defined ( 'IS_INCLUDE' ) ) { exit (); }

pif!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

thanks it works!
Post Reply