猿问

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

我创建了一个用户需要登录的网页。我希望用户看到他/她的信息。这表示你好user1,然后单击此处注销。这是我的代码,我什至看不到注销按钮。

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


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


温温酱
浏览 100回答 1
1回答

狐的传说

您的代码错误,您的代码无法设置会话,当条件成立时,它会根据您使用的条件将用户重定向到另一个 .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

相关分类

Html5
我要回答