php判断是否登录

本文介绍了通过PHP实现用户登录验证及会话管理的过程,包括使用Session存储用户信息,实现登录状态判断,并提供了用户登录失败时的跳转逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

index.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
session_start();
if(isset($_SESSION["name"])&&time()>$_SESSION['name'] + 1200){
echo "<hr>";
echo $_SESSION['name'];
echo "<hr>";
echo "welcome back";
}else
echo "<script>alert('先登陆。。。!');location.href='login.html';;</script>";
?>

login.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
session_start();
$conn=mysql_connect("localhost","root","");
if(!$conn)
{
die('Could not connect: ' . mysql_error());
}
$userName=$_POST['userName'];
$userPwd=$_POST['userPwd'];
//$userPwd=md5(trim($_POST['userPwd']));
mysql_select_db(mydb,$conn);
$sql = "SELECT * FROM TABLES where username='".$userName."' and password='".$userPwd."'";
$rows=mysql_fetch_array( mysql_query($sql));
echo $sql;
if(empty($userName)){
echo "用户名NULL";
}else if(empty($userPwd)){
echo "密码NULL";
}else{
if($rows['password']==$userPwd)
{ 

echo "<script>alert('登陆成功。。。!');location.href='index.php';;</script>";
$_SESSION['name']=$userName;
}
else {
echo "<script>alert('用户名或密码错误!!!!');history.back();</script>";
}
}
mysql_close($conn);
?>
login.html

<!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=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form name="login" method="POST" action="login.php" >
<fieldset style="color:blue;;position:absolute;top:20%;left:35%">
<legend>登录</legend>
<p>用户名 <input type="text" name="userName" id="userName" /></p>
<p>密 码 <input type="password" name="userPwd" id="userPwd" /></p>
<p>    
<input type="submit" value="登录" style="color:blue" />   
<input type="reset" value="重置" style="color:blue" />
</fieldset>
</form>
</body>
</html>

转载于:https://my.oschina.net/rong1003/blog/128926

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值