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

我创建了一个用户需要登录的网页。我想让用户看到他/她的信息。那是说你好user1并单击此处注销。这是我的代码,我什至看不到注销按钮。

更新:我看到我没有使用该用户名登录的用户名。我用用户名学生进入了网站。但它显示不同的用户名。见下图。它应该写 welcome student 而不是 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");

        }

        }


     }


     ?> 


PIPIONE
浏览 146回答 1
1回答

Cats萌萌

您的代码出错,您的代码无法设置会话,当条件成立时,它会根据您使用的条件将用户重定向到另一个 .php 页面。试试这段代码。<?php session_start(); ?>&nbsp; &nbsp; if (isset($_POST['login_user'])) {&nbsp; &nbsp; &nbsp; &nbsp; $username = mysqli_real_escape_string($db, $_POST['username']);&nbsp; &nbsp; &nbsp; &nbsp; $password = mysqli_real_escape_string($db, $_POST['password']);&nbsp; &nbsp; &nbsp; &nbsp; if (empty($username)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "Username is required");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (empty($password)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "Password is required");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (count($errors) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $password = md5($password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results = mysqli_query($db, $query);&nbsp; &nbsp; &nbsp; &nbsp; if (mysqli_num_rows($results) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row = mysqli_fetch_assoc($results);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['username'] = $username;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['success'] = "You are now logged in";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if you want to redirect user as per its type, you can use this condition&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if('student' == $row['user_type']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header('Location: ogrenciarayuzu.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } elseif ('department' == $row['user_type']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header('Location: bolumsekreterligiarayuzu.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } elseif ('institute' == $row['user_type']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header('Location: enstituarayuzu.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header('location: index.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "Wrong username/password combination");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP