简单的session登录演示

表单部分
<!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']."的身份登录了。";
	}
?>

转载于:https://my.oschina.net/kisscucci/blog/15245

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值