PHP Authentication API I created small simple PHP Authentication API. I have a couple of scripts that I use for session, authentication and registration. Since I'm not an experienced backend and PHP developer, I wanted someone more experienced to review my scripts and tell me what I did wrong and what I can improve.

I did not use any framework; this is plain PHP.

User registration:

&lt;?php
require_once '../dbConnect.php';

$object = json_decode(file_get_contents("php://input"), true);

if (isset($object['email']) &amp;&amp; isset($object['password']) &amp;&amp; isset($object['firstName']) &amp;&amp; isset($object['lastName'])) {
    $email = $object['email'];

    $validationQuery="select * from members where email='$email'";
    $result = $mysqli-&gt;query($validationQuery) or die($mysqli-&gt;error.__LINE__);
    $member = mysqli_fetch_assoc($result);

    if($member) {
        $message = array('message' =&gt; 'Member with provided email address already exist, please use other email.');
        http_response_code(406);
        echo json_encode($message);
    } else {
        session_start();
        $firstName = $object['firstName'];
        $lastName = $object['lastName'];
        $password = password_hash($object['password'], PASSWORD_DEFAULT);

        $registrationQuery = "INSERT INTO members 
                (firstName, lastName, email, password)
                VALUES 
                ('$firstName', '$lastName', '$email', '$password')";

        if ($mysqli-&gt;query($registrationQuery) === TRUE) {
            $message = array(
                'message' =&gt; 'Registration Successful, you can use your credentials to log in.',
                'memberId' =&gt; mysqli_insert_id($mysqli));
            $_SESSION["id"] = $message['memberId'];
            echo json_encode($message);
        }
    }

    $mysqli-&gt;close();
} else {
    http_response_code(400);
}
?&gt;


Getting authenticated member from session:

&lt;?php
require_once '../dbConnect.php';
session_start();

$object = json_decode(file_get_contents("php://input"), true);

if (isset($object['email']) &amp;&amp; isset($object['password'])) {

    $email = $object['email'];
    $password = $object['password'];
    $query="select * from members where email='$email'";
    $result = $mysqli-&gt;query($query) or die($mysqli-&gt;error.__LINE__);
    $member = mysqli_fetch_assoc($result);

    if($member) {
        if (password_verify($object['password'], $member['password'])) {
            $message = array('message' =&gt; 'Authentication Successful!');
            $_SESSION["id"] = $member['id'];
            echo json_encode($message);
        } else {
            $message = array('message' =&gt; 'Wrong Credentials, Authentication failed!');
            session_destroy();
            http_response_code(400);
            echo json_encode($message);
        }
    } else {
        session_destroy();
        http_response_code(406);
    }

    $mysqli-&gt;close();
} else {
    session_destroy();
    http_response_code(400);
}
?&gt;


Getting authenticated member from PHP session cookie

&lt;?php
require_once '../dbConnect.php';
session_start();

if (isset($_SESSION["id"])) {
    $memberId = $_SESSION["id"];
    $query="select id, firstName, lastName, email, profileImage from members where id='$memberId'";

    $result = $mysqli-&gt;query($query) or die($mysqli-&gt;error.__LINE__);
    $member = mysqli_fetch_assoc($result);

    echo $json_response = json_encode($member);

    $mysqli-&gt;close();
} else {
    http_response_code(401);
}

?&gt;


Simple logout script:

&lt;?php

session_start();

if (isset($_SESSION["id"])) {
    $message = array('message' =&gt; 'Successful log out!');
    session_destroy();
    echo json_encode($message);
} else {
    echo 'You are not logged in!';
    http_response_code(403);
}

?&gt;
Ehh, let's look at the biggest issue here: the SQL-Injection vulnerability.

$object = json_decode(file_get_contents("php://input"), true);

if (isset($object['email']) &amp;&amp; isset($object['password']) &amp;&amp; isset($object['firstName']) &amp;&amp; isset($object['lastName'])) {
    $email = $object['email'];

    $validationQuery="select * from members where email='$email'";


All I have to do is provide a bad string in that JSON for email and now I can destroy your database easy.

Solution: prepared statements.