当前位置:   article > 正文

php实现用户退出,php如何实现用户退出

php退出账号的实现原理

上一节中谈到用户登录之后,将用户名保存到会话变量中。下面谈谈怎么实现用户退出。

// include function files for this application

require_once('bookmark_fns.php');

session_start();

$old_user = $_SESSION['valid_user'];

// store to test if they *were* logged in

unset($_SESSION['valid_user']);

$result_dest = session_destroy();

// start output html

do_html_header('Logging Out');

if (!empty($old_user)) {

if ($result_dest) {

// if they were logged in and are now logged out

echo 'Logged out.
';

do_html_url('login.php', 'Login');

} else {

// they were logged in and could not be logged out

echo 'Could not log you out.
';

}

} else {

// if they weren't logged in but came to this page somehow

echo 'You were not logged in, and so have not been logged out.
';

do_html_url('login.php', 'Login');

}

do_html_footer();

?>

1、从session中读取valid_user字段的内容,保存到$old_user中备用;

2、将valid_user变量销毁,然后session会话destroy掉;

3、判断读取的$old_user是否为空,如果为空,说明用户根本都没有登录,而点击到退出的页面,这个时候抛出来消息,通知用户没有登录;

4、否则,进一步的判断一下session的destroy操作是否成功完成,如果是则退出成功,否则说明没有正常退出。

以上sample中到了php的库函数,unset()销毁指定的变量。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/746746
推荐阅读
相关标签
  

闽ICP备14008679号