apw Posted July 30, 2012 Share Posted July 30, 2012 Hello How do i use preg_match to keep people from directly accesssing files? I have this so far but it comes up to blank screen with no error: If (preg_match("/test.php/i", $PHP_SELF)) { print "you cannot view this file directly"; die(); } Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/266434-help-with-using-preg_match/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 30, 2012 Share Posted July 30, 2012 Try this - // detect direct access to included/required file if(strtolower(basename($_SERVER["SCRIPT_NAME"])) == strtolower(basename(__FILE__))){ exit('No Direct Access'); } Quote Link to comment https://forums.phpfreaks.com/topic/266434-help-with-using-preg_match/#findComment-1365345 Share on other sites More sharing options...
scootstah Posted July 30, 2012 Share Posted July 30, 2012 Personally, I would put scripts that you don't want accessed directly either above the docroot, or in a directory protected by .htaccess. For the .htaccess, all you need is: order deny,allow deny from all In either case, scripts can still be used by other scripts (include() or require()), but cannot be accessed directly by URL. Quote Link to comment https://forums.phpfreaks.com/topic/266434-help-with-using-preg_match/#findComment-1365346 Share on other sites More sharing options...
apw Posted July 30, 2012 Author Share Posted July 30, 2012 Went word for word with your example and used test.php as my test page and got no errors just a blank white screen Quote Link to comment https://forums.phpfreaks.com/topic/266434-help-with-using-preg_match/#findComment-1365348 Share on other sites More sharing options...
PFMaBiSmAd Posted July 30, 2012 Share Posted July 30, 2012 Cannot really help you without seeing the exact whole file that you tried to run. Quote Link to comment https://forums.phpfreaks.com/topic/266434-help-with-using-preg_match/#findComment-1365385 Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 A completely white page tends to mean a PHP fatal error, and error_reporting turned off. You should be able to find the reason in your HTTP server's (error) log. Also, scootstah suggestion is the best. If that's not possible for you, then you could always check for a defined constant at the top: <?php if (!is_defined ("SEC_CONST") || SEC_CONST !== true) { die ("No access"); } /* Regular code */ Quote Link to comment https://forums.phpfreaks.com/topic/266434-help-with-using-preg_match/#findComment-1365391 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.