doucu5836 2018-04-01 21:39
浏览 18
已采纳

我的登录系统出了什么问题? [重复]

This question already has an answer here:

I'm trying to make a login system in PHP but it isn't working as intended. The code is supposed to generate a random integer and assaign it to a cookie named "token", then it uploads the value of the cookie to a database. If there is no token, it redirects the user to a 404 page, or if the token isn't equal to the token in the database it also redirects the user to a 404 page. But when I login, instead of setting the token in the database to the cookie's value, it sets it to 2147483647. How can I make the PHP code set the token in the database to the cookie's value?

Code:

admin.php:

include_once "connect.php";
if (!empty($_POST['user']) && !empty($_POST['pass'])) {
$user = $_POST['user'];
$pass = $_POST['pass'];
$_user = mysqli_escape_string($conn, $user);
$_pass = mysqli_escape_string($conn, $pass);
$query = "SELECT * FROM supa WHERE user='$_user' AND pass='$_pass'";
$result = mysqli_query($conn, $query);
if (mysqli_fetch_assoc($result) > 0) {
  setcookie("token", random_int(111, 8942));
  $ok = $_COOKIE['token'];
  $conn->query("INSERT INTO token (token) VALUES ('$ok')");
  echo "<script>location.href = \"panel.php\"</script>";
} else {
  echo "Wrong Cridentials";
}
}

panel.php:

include_once 'connect.php';
if (!empty($_COOKIE['token'])) {
  $token = $_COOKIE['token'];
  $query = "SELECT * FROM token WHERE token='$token'";
  $result = mysqli_query($conn, $query);
  if (mysqli_fetch_assoc($result) > 0) {

  } else {
    echo "<script>location.href = \"/\";</script>";
  }
} else {
  echo "<script>location.href = \"/\";</script>";
}
</div>
  • 写回答

2条回答 默认 最新

  • dongyongju9560 2018-04-01 21:42
    关注

    It seems that you have a type problem in your database. 2147483647 is the highest number of a 32 bits integer.

    I guess you have to change your type in your database from int to varchar or something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?