144

I am calling a PHP script whenever a webpage loads. However, there is a parameter that the PHP script needs to run (which I normally pass through the command line when I am testing the script).

How can I pass this argument every time the script is run when the page loads?

3
  • 1
    Can you post your code, please?
    – SenorAmor
    Mar 8, 2012 at 2:37
  • 1
    so you want to run a command line? or a php script? Mar 8, 2012 at 2:41
  • Okay, thank you guys. I got the answers below: use $GET and pass it as a parameter value in the url itself.
    – Nick
    Mar 8, 2012 at 8:28

2 Answers 2

240

Presumably you're passing the arguments in on the command line as follows:

php /path/to/wwwpublic/path/to/script.php arg1 arg2

... and then accessing them in the script thusly:

<?php
// $argv[0] is '/path/to/wwwpublic/path/to/script.php'
$argument1 = $argv[1];
$argument2 = $argv[2];
?>

What you need to be doing when passing arguments through HTTP (accessing the script over the web) is using the query string and access them through the $_GET superglobal:

Go to http://yourdomain.example/path/to/script.php?argument1=arg1&argument2=arg2

... and access:

<?php
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
?>

If you want the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:

as pointed out by Cthulhu in the comments, the most direct way to test which environment you're executing in is to use the PHP_SAPI constant. I've updated the code accordingly:

<?php
if (PHP_SAPI === 'cli') {
    $argument1 = $argv[1];
    $argument2 = $argv[2];
}
else {
    $argument1 = $_GET['argument1'];
    $argument2 = $_GET['argument2'];
}
?>
9
  • I would use empty($_GET). $_GET is a predefined value, so I'm pretty sure it is always set, but empty if there are no GET parameters set. empty() returns false for empty strings and arrays.
    – Tim S.
    Mar 7, 2013 at 8:15
  • Without actually checking, I do believe that $_GET is in fact not set if the script is called in a command line context. empty() would cause it to look for command line arguments if the script is accessed from the web without a query string, thus throwing more notices.
    – Jason
    Mar 13, 2013 at 0:28
  • $argv[0] isn't actually arg1 in your 1st example, it would be "/path/to/wwwpublic/path/to/script.php". (php.net/manual/en/reserved.variables.argv.php)
    – CasualT
    Jun 3, 2013 at 17:22
  • 3
    I realise i'm late for the party, but the proper way to check is PHP_SAPI === 'cli'.
    – Cthulhu
    Mar 11, 2015 at 8:49
  • 1
    @emanueledelgrande I'm not entirely sure I follow what you're asking, but I'll try. .htaccess is an Apache file - Apache processes incoming requests with the directives in .htaccess and then invokes PHP to build the response to the request. So, if you're in the command line, .htaccess will be completely ignored. If you're accessing a script through the web, and you want to add new querystring arguments after receiving the request, that's typically done with mod_rewrite. If that doesn't sufficiently answer your question, I suggest you post a brand new question.
    – Jason
    Nov 23, 2017 at 16:39
18
$argv[0]; // the script name
$argv[1]; // the first parameter
$argv[2]; // the second parameter

If you want to all the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:

<?php
if ($_GET) {
    $argument1 = $_GET['argument1'];
    $argument2 = $_GET['argument2'];
} else {
    $argument1 = $argv[1];
    $argument2 = $argv[2];
}
?>

To call from command line chmod 755 /var/www/webroot/index.php and use

/usr/bin/php /var/www/webroot/index.php arg1 arg2

To call from the browser, use

http://www.mydomain.example/index.php?argument1=arg1&argument2=arg2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.