restricting access to a php script, so it can only be called by particular domain.

Can you restrict access to a php script?
Would like to set it up so:

domain2.com\myscript.php can only be called from:

1. webpage served by domain1.com
or
2. fsockopen from domain1.com

Is it possible?  
Thank you.  
mtnrAsked:
Who is Participating?
 
cx323Commented:
you should be able to use htaccess to allow only domain1.  

http://www.novia.net/~tomcat/HowTo/domainrestric.html
0
 
MasonWolfCommented:
You can use this code to check that the script is called from a particular IP address. I think that placing this at the head of your script should do what you want:

if($_SERVER['REMOTE_ADDR'] != $valid_ip)
   exit;
0
 
MasonWolfCommented:
mtnr, are you still monitoring this question? Both answers you've received can do the trick. It's just a question of how and where you want to deploy the solution.
0
 
MasonWolfCommented:
By the way, for either of these solutions to work, you must serve up a php webpage on domain2 by using code like this:

echo file_get_contents('http://domain1.com/myscript.php');

or open a socket like this:

$fp = fsockopen('http://domain1.com', 80);
$header = "GET myscript.php HTTP/1.1\r\n";
$header .= "Host: domain1.com\r\n";
$header .= "Accept: */*\r\n";
fwrite($fp, $header);
while (!feof($fp)) {
  echo fread($fp, 8192);
}
0
 
mtnrAuthor Commented:
very good answers, thank u!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.