"unopenable" Scripts?
#1
Posted 22 November 2012 - 11:13 AM
I have this question as to how I can make my scripts so that they cannot be opened individually, only called by other scripts?
eg. buddy_list.php is a page that facebook uses to load your friends, but if you attempt to open facebook.com/buddy_list.php it will load the "Page was not found" page
#2
Posted 22 November 2012 - 11:19 AM
Another common and relatively easy thing to do is have your main files define() a constant which you then check for in your other files. Example:
index.php:
<?php
define('PROPER_REQUEST', true);
include('buddy_list.php');
buddy_list.php
<?php
if (!defined('PROPER_REQUEST')) die("Invalid Request.");
//... rest of script
Did I help you out? Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7
#3
Posted 22 November 2012 - 11:22 AM
Or another way could be that PHP can, I belive, be given access to the servers file system, not just the webdir. This meens that you can, in theory, require/include/fopen/file/...etc anywhere that php has the rights to access, even if the http demon doesn't have those rights.
function makePass($word=''){
$dbSalt = '$2a$07$'.substr(hash('whirlpool',$word),0,22);
$dbPass = crypt($word, $dbSalt);
return substr($dbPass,12);
}My SQL/PHP Blog
#4
Posted 22 November 2012 - 11:59 AM
Using htaccess makes "main" pages unable to access scripts as well :/
#5
Posted 22 November 2012 - 12:04 PM
if( basename(__FILE__) === basename($_SERVER['SCRIPT_NAME']) ) {
die('Direct access to this file is not allowed.');
}
Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *
Random Quote:
"
#6
Posted 22 November 2012 - 12:09 PM
Using htaccess makes "main" pages unable to access scripts as well
Not if you are including them using a file system path, which is the normal way. Using a URL to include files takes from 10 to 100 times longer to execute, only includes the content that the file outputs, and means that you won't be able to prevent http requests to them because the http request your main page is making to them must work, therefor a http request from a browser must work as well.
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.
Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.
#7
Posted 22 November 2012 - 12:49 PM
Unless I've overlooked something, for scripts that are not to be directly accessed this should work.
if( basename(__FILE__) === basename($_SERVER['SCRIPT_NAME']) ) { die('Direct access to this file is not allowed.'); }
basename(__FILE__)
this causes an internal server error O.o
Not if you are including them using a file system path, which is the normal way. Using a URL to include files takes from 10 to 100 times longer to execute, only includes the content that the file outputs, and means that you won't be able to prevent http requests to them because the http request your main page is making to them must work, therefor a http request from a browser must work as well.
I'm not quite sure I understand what you mean, I use relative paths ?
Another common and relatively easy thing to do is have your main files define() a constant which you then check for in your other files. Example:
index.php:<?php define('PROPER_REQUEST', true); include('buddy_list.php');
buddy_list.php<?php if (!defined('PROPER_REQUEST')) die("Invalid Request."); //... rest of script
Another thing I thought about is that this will not work out well with ajax
Edited by Manixat, 22 November 2012 - 01:03 PM.
#8
Posted 22 November 2012 - 08:03 PM
#9
Posted 22 November 2012 - 11:13 PM
basename(__FILE__)
this causes an internal server error O.o
That's odd, it works fine for me. What shows up in your error logs?
Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *
Random Quote:
"
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












