PHP $ _POST空 - 存储在$ _REQUEST中的变量

时间:2016-12-02 15:53:23

标签: php mysql angularjs http

我对php很新,但我有一个" API"在我的Web服务器上设置,以将数据发送/接收到MySQL数据库。

出于某种原因,当我向我的 register.php 文件(注册入口点)发送http post请求时,$_POST变量仍为空。但是,在使用phpconfig()并进行一些挖掘之后,我发现我发送的参数存储在$_REQUEST变量中,所以我相应地更改了我的 register.php 并且它有效。

我的问题是为什么会发生这种情况以及使用$_REQUEST是否错误。

我使用angularjs发送http请求' $ HTTP:

var link = 'http://www.temporaryurl.com/register.php'

$http.post(link, {'name': 'Adam'},{'email': 'awbasham@test.com'},{'password': 'testingPass!'}).then(function(response) {
console.log("Http success!");
console.log(response);
}, function(response) {
console.log("Http failure");
console.log(response);
});

这里是register.php

<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_REQUEST['name']) && isset($_REQUEST['email']) && isset($_REQUEST['password'])) {

    // receiving the post params
    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $password = $_REQUEST['password'];

    // Remove all illegal characters from email
    $email = filter_var($email, FILTER_SANITIZE_EMAIL);

    // Validate e-mail
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        echo("$email is a valid email address");
    } else {
        echo("$email is not a valid email address");
    }

    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        // check if user is already existed with the same email
        if ($db->isUserExisted($email)) {
            // user already existed
            $response["error"] = TRUE;
            $response["error_msg"] = "User already existed with " . $email;
            echo json_encode($response);
        } else {
            // create a new user
            $user = $db->storeUser($name, $email, $password);
            if ($user) {
                // user stored successfully
                $response["error"] = FALSE;
                $response["uid"] = $user["unique_id"];
                $response["user"]["name"] = $user["name"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["created_at"] = $user["created_at"];
                $response["user"]["updated_at"] = $user["updated_at"];
                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = TRUE;
                $response["error_msg"] = "Unknown error occurred in registration!";
                echo json_encode($response);
            }
        }
    } else {
        echo("$email is not a valid email address");
        $response["error"] = TRUE;
        $response["error_msg"] = "Email is invalid!";
        echo json_encode($response);
    }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters (name, email or password) is missing!";
    echo json_encode($response);
}
?>

1 个答案:

答案 0 :(得分:0)

我的问题是双重的。一,我认为$ _POST是空的,因为我通过在我的网络浏览器中键入url来天真地测试register.php文件(我忘了使用chrome扩展来强制POST而不是GET)。

另外,我在我的angularjs文件中调用了$ http:

$http({
    method: 'POST',
    url: link,
    data: data1,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }}).then(function(response) {
      console.log("Http success!");
      console.log(response);
    }, function(response) {
      console.log("Http failure!");
      console.log(response);
    });

我认为有助于将Content-Type定义为&application; x-www-form-urlencoded&#39;并将data1作为

传递
var data1 = "name=adam&email=test@test.com&password=testPass!";