如何使用php在页面一角显示用户登录信息

How to display user log in information at the corner of the page using php

我创建了一个网页,用户需要登录。我想让用户看到his/her信息。上面写着 hello user1 然后点击这里注销。这是我的代码,我什至看不到注销按钮。

更新:我看到了我没有使用该用户名登录的用户名。我用用户名学生进入了网站。但它显示不同的用户名。见下图it supposed to write welcome student not yasemin

登录码:

// LOGIN USER
if (isset($_POST['login_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    if (empty($username)) {
        array_push($errors, "Username is required");


    }
    if (empty($password)) {
        array_push($errors, "Password is required");

    }


    if (count($errors) == 0) {

        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $results = mysqli_query($db, $query);
        $row = mysqli_fetch_assoc($results);


        if('student' == $row['user_type']) {
                 header('Location: ogrenciarayuzu.php');
                 exit();
    } elseif ('department' == $row['user_type']) {
                 header('Location: bolumsekreterligiarayuzu.php');
                 exit();

    } elseif ('institute' == $row['user_type']) {
                 header('Location: enstituarayuzu.php');
                 exit();    

    } 
    if (mysqli_num_rows($results) == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }else {
            array_push($errors, "Wrong username/password combination");
        }
        }

     }

     ?> 

我把这段代码放在我想看登录信息的地方。

  <?php session_start(); ?> // at the beginning of my code

   .
   .
   .
    <?php  
    if (isset($_SESSION['username'])) :
    ?>
    <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
    <p ><a href="index.php" style="color: black;"> <input type="submit" value="Logout" name="logout" 
    class="button"/></a>  </p>

      <?php endif ?>

为了成功注销,我将此代码放入我的 index.php 文件

<?php 
session_start(); 

if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login1.php");
 }
 ?>

请帮助我为什么我仍然看不到。这里有什么问题?

Error in your code, your code failed to set the session, when the condition found true, then it redirect user to another .php page as per the condition which you have used.

试试这个代码。

<?php session_start(); ?>

    if (isset($_POST['login_user'])) {
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $password = mysqli_real_escape_string($db, $_POST['password']);

        if (empty($username)) {
            array_push($errors, "Username is required");


        }
        if (empty($password)) {
            array_push($errors, "Password is required");

        }


        if (count($errors) == 0) {

            $password = md5($password);
            $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
            $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) == 1) {
            $row = mysqli_fetch_assoc($results);
                $_SESSION['username'] = $username;
                $_SESSION['success'] = "You are now logged in";

                //if you want to redirect user as per its type, you can use this condition

                if('student' == $row['user_type']) {
                         header('Location: ogrenciarayuzu.php');
                         exit();
                } elseif ('department' == $row['user_type']) {
                             header('Location: bolumsekreterligiarayuzu.php');
                             exit();

                } elseif ('institute' == $row['user_type']) {
                             header('Location: enstituarayuzu.php');
                             exit();    

                } 
                else
                {
                    header('location: index.php');
                }
            }else {
                array_push($errors, "Wrong username/password combination");
            }
            }

         }