PHP登录 - 如果语句执行即使没有匹配

时间:2017-03-05 08:38:43

标签: php login

我是PHP的新手,我创建了一个登录方法。当我提交数据时,即使该值与“if”语句正在执行不匹配

我的代码:

<?php
  // include db configuration
  include("./config.php");

  // converting data to JSON
  $data = json_decode(file_get_contents("php://input"));

  //assigning input to variables
  $user_name = $data->user_name;
  $password  = hash( 'sha256', $data->password );
  $tokenUser  = hash( 'sha256', $data->user_name );

  //checking username and password
  $getUserInfo = $db->query("SELECT user_name FROM user WHERE user_name='$user_name' AND password='$password'");

  //getting user information
  $getUserInfo = $getUserInfo->fetchAll();

  $token;

  if (count($getUserInfo == 1))
  {
      //creating a token for user authentication
      $token = $tokenUser . " | " . uniqid() . uniqid() . uniqid();

      $q = "UPDATE user SET token=:token WHERE user_name=:user_name AND password=:password";

      $query = $db->prepare($q);

      $execute = $query->execute(array(
          ":token" => $token,
          ":user_name" => $user_name,
          ":password" => $password
      ));

      $response = array(
          'user_name' => $user_name,
          'token' => $token,
          'access' => 'Granted'
      );

      echo json_encode($response);
  }
  else
  {
      $error = array(
          'status' => 'error',
          'message' => 'Username or Password is invlid'
      );
      echo json_encode($error);
  }

?>

如何验证这一点,代码是否正确,需要一些澄清

1 个答案:

答案 0 :(得分:2)

这一行:

if (count($getUserInfo == 1))

应如下:

if (count($getUserInfo) == 1)
相关问题