$_SERVER['REQUEST_METHOD'] does not exist

怎甘沉沦 提交于 2019-12-02 01:29:23

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;

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

$value = getenv('REQUEST_METHOD');

Also there is no need to do

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

This is all you need in a blank PHP file.

<?php phpinfo();

I would write your example like this:

$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');
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!