1
有人會告訴我爲什麼下面的代碼工作不正常? 我嘗試了幾乎所有的東西,但不明白髮生了什麼。isset()在php中總是返回false
代碼爲: -
<?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 = "[email protected]";
$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 = "[email protected]";
$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 = register 或者tag = login 它總是返回數據庫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;
}
}
我得到了以下錯誤: - { 「標籤」: 「註冊」, 「成功」:0, 「錯誤」:1, 「ERROR_MSG」:「JSON錯誤發生在註冊「}
1.它的$ _GET ['task'],而不是$ _POST ['task']。 2.你傳遞任務參數,並在代碼中使用標籤參數。 – 2014-09-03 10:46:29
tag!=任務。你的URL使用任務,而你在代碼中使用標籤 – 2014-09-03 10:46:41
@KaranPunamiya:對不起,實際上是標籤,我錯誤地寫了它的任務。 – 2014-09-03 11:13:41