Disallow direct access..

Fedlerner

Former Adm & Team Manager
Community Support
Messages
12,934
Reaction score
6
Points
38
Hi!
I want to disallow the access to some files, cause i want to make a header.php, footer.php etc, and i don't want them to be accessed directly...
Which is the PHP code for that?? :nuts:

Thanks,
fedlerner
 

Micro

Retired staff <i> (11-12-2008)</I>
Messages
1,301
Reaction score
0
Points
36
you can use file_get_contents, include() or require() (Perhaps put them in a randomly named folder)...
 

Fedlerner

Former Adm & Team Manager
Community Support
Messages
12,934
Reaction score
6
Points
38
Sorry, i didn't expressed myself well..
What i do is have for example the index.php and the header.php and footer.php
i use the include() function to include the header.php and the footer.php in my index.php file and all my other files.
What i want to know is the PHP code to don't allow users to access the file directly if the tipe mydomain.com/header.php...

In PHP-Nuke for example, they use this code if you want to access a module directly:
Code:
if ( !defined('MODULE_FILE') )
{
	die("You can't access this file directly...");
}

I don't really know too much php. I added this code to my header but as it's only for PHP-Nuke when i open the index.php it also appears "You can't access this file directly..."

Does someone know how to do it?
 

Woolie

Member
Messages
862
Reaction score
0
Points
16
You've just answered yourself in you last post. You can define a constant in the file which you are "including" header.php from. Then in your seperate files, just check to see if that constant has been defined, which is what phpnuke is doing. Place this before the includes in all the files which you are including other files.

PHP:
 define('IN_MY_SITE', true);
Then place this in all the include files.

PHP:
if(!defined('IN_MY_SITE'))
  {
    die("Please don't access this file directly.");
  }

This means that if the constant "IN_MY_SITE" has not been defined: i.e. The file has not been included by another PHP script, it won't allow you to view the rest.
 
Last edited:

jaint

Member
Messages
174
Reaction score
0
Points
16
you could still access it I think, if you accessed it like this (not sure if it'll work) :

header.php?IN_MY_SITE=true&defined=IN_MY_SITE (of course you'd need to know that probably).

I'm trying to something similar but I'm not including the file, so it's a bit more difficult.
 

Fedlerner

Former Adm & Team Manager
Community Support
Messages
12,934
Reaction score
6
Points
38
Oh thanks Woolie!! It works now!!! :D
jaint, i tryed puting header.php?IN_MY_SITE=true&defined=IN_MY_SITE and it doesn't acces it, it appears the message i put "You can't access this file directly..."

Thanks to all!!!
fedlerner
 
Top