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

Posted on 2007-07-25
Last Modified: 2008-02-01
Can you restrict access to a php script?
Would like to set it up so:\myscript.php can only be called from:

1. webpage served by
2. fsockopen from

Is it possible?  
Thank you.  
Question by:mtnr
    LVL 4

    Accepted Solution

    you should be able to use htaccess to allow only domain1.
    LVL 13

    Assisted Solution

    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

    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

    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('');

    or open a socket like this:

    $fp = fsockopen('', 80);
    $header = "GET myscript.php HTTP/1.1\r\n";
    $header .= "Host:\r\n";
    $header .= "Accept: */*\r\n";
    fwrite($fp, $header);
    while (!feof($fp)) {
      echo fread($fp, 8192);

    Author Comment

    very good answers, thank u!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now