Php 如何为管理员和用户编写一个登录表单
我正在尝试编写一个代码,它接受两个输入值(用户名和密码),并将它们与数据库中表(名为user)中的值进行比较。现在,如果为用户名插入的值是“admin”,并且密码也是“admin”。我想直接管理员到他的页面,如果用户插入了他的信息,我想直接他到他的页面也。我下面的代码看起来是正确的,但没有得到任何响应。如何解决这个问题 我为html编写了以下代码:Php 如何为管理员和用户编写一个登录表单,php,select,login,html-table,Php,Select,Login,Html Table,我正在尝试编写一个代码,它接受两个输入值(用户名和密码),并将它们与数据库中表(名为user)中的值进行比较。现在,如果为用户名插入的值是“admin”,并且密码也是“admin”。我想直接管理员到他的页面,如果用户插入了他的信息,我想直接他到他的页面也。我下面的代码看起来是正确的,但没有得到任何响应。如何解决这个问题 我为html编写了以下代码: <form name="userLogin" action="LoginCode.php" method="POST" >
<form name="userLogin" action="LoginCode.php" method="POST" >
<h3>Login</h3>
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">Your Name *</label>
</td>
<td valign="top">
<input type="text" name="user_username" maxlength="50" size="30" required>
</td>
</tr>
<tr>
<td valign="top">
<label for="last_name">Password *</label>
</td>
<td valign="top">
<input type="password" name="user_password" maxlength="50" size="30" required>
</td>
<tr>
<td></td>
<td><input type="submit" name="login" value="Login" required>
</td>
</tr>
</table>
</form>
登录
你的名字*
密码*
这是我的LoginCode.php
<?php
include ("../Connections/map_connection.php");
if (isset($_POST["login"])) {
$user_username = $_POST["user_username"];
$user_password = $_POST["user_password"];
/* $user_email=$_POST["user_email"]; */
if ($username = 'admin' and $user_password = 'admin') {
$data = mysql_fetch_array($result);
session_start();
$_SESSION['name'] = $data['user_username'];
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + 400;
header("location: ..Admin/AdminIndex.php");
} else {
$sql = ("select * from user where user_username='$user_username' and user_password= '$user_password' ");
$result = mysql_query($sql);
if (!$result) {
echo "Error" . mysql_error();
} else {
$row = mysql_num_rows($result);
if ($row == 0) {
echo 'Invalid username or password';
} else {
$data = mysql_fetch_array($result);
session_start();
$_SESSION['name'] = $data['user_username'];
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + 400;
header("location: UserIndex.php");
}
}
}
}
?>
检查您的如果状况
if ($username = 'admin' and $user_password = 'admin')
这里您使用的是单个'='即赋值操作,而不是比较,即'='
试试这个:
if ($username == 'admin' && $user_password == 'admin')
:更新:更新:更新:更新:更新:更新:更新
这是什么意思
if ($username == 'admin' && $user_password == 'admin')
{
$data = mysql_fetch_array($result);
....
}
我的观点是没有mysql\u query(),您使用的是mysql\u fetch\u assoc()。我修复了它
<?php
include ("../Connections/map_connection.php");
if (isset($_POST["login"])) {
$user_username= $_POST["user_username"];
$user_password= $_POST["user_password"];
if($user_username=='admin' && $user_password){
$sql= ("select * from admin where admin_username='$user_username' and admin_password= '$user_password' ");
$result = mysql_query($sql);
if(!$result){
echo "Error".mysql_error();
}
else
{
$row= mysql_num_rows($result);
if($row==0) {
echo 'Invalid username or password';
}
else
{
$data= mysql_fetch_array($result);
session_start();
$_SESSION['name'] = $data['admin_username'];
$_SESSION['start']=time();
$_SESSION['expire']= $_SESSION['start'] + 400;
header("location: ../Admin/AdminIndex.php");
}
}
}
else{
$sql= ("select * from user where user_username='$user_username' and user_password= '$user_password' ");
$result = mysql_query($sql);
if(!$result){
echo "Error".mysql_error();
}
else
{
$row= mysql_num_rows($result);
if($row==0) {
echo 'Invalid username or password';
}
else
{
$data= mysql_fetch_array($result);
session_start();
$_SESSION['name'] = $data['user_username'];
$_SESSION['start']=time();
$_SESSION['expire']= $_SESSION['start'] + 400;
header("location: UserIndex.php");
}
}
}
}
?>
此设置有很多错误。将管理员用户名和密码都设置为admin
是其中之一…@MariM另一件事是将密码以明文形式存储在数据库中。请永远不要这样做。此外,由于某些原因,您有时可以互换使用$user\u username
和$username
。此外,您在查询时也有SQL注入的风险。。。哦,天哪。你很容易受到攻击,而且你在任何地方都没有实际运行(或至少显示如何运行)你试图从中的$result
获取的查询。