Phuc Coi
Phuc Coi

Reputation: 101

Postman (trying to get property of non-object) error

I'm wondering why my code here having this notice error

enter image description here

And here is my code index.php.

<?php
require_once("DB.php");
$db = new DB("localhost", "unleashe_hotel", "root", "");
if ($_SERVER['REQUEST_METHOD'] == "GET") {
        if ($_GET['url'] == "auth") {
        } else if ($_GET['url'] == "users") {
        }
} else if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if ($_GET['url'] == "auth") {
                $postBody = file_get_contents("php://input");
                // header('Content-Type: application/json')
                $postBody = json_decode($postBody);
                $username = $postBody->username;
                $password = $postBody->password;
                if ($db->query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))) {
                        if (password_verify($password, $db->query('SELECT password FROM users WHERE username=:username', array(':username'=>$username))[0]['password'])) {
                                $cstrong = True;
                                $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));
                                $user_id = $db->query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
                                $db->query('INSERT INTO login_tokens VALUES (\'\', :token, :user_id)', array(':token'=>sha1($token), ':user_id'=>$user_id));
                                echo '{ "Token": "'.$token.'" }';
                        } else {
                                http_response_code(401);
                        }
                } else {
                        http_response_code(401);
                }
        }
} else {
        http_response_code(405);
}
?>

Anyone has any suggestion?

Upvotes: 2

Views: 1542

Answers (1)

Satya
Satya

Reputation: 8881

you are trying to use $_GET when your request method is POST here:

else if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if ($_GET['url'] == "auth") {

correct it to

else if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if ($_POST['url'] == "auth") {

Upvotes: 3

Related Questions