Log-in/log-out user status with LightOpenID

纵然是瞬间 提交于 2019-11-30 20:27:32

问题


I m trying to use LightOpenId for my site to log-in/log-out users. This works fine but my question is "How can I track user log-in/log-out status and take respective actions".

I want user to access my site functionality only when they are logged-in and redirect to login page when user is logged-out.

Thanks in advance.


回答1:


Your question has actually nothing to do with OpenID.

OpenID is an authentication protocol, meaning that it only checks whether the user really is who he claims to be -- in the same sense that asking for a password checks that. It has nothing do to with your user being logged in or out.

In order to keep track of your user's session you need to, well, use sessions. For example, after validation:

<?php
if($openid->validate()) {
    // User has logged in
    $_SESSION['identity'] = $openid->identity;
}
?>

Then when you want to check whether your user is logged in (and who is he):

<?php
if(isset($_SESSION['identity'])) {
    echo 'User is logged in as ' . $_SESSION['identity'];
} else {
    echo 'User isn\'t logged in';
}
?>

And for the sake of completion, when logging out:

<?php
unset($_SESSION['identity']);
session_destroy();
?>

If you don't know how to use sessions, you can find more information in the manual.



来源:https://stackoverflow.com/questions/8560940/log-in-log-out-user-status-with-lightopenid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!