php 如果 isset $_SESSION 转到此页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4806098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:26:59  来源:igfitidea点击:

If isset $_SESSION goto this page?

phpsessionisset

提问by eberswine

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 :-(

我的问题是,如果用户登录并再次跌跌撞撞地进入登录页面——不小心——我希望它能够识别出用户已登录并将他们重定向到下一个页面(即 index2.php) ?? 遇到麻烦:-(

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;
        }

回答by alexn

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.

这将检查您的会话中是否有用户,如果有,则重定向到index2.php.

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

您需要'user'根据您的会话密钥进行更改。