如何通过网页将参数传递给 PHP 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9612166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:15:45  来源:igfitidea点击:

How do I pass parameters into a PHP script through a webpage?

phpparameters

提问by Nick

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).

每当网页加载时,我都会调用 PHP 脚本。但是,PHP 脚本需要运行一个参数(我通常在测试脚本时通过命令行传递)。

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

每次在页面加载时运行脚本时,如何传递此参数?

回答by Jason

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:

通过 HTTP 传递参数(通过 Web 访问脚本)时,您需要做的是使用查询字符串并通过 $_GET 超全局变量访问它们:

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

转到http://yourdomain.com/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:

如果您希望脚本运行而不管您从何处调用它(命令行或浏览器),您将需要以下内容:

EDIT: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_SAPIconstant. I've updated the code accordingly:

编辑:正如 Cthulhu 在评论中指出的那样,测试您正在执行的环境的最直接方法是使用PHP_SAPI常量。我已经相应地更新了代码:

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

回答by Ap.Muthu

$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.phpand use

从命令行调用chmod 755 /var/www/webroot/index.php并使用

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

To call from the browser, use

要从浏览器调用,请使用

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