如何在PHP中获取批处理(cmd)变量?

时间:2017-02-04 22:10:02

标签: php batch-file variables cmd

所以这就是我想要做的。我有两个文件:.bat.php

的run.bat:

set password = ok123
php -f data.php

data.php:

<?php
$pass = %password%;
echo '.$password.'

我需要将密码从bat文件传递给php脚本。这样做的最佳方式是什么?

3 个答案:

答案 0 :(得分:0)

set password = ok123

将设置变量&#34;密码&#34;到&#34; ok123&#34;

空格在字符串set语句的两边都很重要。失去空间!

答案 1 :(得分:0)

像这样运行PHP脚本,因为您不想在其中放置密码  环境变量

php data.php paswd123

data.php

<?php
if ( $argc < 2 ) {
    echo 'call this script using the password as a parameter';
    exit;
}

$password = $argv[1];
  

手册http://php.net/manual/en/reserved.variables.argv.php

答案 2 :(得分:0)

如果要设置环境变量,就像使用set命令一样,可以使用$_ENV数组在PHP中访问它们。因此,在您的情况下,密码将存储为$_ENV['password']