session技术登陆+我的博客里面有核心解释

2020-10-28 11:40:27 浏览数 (1)

//销毁页面

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
session_start();
//$_SESSION=array();
unset($_SESSION['username']);
?>
</body>
</html>

//判断是否登陆成功或失败页面

代码语言:javascript复制
	<?php
header('content-type:text/html;charset=utf-8');

	session_start();
//获取输入的值
$username=$_POST['username'];
$password=$_POST['password'];
//链接数据库
$link=mysqli_connect('localhost','root','','test44');
//字符集
mysqli_set_charset($link,'utf8');
//安全化
$username=mysqli_real_escape_string($link,$username);
//然后是sql语句
$sql="select id from user2 where username='{$username}' and password='{$password}'";
$result=mysqli_query($link,$sql);
if($result&&mysqli_affected_rows($link)==1)
{
	$_SESSION['username']=$username;
	$_SESSION['islogin']=1;
	exit("<script>
			alert('登录成功');
			location.href='index.php';
		</script>");
}
else
{
	exit("<script>
			alert('登录失败');
			location.href='login.php';
		</script>");
}
?>

//登陆后页面

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
header('content-type:text/html;charset=utf-8');
session_start();
if (empty($_SESSION['islogin'])||($_SESSION['islogin']!=1)) {
	# code...
	exit("<script>
			alert('登录失败');
			location.href='login.php';
		</script>");
}
echo "欢迎您".$_SESSION['username']."<br />";
echo  "<a href='logout.php'>注销</a>";
?>
</body>
</html>

//登陆页面

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<h1>登陆页面</h1>
	<form action="doAction.php" method="post">
		<table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#abcdef">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="username" id=""></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="password" name="password" id="" /></td>
			</tr>
			<tr colspan="2">
				<td><input type="submit" name="islogin" value="登陆"></td>
			</tr>
		</table>
	</form>
</body>
</html>

//退出页面

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
session_start();
$_SESSION=array();
session_destroy();
exit("<script>
			alert('已退出');
			location.href='login.php';
		</script>");
?>
</body>
</html>

哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈

0 人点赞