登录后显示用户名(新手)

时间:2014-04-04 22:53:18

标签: php

你好我对php完全陌生,我的问题是如何在登录成功后在他们收到的页面上回显出登录人的用户名?

我的登录系统工作正常,但不知道在哪里写会话等等。

这是我的login2a.php     

$username = $_POST['username'];
$password = $_POST['password'];


$conn = mysqli_connect('localhost', 'root', '', 'assign02');

$username = mysqli_real_escape_string($conn, $username);
$query = "SELECT password, salt
        FROM members
        WHERE username = '$username';";

$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) == 0) // User not found. So, redirect to login_form again.
{
    header('Location: login.html');
}

$userData = mysqli_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );


//check to see if the password is wrong if wrong redirect user to login forma again and if correct redirect to 
if($hash == $userData['password']) 
{
    header('Location: signed_in.php?username = $username ');
    //header('Location: login.html');
}else{ // Redirect to home page after successful login.
    //header('Location: signed_in.php?username=$username');
    header('Location: login.html');
}
?>

这是我想要显示其用户名的页面,这只是网站的某些部分,因为它太大了,我所回应的是完全错误的我知道,但希望有人可以帮我解决这个问题。此页面是signed_in.php

 <div class="layout-978">
            <img id="content_background" src="Images/Background.png" />


                <div class="main_content">
                    <div id="top_sellers_title"> 
                        <div class="col7">

                            <!--username displayed to show logged in-->
                            <?php
                                if (isset($_SESSION['username'])){

                                    echo "<div id=\"welcome_msg\"> $username </div>";
                                }
                            ?>

2 个答案:

答案 0 :(得分:1)

在登录页面中修改此内容,不要忘记使用session_start();在登录页面的最开始。

if($hash == $userData['password']) 
{
    $_SESSION['username'] = $username;
    header('Location: signed_in.php');
    //header('Location: login.html');
}

然后在signed_in.php页面中,要显示用户名,请执行以下操作

<?php
   if(isset($_SESSION['username'])) echo '<div id="welcome_msg">'.$_SESSION['username'].  '</div>';
?>

答案 1 :(得分:0)

在login2a.php和signed_in.php之上使用session_start()。 在您已成功验证用户身份的login2a.php文件中,创建一个名为username的会话变量,并将您传递给查询的用户名分配给该会话变量。这是

的方式
.
.
.

if($hash == $userData['password']) 
{
    $_SESSION['username'] = $username;
    // Continue with your code
}

希望我的回答有帮助