2014-09-03 144 views
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错误发生在注册“}

+0

1.它的$ _GET ['task'],而不是$ _POST ['task']。 2.你传递任务参数,并在代码中使用标签参数。 – 2014-09-03 10:46:29

+0

tag!=任务。你的URL使用任务,而你在代码中使用标签 – 2014-09-03 10:46:41

+0

@KaranPunamiya:对不起,实际上是标签,我错误地写了它的任务。 – 2014-09-03 11:13:41

回答

1

无论何时您通过URL传递参数,您必须使用GET方法来获取这些变量。要检索这样的变量,你必须使用超级全局变量$_GET['tag name']

这是什么POST方法是由JSON解析器类通过您的Android应用程序通过HTTP请求使用。此HTTP请求通过您的Android应用程序中的名称/值对使用POST方法执行必要的任务。

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

希望这会有所帮助:)