gpt4 book ai didi

php - 用户名和密码验证PHP MySql

转载 作者:行者123 更新时间:2023-11-29 02:50:24 24 4
gpt4 key购买 nike

我试过这段代码,但似乎不起作用,所以如果有人能帮我解决任何错误,我将不胜感激。
简要概述,我的数据库根目录是“localhost”,数据库名是“ee2800”,密码是“secret”,我需要它来验证用户名和密码。
下面是我所拥有的那些似乎不起作用的东西。

  <?php
$conn = new mysqli("localhost", "ee2800", "secret", "ee2800");

if (mysqli_connect_errno()) {
echo "Connection Failed. Try again." . mysqli_connect_error();
exit();
} /* else {
echo "Connection Successful <br/> ";
} */

// Connect to database server
mysql_connect("localhost", "ee2800", "secret") or die(mysql_error());

$UserName = mysql_real_escape_string($_POST['UserName']);
$Password = mysql_real_escape_string($_POST['Password']);

$sql = "SELECT * FROM users WHERE UserName = '$UserName' AND Password = '$Password' ";
$result = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($result);
if ($numrows > 1) {
else if ($numrows = 0) {
echo "wrong username or password"
} else {
echo "welcome"
}

// Close the database connection
mysql_close();
?>

谢谢你

最佳答案

出于安全考虑,请不要以纯文本格式存储密码。对用户输入和最初存储的值使用相同的加密方法,以进一步防止用户密码泄漏。希望这段代码能更好地为您服务。

<?php

$dbLink = mysqli_connect("localhost", "ee2800", "secret", "ee2800");
if (!$dbLink) {
echo "Connection Failed. Try again." . mysqli_connect_error();
exit();
} else {
echo "Connection Successful <br />";
}

$UserName = mysqli_real_escape_string($_POST['UserName']);
$Password = mysqli_real_escape_string($_POST['Password']); //NOTE FOR THE FUTURE - PLEASE ADD ENCRYPTION TO THIS

$sql = "SELECT * FROM users WHERE UserName = '$UserName' AND Password = '$Password' ";
$result = mysqli_query($dbLink, $sql) or die(mysqli_error());
$numrows = mysqli_num_rows($result, MYSQLI_ASSOC);

if ($numrows == 0) {
echo "wrong username or password";
} else {
echo "welcome";
}

// Close the database connection
mysqli_close();

?>

关于php - 用户名和密码验证PHP MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36628275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com