将username保存为变量,然后在其他地方回显它


save username as variable then echo it elsewhere

我正在尝试修改论坛的登录表单,以便登录后,它会显示"欢迎,用户名。"

下面是signin.php的一大块内容-

    if($errors == false){
    $user = $db->get_row("SELECT * FROM " . TABLES_PREFIX . "users WHERE email = '" . $_POST['username_or_email'] . "'  OR username = '" . $_POST['username_or_email'] . "' ORDER BY id DESC LIMIT 0,1");
    if($user AND ($user->password == encode_password($_POST['password']))){
        if($user->status == 'pending'){
            $layout->AddContentById('alert', $layout->GetContent('alert'));
            $layout->AddContentById('alert_nature', ' alert-danger');
            $layout->AddContentById('alert_heading', '{{ST:error}}!');
            $layout->AddContentById('alert_message', '{{ST:your_account_not_activated}}');
        }elseif($user->status == 'banned'){
            $layout->AddContentById('alert', $layout->GetContent('alert'));
            $layout->AddContentById('alert_nature', ' alert-danger');
            $layout->AddContentById('alert_heading', '{{ST:error}}!');
            $layout->AddContentById('alert_message', '{{ST:your_account_is_banned}}');
        }else{
            Users_LoggedIn($user, isset($_POST['remember_me']));
            // **I'm guessing this is exactly where I should store the username as var**
            header('Location: '.FORUM_URL); // redirects to index.php
            exit;
        }
    }else{
        $layout->AddContentById('alert', $layout->GetContent('alert'));
        $layout->AddContentById('alert_nature', ' alert-danger');
        $layout->AddContentById('alert_heading', '{{ST:error}}!');
        $layout->AddContentById('alert_message', '{{ST:the_info_is_not_correct}}');
    }
}

然后,在index.php中,我想我会添加一行类似于(伪代码)-的内容

echo '<div id="username">Welcome, '+$USERNAME+'</div>';

我试过玩$_SESSION['username'],但我在php中很没用,所以任何指导都会很有帮助,谢谢。

您可以使用会话

在您的登录脚本中:

session_start();

}else{
    Users_LoggedIn($user, isset($_POST['remember_me']));
    $_SESSION['Username'] = $user['username'];
    header('Location: '.FORUM_URL); // redirects to index.php
    exit;
}

你想在哪里显示它:

session_start();
echo '<div id="username">Welcome, '. $_SESSION['Username'] .'</div>';

更多示例如下:http://php.net/manual/en/session.examples.php