php服务器方法发送GET而不是DELETE

时间:2015-10-17 23:09:20

标签: php html rest http http-delete

我正在尝试实现基本用户注册RESTful api。 我有一个html表单registerform.html,deregisterform.html,users.php,register.php和deregister.php,它们都粘贴在下面。

当我在浏览器中访问registerform.html时,我可以填写详细信息,users.php脚本会收到一个POST请求,并回显一个json响应,显示刚刚添加的用户的id号。 我的问题是,当我使用deregister表单时,users.php脚本应该重新发送DELETE请求,但实际上是在获取GET请求。我一直在寻找这个问题的答案,但我找不到解决方案。

registerform.html

<form action="users.php" method="POST">
    Username: <input type="text" name="uname" /><br />
    FirstName: <input type="text" name="fname" /><br />
    Last Name: <input type="text" name="lname" /><br />
    Date of Birth: <input type="date" name="dob" /><br />
    Telephone: <input type="mob" name="tel" /><br />
    Email: <input type="email" name="email1" /><br />
    Confirm Email: <input type="email" name="email2" /><br />
    Password: <input type="password" name="pass1" /><br />
    Confirm Password: <input type="password" name ="pass2" /><br />
    <input type="submit" value="Register" name="sub" />
    
    <br/><A HREF="login.php">Already Registered? Login Here</A><br/>
</form>  

deregisterform.html

<form action="users.php" method="DELETE">
    Username: <input type="text" name="uname" /><br />
    Password: <input type="password" name="pass1" /><br />
    Confirm Password: <input type="password" name ="pass2" /><br />
    <input type="submit" value="Deregister" name="sub" />
</form> 

register.php

<?php
    if(isset($_POST['uname']) &&  isset($_POST['fname']) &&  isset($_POST['lname']) &&  isset($_POST['tel']) &&  isset($_POST['dob']) &&  
isset($_POST['email1']) &&  isset($_POST['email2']) &&  isset($_POST['pass1']) &&  isset($_POST['pass2'])) 
    {
        //take values from http POST and trim whitespace
        $uname = trim($_POST['uname']);
        $fname = trim($_POST['fname']);
        $lname = trim($_POST['lname']);
        $tel = trim($_POST['tel']);
        $dob = trim($_POST['dob']);
        $email1 = trim($_POST['email1']);
        $email2 = trim($_POST['email2']);
        $pass1 = trim($_POST['pass1']);
        $pass2 = trim($_POST['pass2']);
        //validate the data from the form
        if($um->isNameFormatted($uname))
        {
            if(!$um->isUserExists($uname)) //does user already exist with this username?
            {
                if($um->isNameFormatted($fname)) //first name formatted correctly
                {
                    if($um->isNameFormatted($lname)) //last name formatted correctly
                    {
                        if($um->isDOBFormatted($dob))
                        {
                            if($email1 == $email2)
                            {
                                if($pass1 == $pass2)
                                {
                                    if($um->isPasswordClean($pass1))
                                    {
                                        if($um->isTelephoneVerified($tel))
                                        {
                                            //everything is OKAY --- PROCEED WITH ADDING USER
                                            $user = $um->registerUser($uname,$fname,$lname,$dob,$tel,$email1,$pass1);
                                            if(isset($user))///
                                            {
                                                //successful registration
                                                $response["error"] = false;
                                                $response["id"] = $user;
                                                echo json_encode($response);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }?>

deregister.php

<?php
if(isset($_POST['uname']) && isset($_POST['pass1'] && isset($_POST['pass2'])
{
  $uname = $_POST['uname'];
  $pass1 = $_POST['pass1'];
  $pass2 = $_POST['pass2'];
  if($um->isUserExists($uname))
  {
    if($pass1 == $pass2)
    {
      if(true)//$um->isPasswordFor($uname, $pass))
      {
        $um->deregisterUser($uname, $pass1);
        $response["error"] = false;
        $response["text"] = "User removed!";
                echo json_encode($response);
      } 
      else
      {
                $response["error"] = true;
                $response["text"] = "Wrong username and password combination!";
                echo json_encode($response);
      }
    }
    else
    {
      $response["error"] = true;
      $response["text"] = "Passwords don't match!";
      echo json_encode($response);
    }
  }
  else
  {
    $response["error"] = true;
    $response["text"] = "User(".$uname.") not in database!";
    echo json_encode($response);
  }
}?>

users.php

<?php
error_reporting( -1 );
require('userManagement.php');
$um = new UserManagement();
$response = array("error" => FALSE);
//check if user logged in and authenticated
if(true)
{
    echo "user logged in.";
    echo $httpVerb = trim(strtoupper($_SERVER['REQUEST_METHOD']));
    switch($httpVerb)
    {
        case "GET":
            $response["error"] = false;
            $response["httpVerb"] = $httpVerb;
            echo json_encode($response);
            break;
        case "PUT":
            $response["error"] = false;
            $response["httpVerb"] = $httpVerb;
            echo json_encode($response);
            break;
        case "POST":
           include('register.php');     
            break;
        case "DELETE":
            include('deregister.php');
            break;
        default:
            echo "http verb ".$httpVerb." is not supported for this resource.";
            $response["error"] = true;
            $response["httpVerb"] = $httpVerb;
            echo json_encode($response);
            break;
    }
} 
else 
{
    echo "need to login first.";
}?>

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:5)

您不能将DELETE用作表单操作。

specs开始,我们有:

  

methodformmethod内容属性是枚举属性   使用以下关键字和状态:

     
      
  • 关键字get,映射到状态 GET ,表示HTTP GET方法。
  •   
  • 关键字post,映射到状态 POST ,表示HTTP POST方法。
  •   
     

这些属性的默认值无效是GET状态。