PHP脚本如何检测它是否已作为脚本或从外壳调用? [英] How can a PHP script detect if it has been invoked as a script or from the shell?

查看:84
本文介绍了PHP脚本如何检测它是否已作为脚本或从外壳调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络服务器上有一个PHP脚本.

I have a PHP script on a webserver.

此文件由外壳程序由另一个程序调用,但它仍可以由Web服务器运行以响应HTTP请求.

This file is invoked via the shell by another program but it could still be run by the webserver in response to an HTTP request.

该脚本如何确定其调用方式?

How can the script determine the way it was invoked?

推荐答案

有很多方法,例如:我检查$_SERVER['HTTP_HOST']是否为空.我认为技术上正确的方法是查看php_sapi_name()是否返回cli

There are lots of ways; I check if $_SERVER['HTTP_HOST'] is empty. I think the technically correct way is to see if php_sapi_name() returns cli

这篇关于PHP脚本如何检测它是否已作为脚本或从外壳调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆