0

I have created two PHP files for login and API.in the first file I created a function to check the given credentials with the registration table and if matched set the status to 1. and in the Api file I set if that status is set to 1 send login successful. but even when I enter a wrong password it returns login successful. is it something with the query or condition?

***okay I solved with the answers here by changing == s.I wrote this for a react native application. the login was working well before i add those == to API file and now loging doesn't work. but when I try the api with postman it returns the true results. if I entered a wrong password it returns 405 and if right password 200. but the logging in the application doesn't work. it works when I remove the changes.

<?php

function Database()
{
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "chatapp";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}

function login($connection, $phoneNo, $password){

    $newmessag  = "SELECT * FROM registration WHERE phone_number='$phoneNo' AND password ='$password'";
    $result = $connection->query( $newmessag);
    $count = $result->num_rows;


    if($count> 0)
    {
        $status =1;  

    }else

    {
        $status =0;
    }
    return $status;
}

?>

API file

<?php
include('new.php');

header("Content-Type:application/json");

$method = $_SERVER['REQUEST_METHOD'];
$myJson= file_get_contents('php://input');
$myData = json_decode($myJson);



if($method== 'POST')
{
    if(isset($myData))
    {
           $phoneNo = $myData->phone_number;
           $password = $myData->password;

        if(isset($phoneNo) && isset($password))
        {
            $status = login(Database(), $phoneNo, $password);

            if($status == 1)
            {
                deliver_response(200, "login sucess", $phoneNo);

            }else if ($status == 0)
            {
                deliver_response(405, "login failed", $phoneNo);
            }
        }
    }else{
        deliver_response(403, "Forbidden",$myData);
    }
}
else
{
    deliver_response(403, "Forbidden", $myData);
}


function deliver_response($status, $statusMessage, $data){
    header("HTTP/1.1 $status $statusMessage");
    header('Content-Type: application/json');

    $response['status'] = $status;
    $response['status_message'] = $statusMessage;
    $response['data'] = $data;

    $jres = json_encode($response);
    echo $jres;
}

?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
dilanka
  • 13
  • 5

1 Answers1

1

Typo of using = instead of == and == instead of =.

You need to use = to assign a value and use == for comparison.

Replace if($status = 1) with if($status == 1) and else if ($status = 0) with else if ($status == 0).

Also replace $status ==1 with $status = 1 and $status ==0 with $status = 0 in login function for the same reason, since here you need to assign values.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52