PHP基础-session的使用
程序员文章站
2022-03-14 23:47:26
...
29.php
<html>
<head>
<title>用户登录</title>
</head>
<body>
<form name="form1" action="30.php" method="post">
<table border="0" cellspacing="0" cellpadding="">
<tr>
<td>用户名:</td>
<td><input type="text" name="user_name" id="user_name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="button1" id="button1" value="提 交"></td>
</tr>
</table>
</form>
</body>
</html>
30.php
<?php
/**
* Created by PhpStorm.
*/
$users = array(
array('user_name'=>'admin','password'=>'1','style'=>'css1'),
array('user_name'=>'root','password'=>'2','style'=>'css2'),
array('user_name'=>'abc','password'=>'3','style'=>'css3'),
array('user_name'=>'bat','password'=>'4','style'=>'css4'),
array('user_name'=>'cbd','password'=>'5','style'=>'css5'),
);
if(isset($_GET['action']))
{
if($_GET['action'] == 'logout')
{
session_destroy();
header("location:29.php");
}
}
function login()
{
global $users;
$u = $_POST['user_name'];
$p = $_POST['password'];
foreach ($users as $key => $value)
{
if($value['user_name'] == $u && $value['password'] == $p)
{
session_start();
$_SESSION['user_name'] = $u;
$_SESSION['password'] = $p;
$_SESSION['style'] = $value['style'];
return true;
}
}
return false;
}
if(login())
{
echo "<script>alert('登录成功!');</script>";
echo "<div class='css'>";
echo "你好:$_SESSION[user_name],欢迎光临系统。<br>"."<a href='?action=logout'>注销</a>";
echo "</div>";
}
else
{
echo "<script>alert('登录失败');window.history.go(-1);</script>";
}