以下讨论的是php5版本以上的会话特性:
1.开始一个会话:
session_start();
该函数首先检查是否有一个会话ID存在,如果存在则把已注册的变量载入,以便使用。如果不存在,则创建。
创建的变量保存在超级全局变量$_session数组中.
2.注册一个新的会话变量:
$_session['newvar']='this is new var';
3.使用会话变量
使用前首先必须检查是否已经被注册:
if(isset($_session['new']))
{
$var=$_session['newvar'];
}
4.注销变量和销毁ID
不能销毁整个$_session 数组。只能一个一个变量的销毁。
要一次销毁所有变量:
$_session=array();
清理完所有变量后,再销毁会话ID
session_destory();
简单的会话使用例子:
session.php
<?php
session_start();
$_SESSION['sess_var']='hello world!';
echo 'The content of $_SESSION[/'sess_var/'] is:'
.$_SESSION['sess_var'].'<br />';
?>
<a href="page2.php">Next page</a>
page2.php
<?php
session_start();
$_SESSION['sess_var']='hello world!';
echo 'The content of $_SESSION[/'sess_var/'] is:'
.$_SESSION['sess_var'].'<br />';
unset($_SESSION['sess_var']);
session_destroy();
?>
配置会话控制:
php.ini
选项名 默认值 效果
session.auto_start 0(被禁用) 自动启动会话
session.cache_expire 180 为缓存中的会话页设置当前的时间,精确到分钟
session.cookie_domain none 指定会话cookie中的域
session.cookie_lifetime 0 cookie会话ID将在用户的机器上延续多久。默认 0 表示延续到浏览器关闭
session.cookie_path / 在会话cookie中腰设置的路径
session.name PHPSESSID 会话的名称,在用户系统中用作会话名
session.save_handler files 定义会话数据保存的地方。
session.save_path /tmp 会话数据存储的路径
session.use_cookies 1(允许使用) 配置在客户端使用的cookie的会话
身份验证样例:
authmain.php
<?php
session_start();
if(isset($_POST['userid']) && isset($_POST['password']))
{
$userid=$_POST['userid'];
$password=$_POST['password'];
$db_conn=new mysqli('10.1.0.3','fceiling','sf123456','test');
if (mysqli_connect_errno())
{
echo 'Connect to database failed:'.mysqli_connect_error();
exit;
}
$query='select * from t1'
." where username='$userid' "
." and password=md5('$password')";
$result=$db_conn->query($query);
if($result->num_rows>0)
{
$_SESSION['valid_user']=$userid;
}
$db_conn->close();
}
?>
<html>
<body>
<h1>Home Page</h1>
<?php
if(isset($_SESSION['valid_user']))
{
echo 'You are logged in as :'.$_SESSION['valid_user'].'<br />';
echo '<a href="logout.php">Log out </a><br />';
}
else
{
if(isset($userid))
{
echo 'Could not log you in.<br/>';
}
else
{
echo 'You are not logged in.<br/>';
}
echo '<form method="post" action="authmain.php">' ;
echo '<table>';
echo '<tr><td>User</td>';
echo '<td><input type="text" name="userid"></td></tr>';
echo '<tr><td>Password:</td>';
echo '<td><input type="password" name="password"></td></tr>';
echo '<tr><td colspan="2" align="center">';
echo '<input type="submit" value="Log in"></td></tr>';
echo '</table></form>';
}
?>
<br />
<a href="members_only.php"> Members section</a>
</body>
</html>
members_only.php
<?php
session_start();
echo '<h1>Members only</h1>';
if(isset($_SESSION['valid_user']))
{
echo '<p>You are logged in as '.$_SESSION['valid_user'].'</p>';
echo '<p> Member only content goes here</p>';
}
else
{
echo '<p> You are not logged in.</p>';
echo '<p> only logged in members may see this page.</p>';
}
echo '<a href="authmain.php">Back to main page</a>';
?>
logout.php
<?php
session_start();
$old_user=$_SESSION['valid_user'];
unset($_SESSION['valid_user']);
session_destroy();
?>
<html>
<body>
<h1>Log out</h1>
<?php
if (!empty($old_user))
{
echo 'Logged out.<br/>';
}
else
{
echo 'You were not logged in ,and so have not been log out.';
}
?>
<a href="authmain.php">Back to main</a>
</body>
</html>