判断php运行环境的函数php_sapi_name()
2012-04-12 22:04
357 查看
判断php运行环境的函数php_sapi_name()
我们一般情况下,都是在apache下面运行我们的php程序,当然也有些人是用IIS环境的我们要是想知道我们目前运行的环境是什么的话,那我们可以用函数php_sapi_name()来测试
代码:
view sourceprint?
1.
<?php
2.
echo
php_sapi_name();
在apache环境下面输出的结果是“apache2handler”;
在cgi模式下输出的结果是“cgi-fcgi”
要是在命令行模式下面运行的话,那么输出的结果是:”cli”
依据这个内容我们可以判断当前运行的环境是什么!
那么在命令行下怎么运行呢?
如下:
进入DOS 进入php.exe文件的地址 如我的是:d:/wamp/bin/php/php5.3.3/
然后输入php.exe “文件的绝对路径” 如:>php.exe d:/wamp/www/info.php
既可以了。
view sourceprint?
01.
<?php
02.
if
(
substr
(PHP_SAPI_NAME(),0,3) !==
'cli'
){
03.
die
(
"该程序只能在CLI模式下运行!"
);
04.
}
05.
//print_r(get_defined_constants());
06.
07.
Class Test{
08.
public
$name
=
""
;
09.
public
$value
=
""
;
10.
public
$content
=
""
;
11.
public
$arr
=
array
();
12.
13.
public
function
__construct(){
14.
$this
->top();
15.
}
16.
17.
public
function
top(){
18.
fwrite(STDOUT,
"请输入参数和值:"
);
19.
$this
->content =trim(
fgets
(STDIN));
20.
preg_replace(
'/\s+/'
,
' '
,
$this
->content);
21.
$this
->arr =
explode
(
'->'
,
$this
->content);
22.
$this
->name =
$this
->arr[
'0'
];
23.
$this
->value =
$this
->arr[
'1'
];
24.
}
25.
26.
public
function
ouput(){
27.
echo
"=========================================
28.
| THE NAME | .
$this
->name
29.
=========================================
30.
| THE VALUE | '.
$this
->value;
31.
=========================================\n";
32.
}
33.
34.
}
35.
while
(true){
36.
$t
=
new
Test();
37.
$t
->ouput();
38.
}
转自:http://blog.xhbin.com/archives/702
相关文章推荐
- PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测
- PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测
- PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测
- centos LNMP第一部分环境搭建 LAMP LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/php/{p.conf.default,p.conf} php运行方式SAPI介绍 第二十三节课
- 判断php的运行环境
- 在写PHP脚本的时候,如何判断是否是从CLI环境运行
- PHP basename()、dirname()、pathinfo()函数在linux环境下的注意事项
- win10本地搭建apache+php+mysql运行环境
- windows 7 home basic 上安装iis 和php运行环境
- 在Windows 7下配置php运行环境
- 判断是否为移动端运行环境
- C++中用 GetModuleFileName()函数 获得程序当前的运行目录
- PHP+FastCGI+Nginx配置PHP运行环境
- 在Windows系统下怎么部署PHP网站运行环境?
- C语言模拟php basename() 函数
- apache+php+mysql运行环境
- C++函数不写bool返回值,居然编译运行全部通过,但判断结果就不对了
- 计算php页面运行时间的函数
- PHP判断是否为手持设备的函数
- Linux下Apache+mysql+PHP运行环境搭建教程