$ _SERVER ['REQUEST_METHOD']不存在 [英] $_SERVER['REQUEST_METHOD'] does not exist

查看:300
本文介绍了$ _SERVER ['REQUEST_METHOD']不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了WAMP,可以访问localhost并获取phpinfo()输出.

I've just installed WAMP and I can access localhost and get the phpinfo() output.

但是,尽管我可以看到_SERVER ['REQUEST_METHOD']设置为GET,但是我尝试使用以下PHP:

However, although I can see _SERVER['REQUEST_METHOD'] is set to GET, I'm trying to use the following PHP:

if ($_SERVER["REQUEST_METHOD"]=="POST") {
  ...

但是会产生此错误:

PHP注意:未定义的索引:第40行的C:\ ... \ test.php中的REQUEST_METHOD

PHP Notice: Undefined index: REQUEST_METHOD in C:\ ... \test.php on line 40

使用Komodo在第40行停止并检查$ _SERVER-它在数组中根本没有'REQUEST_METHOD'-甚至没有GET.

Using Komodo to stop at line 40 and check $_SERVER - it does not have 'REQUEST_METHOD' in the array at all - not even GET.

有人有什么想法吗?我必须启用POST,REQUEST_METHOD吗?

Anyone have any ideas? Do I have to enable POST, REQUEST_METHOD?

为什么我可以在phpinfo中看到REQUEST_METHOD = GET,而在PHP脚本中却看不到.

Why can I see REQUEST_METHOD=GET in the phpinfo but not in the PHP script.

我也尝试过:

<?php
ob_start();
phpinfo();
$info = ob_get_contents();
ob_end_clean();
?>

我生成了一些phpinfo(使用localhost/?phpinfo = 1在浏览器中查看),但不是全部.为什么不呢?

I generates some of the phpinfo (as viewed in the browser using localhost/?phpinfo=1) but not all of it. Why not?

推荐答案

大多数$ _SERVER指令由Web服务器设置.如果您使用的是WAMP,那将是Apache.您可以检查您的apache配置,以找出未设置此值的原因.

Most $_SERVER directives are set by the web server. If you are using WAMP that would be Apache. You can check your apache config to find out why this value isn't set.

最好在尝试使用值之前测试它们是否存在.

It's better to test for the existence of values before trying to use them.

$value = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;

您甚至可以使用 getenv()方法来缩短此时间.

You can even use the getenv() method to shorten this.

$value = getenv('REQUEST_METHOD');

也没有必要

<?php
ob_start();
phpinfo();
$info = ob_get_contents();
ob_end_clean();
?>

这是您在空白PHP文件中所需的全部内容.

This is all you need in a blank PHP file.

<?php phpinfo();

我会这样写你的例子:

$request_method = strtoupper(getenv('REQUEST_METHOD'));
$http_methods = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS');

if( ! in_array($request_method, $http_methods)
{
    die('invalid request');
}

这篇关于$ _SERVER ['REQUEST_METHOD']不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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