gpt4 book ai didi

php - PHP/MySQL登录

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

我正在尝试创建简单的登录表单。
登录.php

<?php

include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{
$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);


$sql="SELECT id FROM login_test WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];

$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;
//echo $_SESSION['login_user'];
header("location: welcome.php");
}
else
{
$error="Your Login Name or Password is invalid";
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional. dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Page</title>

<style type="text/css">
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;

}
label
{
font-weight:bold;

width:100px;
font-size:14px;

}
.box
{
border:#666666 solid 1px;

}
</style>
</head>
<body bgcolor="#FFFFFF">

<div style="font-weight:bold; margin-bottom:10px">Demo Login Details -> Username : <a href="#">test</a> Password : <a href="#">test</a></div>

<div align="center">
<div style="width:300px; border: solid 1px #333333; " align="left">
<div style="background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>


<div style="margin:30px">

<form action="" method="post">
<label>UserName :</label><input type="text" name="username" class="box"/><br /><br />
<label>Password :</label><input type="password" name="password" class="box" /><br/><br />
<input type="submit" value=" Submit "/><br />

</form>
<div style="font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</div>
</div>

</body>
</html>

锁定.php
<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysql_query("SELECT username FROM login_test WHERE username='$user_check' ");

$row=mysql_fetch_array($ses_sql);

$login_session=$row['username'];

if(!isset($login_session))
{
header("Location: login.php");
}
?>

欢迎.php
<?php

include('lock.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional. dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome </title>
</head>

<body>
<h1>Welcome <?php echo $login_session; ?></h1>
<h2>Login succesfull</h2>

<h2><a href="logout.php">Sign Out</a></h2>
</body>
</html>

问题是它一直重定向回login.php,lock.php启动,我不确定是什么错了
当我输入错误的用户名密码时,它会正确显示消息

最佳答案

问题就在这里

if($count==1)
{
session_register("myusername");
^ // this is deprecated
$_SESSION['login_user']=$myusername;
//echo $_SESSION['login_user'];
header("location: welcome.php");
}

不要使用 session_register它已被弃用
直接使用此代码
$_SESSION['login_user']="myusername";

关于php - PHP/MySQL登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14538547/

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