Home >Backend Development >PHP Tutorial >How Can I Access $_GET Variables When Running PHP Scripts from the Linux Command Line?

How Can I Access $_GET Variables When Running PHP Scripts from the Linux Command Line?

Barbara Streisand
Barbara StreisandOriginal
2024-11-29 15:17:15998browse

How Can I Access $_GET Variables When Running PHP Scripts from the Linux Command Line?

Accessing $_GET Variables from Linux Command Prompt

While accessing PHP scripts through the web browser involves appending GET variables to the URL, executing the same through the Linux command prompt can be trickier. The usual approach of using php -e index.php alone does not allow for GET variable passing.

Solution: Using php-cgi

Instead of using the basic php command, you can utilize the php-cgi binary. By passing the arguments on the command line, you can effectively simulate GET variable behavior:

php-cgi -f index.php left=1058 right=1067>

This example will populate the $_GET array with the following values:

Array
(
    [left] => 1058
    [right] => 1067
    [class] => A
    [language] => English
)

Setting Environment Variables

In addition to GET variables, you may also need to set environment variables that would normally be set by the web server. This can be achieved using the following syntax:

REQUEST_URI='/index.php' SCRIPT_NAME='/index.php' php-cgi -f index.php left=1058 right=1067>

By employing these techniques, you can effectively execute PHP scripts and access $_GET variables from the Linux command prompt, offering greater flexibility for testing and debugging.

The above is the detailed content of How Can I Access $_GET Variables When Running PHP Scripts from the Linux Command Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn