为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
PIPIONE 2022-12-11 09:23:43
我创建了一个用户需要登录的网页。我想让用户看到他/她的信息。那是说你好user1并单击此处注销。这是我的代码,我什至看不到注销按钮。更新:我看到我没有使用该用户名登录的用户名。我用用户名学生进入了网站。但它显示不同的用户名。见下图。它应该写 welcome student 而不是 yasemin登录代码:// LOGIN USERif (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");        }        }     }     ?> 
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

您的代码出错,您的代码无法设置会话,当条件成立时,它会根据您使用的条件将用户重定向到另一个 .php 页面。


试试这段代码。


<?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");

            }

            }


         }


查看完整回答
反对 回复 2022-12-11
  • 1 回答
  • 0 关注
  • 128 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信