Difference between executing php from the commandline and from the Http Side

后端 未结 7 1667
余生分开走
余生分开走 2020-12-09 18:32

What is the difference between executing php from command line and from HTTP? Do they use the same executable such as (php.exe or php-cgi.exe ( Apache or IIS ))? Do the r

相关标签:
7条回答
  • 2020-12-09 18:57

    Though this question is very old, I'd like to extend @ Bob Fanger's a bit.

    Running php files from the command line is quite trivial, you only have to keep in mind that there are some differences from running the file on a web server or on a servers command line interface:

    • No Cookies available

    • No $_GET, $_POST, $_SESSION available But you can use $argv to get parameters passed as an argument to the command. The first value is allways the file name.

      For example taken this file:

      <?php
      var_dump($argv);
      ?>
      

      called like this:

      user@ubuntu:$ /usr/bin/php /home/user/file.php foo bar 
      

      would give you this output:

      array(4) {
        [0]=>
        string(8) "file.php"
        [1]=>
        string(3) "foo"
        [2]=>
        string(3) "bar"
      }
      
    • Full file paths from your servers root required You will need to provide the full paths to your files ( e.g. for include(), require(), file_get_contents(), ... ), even though they might be in the same folder.

    • different user/permission settings the files aren't getting executed by www-data user, but by the user you are logged in into your machine. This affects all your file's function calls that effect the machines file system, ( e.g. mkdir(), include(), ... ) so you have to make sure to give appropriate permission to that user.

    0 讨论(0)
  • 2020-12-09 18:58

    For the most part, everything is the same. Big differences are that the super globals might not be populated. Best place to look at these would be on php.net http://php.net/manual/en/features.commandline.php

    0 讨论(0)
  • 2020-12-09 19:00

    When you are executing php from command line your server apache or iis have no role to play. You just use php4 or php5 folder to execute your code. There may be differences with the execution as per the difference in libraries available and php.ini settings in the two folders. When running from apache php.ini within your apache/bin is used. When from command line php.ini from your php5 or php4 folder is used.

    0 讨论(0)
  • 2020-12-09 19:08

    Other than what already said, there will likely be differences in privileges as to what parts of the filesystem are accessible: PHP via webserver is run as the webserver user, PHP from command line is run as yourself.

    0 讨论(0)
  • 2020-12-09 19:13

    No html markup in errors
    This is a php.ini setting(html_errors), but this defaults to off in the cli version.

    Logging to stderr
    Usually errors are logged to the webservers error.log, but in the cli version errors are written to stderr.
    This is also available as a php.ini setting(error_log)

    php.ini
    The php.ini file that is used for the cli version can be a different file. Which can lead to some nasty bugs (curl suddenly not available, etc).

    Different executables
    It's possible to install multiple version of php (php5 alongside php4)
    Use which php to determine which version you're using.

    Everything is shown as text
    var_dump() is readable without a <pre>
    No difference between header('Hello'); and echo('Hello');

    0 讨论(0)
  • 2020-12-09 19:14

    Whether PHP is invoked via a web server module or CLI, the same binary base is used (but can sometimes be configured to use different ini's which can affect the script). It's environment will also be different so environment variables will not be exact.

    PHP is also aware that it's been invoked differently and will tailor it's output to suit that (i.e., phpinfo(); output will be formatted differently when called via CLI).

    0 讨论(0)
提交回复
热议问题