表单部分
<!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">
<link rel="stylesheet" type="text/css" href="css/formStyle.css">
</head>
<body>
<table width="600" border="0" align="center" style="font-family:Verdana,宋体;font-size: 12px;">
<form action="session_user.php" method="post" onsubmit="return chkinput(this)">
<tr>
<td width="160" height="25" align="right"> </td>
<td width="400"></td>
</tr>
<tr>
<td height="25" align="right">用户名:</td>
<td> <input type="text" name="user" size="40" maxlength="80" value="" class="inputcss" />
<span class="STYLE1"> </span></td>
</tr>
<tr>
<td height="25" align="right">密 码: </td>
<td>
<input type="text" name="password" size="40" maxlength="80" value="" class="inputcss" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="提交" class="buttoncss" /> <input type="reset" value="重置" class="buttoncss"></td>
</tr>
</form>
</table>
</body>
</html>
收集表单数据并注册session变量
如果某人登录成功了,我们就可以注册一个session变量,这个变量也可以包含用户的ID。
<?php
// session开始
session_start();
// 从表单获取数据
$user = $_POST['user'];
$password = $_POST['password'];
if ( isset($user) && isset($password) )
{
// 注册一个session变量
$_SESSION['user'] = $user;
echo $_SESSION['user'];
}
?>
<a href="session_check.php">check</a>
在另外一个页面中访问session变量
要在别的页面中访问已注册的session变量,我们要做的第一件事就是调用session_start()函数,如果session变量已经创建的话,这个函数将载入这些session变量。
<?php
session_start();
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
if ( isset($_SESSION['user']) )
{
echo "你以".$_SESSION['user']."的身份登录了。";
}
?>