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

Posted on 2007-07-25
Medium Priority
Last Modified: 2008-02-01
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
2. fsockopen from domain1.com

Is it possible?  
Thank you.  
Question by:mtnr
  • 3

Accepted Solution

cx323 earned 800 total points
ID: 19569212
you should be able to use htaccess to allow only domain1.  

LVL 13

Assisted Solution

MasonWolf earned 1200 total points
ID: 19571884
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)
LVL 13

Expert Comment

ID: 19589359
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.
LVL 13

Assisted Solution

MasonWolf earned 1200 total points
ID: 19593190
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);

Author Comment

ID: 19621166
very good answers, thank u!

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question