Vinay Raut
Vinay Raut

Reputation: 119

isset() always returns false in php

Will anyone please tell me why following code is not working properly? I tried almost all things but doesn't understand what's happening.

Code is:-

<?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";
}
?>

I am using following code for registration from my android application. There is no problem with localhost connection. When I pass any parameter for eg. localhost/my_api/?tag=register or tag = login it always returns Database API which I am echoing at the last.

Function 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;
        }
    }

I got the below error:- {"tag":"register","success":0,"error":1,"error_msg":"JSON Error occured in Registartion"}

Upvotes: 1

Views: 1029

Answers (1)

Akshay Asawa
Akshay Asawa

Reputation: 11

Whenever you pass parameters through the URL you have to use GET method to get these variables. To retrieve such variables you have to use super global variable $_GET['tag name'].

What here is a POST method which is used by the JSON Parser class through your android app through HTTP requests. This HTTP request makes use of POST method through name-value pairs from your android app to do necessary tasks.

Also, there is no use in accessing this URL like this by passing parameters because you have to login/register from your android app.

Hope this helps :)

Upvotes: 1

Related Questions