isset()总是在php中返回false

时间:2014-09-03 10:42:20

标签: php isset

有人请告诉我为什么以下代码无法正常工作? 我尝试了几乎所有的东西,但不明白发生了什么。

代码是: -

<?php

/**
 PHP API for Login, Register, Changepassword, Resetpassword Requests and for Email Notifications.
 **/
if (isset($_POST['tag']) && $_POST['tag'] != '') {
    // Get tag
    $tag = $_POST['tag'];

    // Include Database handler
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();
    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);

    // check for tag type
    if ($tag == 'login') {
        // Request type is check Login
        $email = $_POST['email'];
        $password = $_POST['password'];

        // check for user
        $user = $db->getUserByEmailAndPassword($email, $password);
        if ($user != false) {
            // user found
            // echo json with success = 1
            $response["success"] = 1;
            $response["user"]["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["username"] = $user["username"];
            $response["user"]["profile_img_path"] = $user["profile_img_path"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["phone"] = $user["phone"];
            $response["user"]["created_at"] = $user["created_at"];

            $response["user"]["created_at"] = $user["created_at"];

            echo json_encode($response);
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = 1;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    } 
  else if ($tag == 'chgpass'){
  $email = $_POST['email'];

  $newpassword = $_POST['newpas'];


  $hash = $db->hashSSHA($newpassword);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"];
  $subject = "Change Password Notification";
         $message = "Hello User,\n\nYour Password is sucessfully changed.\n\nRegards,\nLearn2Crack Team.";
          $from = "contact@learn2crack.com";
          $headers = "From:" . $from;
    if ($db->isUserExisted($email)) {

 $user = $db->forgotPassword($email, $encrypted_password, $salt);
if ($user) {
         $response["success"] = 1;
          mail($email,$subject,$message,$headers);
         echo json_encode($response);
}
else {
$response["error"] = 1;
echo json_encode($response);
}


            // user is already existed - error response


        } 
           else {

            $response["error"] = 2;
            $response["error_msg"] = "User not exist";
             echo json_encode($response);

}
}
else if ($tag == 'forpass'){
$forgotpassword = $_POST['forgotpassword'];

$randomcode = $db->random_string();


$hash = $db->hashSSHA($randomcode);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"];
  $subject = "Password Recovery";
         $message = "Hello User,\n\nYour Password is sucessfully changed. Your new Password is $randomcode . Login with your new Password and change it in the User Panel.\n\nRegards,\nLearn2Crack Team.";
          $from = "contact@learn2crack.com";
          $headers = "From:" . $from;
    if ($db->isUserExisted($forgotpassword)) {

 $user = $db->forgotPassword($forgotpassword, $encrypted_password, $salt);
if ($user) {
         $response["success"] = 1;
          mail($forgotpassword,$subject,$message,$headers);
         echo json_encode($response);
}
else {
$response["error"] = 1;
echo json_encode($response);
}


            // user is already existed - error response


        } 
           else {

            $response["error"] = 2;
            $response["error_msg"] = "User not exist";
             echo json_encode($response);

}

}
else if ($tag == 'register') {
        // Request type is Register new user
        $name = $_POST['name'];
        $username = $_POST['username'];
        $profile_img_path = $_POST['profile_img_path'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $phone = $_POST['phone'];


        // check if user is already existed
                    // store user
            $user = $db->storeUser($name, $username, $profile_img_path, $email, $password, $phone);
            if ($user) {
                // user stored successfully
            $response["user"]["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["username"] = $user["username"];
            $response["user"]["profile_img_path"] = $user["profile_img_path"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["phone"] = $user["phone"];
            $response["user"]["created_at"] = $user["created_at"];

                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = 1;
                $response["error_msg"] = "JSON Error occured in Registartion";
                echo json_encode($response);

        }
    } else {
         $response["error"] = 3;
         $response["error_msg"] = "JSON ERROR";
        echo json_encode($response);
    }
} else {
    echo "Database API";
}
?>

我正在使用以下代码从我的Android应用程序注册。 localhost连接没有问题。 当我传递任何参数时,例如。 本地主机/ my_api /?标签=寄存器 或tag =登录 它总是返回我最后回应的数据库API。

功能storeUser: -

 public function storeUser($name, $username, $profile_img_path, $email, $password, $phone) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
        $result = mysql_query("INSERT INTO users(unique_id, name, username, profile_img_path, email, encrypted_password, salt, phone, created_at) 
        VALUES('$uuid', '$name', '$username', '$profile_img_path', '$email', '$password', '$salt', '$phone', NOW())");
        // check for successful store
        if ($result) {
            // get user details 
            $uid = mysql_insert_id(); // last inserted id
            $result = mysql_query("SELECT * FROM users WHERE uid = $uid");
            // return user details
            return mysql_fetch_array($result);
        } else {
            return false;
        }
    }

我收到以下错误: - {“tag”:“register”,“success”:0,“error”:1,“error_msg”:“Registartion中出现JSON错误”}

1 个答案:

答案 0 :(得分:1)

每当您通过URL传递参数时,您必须使用GET方法来获取这些变量。要检索此类变量,您必须使用超全局变量$_GET['tag name']

这是一个POST方法,JSON Parser类通过您的Android应用程序通过HTTP请求使用它。此HTTP请求通过Android应用程序中的名称 - 值对使用POST方法来执行必要的任务。

此外,通过传递参数来访问此URL是没有用的,因为您必须从Android应用程序登录/注册。

希望这会有所帮助:)