php-将用户名另存为变量,然后在其他地方回显

php-将用户名另存为变量,然后在其他地方回显,php,authentication,Php,Authentication,我正在尝试修改论坛的登录表单,以便在登录后,它会显示“欢迎,用户名” 下面是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

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

下面是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“欢迎,+$USERNAME+”;
我尝试过使用$\u SESSION['username'],但我在php中非常无用,所以任何指导都会非常有用,谢谢。

您可以使用Sessions

在登录脚本中:

session_start();

要在何处显示它:

session_start();

echo '<div id="username">Welcome, '. $_SESSION['Username'] .'</div>';
session_start();
欢迎你$_会话['Username'].';

这里有更多示例:

这是C/JS连接语法
'+$USERNAME+'
您想使用dots。用户LoggedIn()做什么?我想它会保存对用户的引用,以便在后续请求中可以使用用户名。尝试
print\r($\u SESSION)
查看它是否在那里的某个地方,可能它在
$\u SESSION['user']['username']
或类似的地方,或者它只是保存了用户ID,您需要从数据库中获取用户(或者修改
Users\u LoggedIn()
方法来存储用户名)。您知道
$user
变量包含什么吗?您应该尝试使用
var\u dump()
print\u r()
查看其内容。也许我们给会话分配了不正确的值。这仍然太“超出我的理解”,但感谢您尝试一下。没问题。这些东西一开始很难掌握,继续练习吧。祝你好运,伙计。
}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>';