If isset $_SESSION goto this page?

…衆ロ難τιáo~ 提交于 2020-01-13 19:25:11

问题


Ok, having trouble here:

I created a login script, so after a person logs in then they will get direted to another page. And also, I have it redirecting them to the login page if they try and access one of those other pages.

My problem is, if a user is logged in and stumbles to the login page again --by accident-- I would like for it to recognize that the user is logged in and redirect them to that next page (which is index2.php) ?? Having troubles :-(

Here is my code so far:

require_once "inc/functions.class.php";
$quickprotect = new functions('inc/ini.php');

if (isset($_SESSION['goAfterLogin'])){
    $goto = $_SESSION['goAfterLogin'];
    unset($_SESSION['goAfterLogin']);
}
else $goto = $quickprotect->settings['DEFAULT_LOGIN_SUCCESS_PAGE'];

if (isset($_POST[username])) {
    if($quickprotect->login($_POST[username], $_POST[password])) header ("Location: $goto");
}

Here is how I store a users session in the functions page

 public function is_logged_in() {
        //Determines if a user is logged in or not. Returns true or false;
            if ($_SESSION['logged_in'] === md5($this->settings[ADMIN_PW])) {
                return true;
            }
            else return false;
        }

回答1:


You don't mention how you store your users in your session, but something like this should do it for you:

if(isset($_SESSION['user']))
{
    header("Location: index2.php");
    exit;
}

This will check if you have a user in your session, and if so, redirect to index2.php.

You need to change 'user' according to your session key.



来源:https://stackoverflow.com/questions/4806098/if-isset-session-goto-this-page

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