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

时间:2022-08-24 12:20:40

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?

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

2 个解决方案

#1


218  

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 superglobal访问它们:

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_SAPI constant. 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'];
}
?>

#2


15  

$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

从命令行chmod 755 /var/www/ webroot/index.0调用。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

#1


218  

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 superglobal访问它们:

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_SAPI constant. 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'];
}
?>

#2


15  

$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

从命令行chmod 755 /var/www/ webroot/index.0调用。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

相关文章