9

I have a script scripty.php

Sometimes this script gets called through the browser.

Other times it gets called by another script on the server.

How can I (securely) check within scripty.php to see whether or not it is being called by the server?

hakre
  • 193,403
  • 52
  • 435
  • 836
Bob
  • 93
  • 1
  • 3

5 Answers5

8

in the form of an http URL

The $_SERVER["REMOTE_ADDR"] variable that gives you the IP address of the client who made the request should be 127.0.0.1 when the script is called from the server.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Unless it is called from another script, that is from the browser... I understood it that way. – Rok Kralj Sep 05 '11 at 19:45
  • @Rok if a script on the server is making a `http://` call, that script will be the caller, not the browser. At least that's how I understand what the OP was saying in the comment – Pekka Sep 05 '11 at 19:46
  • I meant script a.php is called from http:// and calls another script b.php. Therefore b.php `script is being called by another script on the server` – Rok Kralj Sep 05 '11 at 19:49
  • Unfortunately it's not suitable for the development on local machine. You'll get `127.0.0.1` either way. – jibiel Feb 26 '13 at 16:45
  • 1
    http://stackoverflow.com/a/7225638/535406 is seems to be a good solution relying only on script _names_ — no additional constants or variables required. – jibiel Feb 26 '13 at 16:53
  • @jibiel that won't tell you whether the request was made by the browser or by a server, though. You could use the HTTP_USER_AGENT for that in your scenario - it's the browser name and it's going to differ when you make a request from server side using curl or fopen – Pekka Feb 26 '13 at 16:55
  • Is it not good to set some session variable when new request come? and cross check when request arises for a particular script. – Amit Kumar Gupta Jun 10 '13 at 13:59
6

you can create a variable before including your script

$by_script = true;
include("new_script.php");

and check it inside

genesis
  • 50,477
  • 20
  • 96
  • 125
2

Just a guess: You want to know, if the script its called trough a browser, or from CLI

var_dump(PHP_SAPI);
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
1

consider a file named test.php, this is the only code contained:

echo str_replace("\\","/",$_SERVER["SCRIPT_FILENAME"]);
echo str_replace("\\","/",__FILE__);

when a user execute* test.php by browser url, this is the output:

"C:/xampp/htdocs/test.php"
"C:/xampp/htdocs/test.php"

otherwise, differ (( in this case another.php was executed by browser url, who include test.php ))

"C:/xampp/htdocs/another.php"
"C:/xampp/htdocs/test.php"
AgelessEssence
  • 6,395
  • 5
  • 33
  • 37
1

On any script that will be calling it define a constant like define("IN_SCRIPT") and within scripty.php you can check for the constant to determine if it's inside another script or not.

e.g.

if(defined("IN_SCRIPT")){
// We must be inside a script right now!
}

or

if(!defined("IN_SCRIPT")){
// We are working alone now
}
Robert
  • 69
  • 8