marcbraulio Posted February 29, 2012 Share Posted February 29, 2012 Hello everyone, What is the best method of blocking direct access to certain files like functions, modules, and etc? I was trying the if ( ! defined('BASEPATH')) exit('No direct script access allowed'); method but I feel like there must be a more convenient/better way. Any suggestions are appreciated, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/257997-block-direct-access-to-certain-pages/ Share on other sites More sharing options...
creata.physics Posted February 29, 2012 Share Posted February 29, 2012 Your way is the best way. Quote Link to comment https://forums.phpfreaks.com/topic/257997-block-direct-access-to-certain-pages/#findComment-1322447 Share on other sites More sharing options...
PFMaBiSmAd Posted February 29, 2012 Share Posted February 29, 2012 Put all the auxiliary files into a folder that is either outside (closer to the disk root) your htdocs folder or if you can only put folders inside your htdocs folder, use a .htaccess file to prevent all http requests to the files in that folder. Quote Link to comment https://forums.phpfreaks.com/topic/257997-block-direct-access-to-certain-pages/#findComment-1322452 Share on other sites More sharing options...
marcbraulio Posted March 1, 2012 Author Share Posted March 1, 2012 Put all the auxiliary files into a folder that is either outside (closer to the disk root) your htdocs folder or if you can only put folders inside your htdocs folder, use a .htaccess file to prevent all http requests to the files in that folder. Aside from being easier, this is more secure as well right? Quote Link to comment https://forums.phpfreaks.com/topic/257997-block-direct-access-to-certain-pages/#findComment-1322539 Share on other sites More sharing options...
PFMaBiSmAd Posted March 1, 2012 Share Posted March 1, 2012 The security is the same for either method. However, by preventing the http request for the files in the first place, you will save some server resources if someone does start trying to directly request the file. For the case of putting the conditional test in each file, if the file is requested, the php language engine will be invoked, the entire file will be parsed and tokenized, then execution will start until it reaches and evaluates the if(){} statement. Quote Link to comment https://forums.phpfreaks.com/topic/257997-block-direct-access-to-certain-pages/#findComment-1322577 Share on other sites More sharing options...
marcbraulio Posted March 1, 2012 Author Share Posted March 1, 2012 The security is the same for either method. However, by preventing the http request for the files in the first place, you will save some server resources if someone does start trying to directly request the file. For the case of putting the conditional test in each file, if the file is requested, the php language engine will be invoked, the entire file will be parsed and tokenized, then execution will start until it reaches and evaluates the if(){} statement. Understood, thank you very much for the in-depth explanation. On a side note, this may seem like a bit of an unorthodox request, but seeing that you have an immense amount of posts/rep, do you think you could take a look at my other post regarding "base url" in php? I have been going at it for a few hours and I am breaking my head over this. I am very sorry if this causes any offence, this will be the only time. Quote Link to comment https://forums.phpfreaks.com/topic/257997-block-direct-access-to-certain-pages/#findComment-1322607 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.