php 使用Mysql数据库用户名和密码登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25847491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Login using Mysql database username and password
提问by user2929717
i'm trying to make my php scripts protected pages, and so when adding a login i want it to check the username and password inputs against the database username and password and if they match it will allow them to view the protected pages until logout. I've been able to set up the protected page & logout based on a dummy login username and password now i want to connect it to the database users. This is my html & php code. Any help would be appreciated :)
我正在尝试使我的 php 脚本受保护页面,因此在添加登录名时,我希望它根据数据库用户名和密码检查用户名和密码输入,如果它们匹配,它将允许他们查看受保护的页面,直到注销。我已经能够根据虚拟登录用户名和密码设置受保护页面并注销,现在我想将其连接到数据库用户。这是我的 html 和 php 代码。任何帮助,将不胜感激 :)
HTML:
HTML:
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="post" action="login.php">
<table border="1" cellpadding="2">
<caption>
Staff Login
</caption>
<tr>
<td><label for="username">Username</label></td>
<td><input type="text" name="username" cols="45" rows="5" tabindex="2"/></td>
</tr>
<tr>
<td><label for="password">Password</label></td>
<td><input type="password" name="password" cols="45" rows="5" tabindex="2"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>
PHP:
PHP:
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
// Check if username and password are correct
if ($_POST["username"] == 'username' && $_POST["password"] == 'password') {
include 'connection.php';
// If correct, we set the session to YES
session_start();
$_SESSION["Login"] = "YES";
echo "You are now logged in.</br></br>";
echo "<a href='list-contacts.php'>Proceed to contacts...</a>";
}
else {
// If not correct, we set the session to NO
session_start();
$_SESSION["Login"] = "NO";
echo "Incorrect username and / or password.</br></br>";
echo "<a href='login.html'>Return to login</a>";
}
?>
</body>
</html>
回答by Asm Hijas
<?php session_start();
include('../db.php');
if(isset($_POST['login_submit'])){
if(!empty($_POST['user_name']) && !empty($_POST['user_password'])){
$get_username=mysqli_real_escape_string($conn,$_POST['user_name']);
$get_password=mysqli_real_escape_string($conn,$_POST['user_password']);
$sql="SELECT * FROM users WHERE user_name= '$get_username' AND user_password = '$get_password' ";
if($result=mysqli_query($conn,$sql)){
if(mysqli_num_rows($result)==1){
header('Location:../admin/index.php');
}else{
header('Location:../index.php?login=wrong');
}
}else{
header('Location:../index.php?login=error');
}
}else{
header('Location:../index.php?login=empty');
}
}else{}
?>
This is a php 5 mysqli_num_row() function to varifiy username and password from db..
这是一个 php 5 mysqli_num_row() 函数,用于从 db 中改变用户名和密码。
回答by Dinesh
always assign session at the top of page, before any output is sent to browser.
you can check the username and password inputs against the database by the simple my sql query
在任何输出发送到浏览器之前,始终在页面顶部分配会话。
您可以通过简单的 my sql 查询检查数据库中的用户名和密码输入
session_start();
$con=mysqli_connect("host","user","password","db_name");
$query=mysqli_query("select from users_table where username='".$_POST['username']."' and password='".$_POST['password']."'");
if(mysqli_num_rows($query)==1) {
//set session data here
//redirect user to where you want
}
回答by shine
Include your connection.php
before your
if ($_POST["username"] == 'username' && $_POST["password"] == 'password')
condition and then run a select query on your db to fetch username and password like "select * from tablename where username='$_POST["username"]' and password='$_POST["password"]'"
and then in your if check that the query result is not empty
connection.php
在您的if ($_POST["username"] == 'username' && $_POST["password"] == 'password')
条件之前
包含您,然后在您的数据库上运行选择查询以获取用户名和密码"select * from tablename where username='$_POST["username"]' and password='$_POST["password"]'"
,然后在您的 if 检查查询结果是否为空
you can also check this link for mysql-php login system:-
您还可以查看此链接以获取 mysql-php 登录系统:-
http://www.sourcecodetuts.com/php/27/how-create-login-page-php-and-mysql-sessionvariable
http://www.sourcecodetuts.com/php/27/how-create-login-page-php-and-mysql-session变量