doutuo6689 2018-04-04 08:59
浏览 78
已采纳

在我的登录通知中回显用户名

How to make a notification that echoes the username of the specific user , not the level of the user

For example the user logins , an alert will pop out and says , welcome customer , what i want is to change the customer to the name of the person.

Thank you very much.

this is my code

<?php
    include('conn.php');
    session_start();
    function check_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $username=check_input($_POST['username']);

        if (!preg_match("/^[a-zA-Z0-9_]*$/",$username)) {
            $_SESSION['msg'] = "Username should not contain space and special characters!"; 
            header('location: index.php');
        }
        else{

        $fusername=$username;

        $password = check_input($_POST["password"]);
        $fpassword=md5($password);

        $query=mysqli_query($conn,"select * from `user` where username='$fusername' and password='$fpassword'");

        if(mysqli_num_rows($query)==0){
            $_SESSION['msg'] = "Login Failed, Invalid Input!";
            header('location: index.php');
        }
        else{

            $row=mysqli_fetch_array($query);
            if ($row['access']==1){
                $_SESSION['id']=$row['userid'];
                ?>
                <script>
                    window.alert('Login Success, Welcome Admin!');
                    window.location.href='admin/';
                </script>
                <?php
            }
            elseif ($row['access']==2){
                $_SESSION['id']=$row['userid'];
                ?>
                <script>
                    window.alert('Login Success, Welcome User!');
                    window.location.href='user/';
                </script>
                <?php
            }
            else{
                $_SESSION['id']=$row['userid'];
                ?>
                <script>
                    window.alert('Login Success, Welcome Supplier!');
                    window.location.href='supplier/';
                </script>
                <?php
            }
        }

        }
    }
?>
  • 写回答

2条回答 默认 最新

  • dongre6404 2018-04-04 09:06
    关注

    Let's say you have mentioned username field present in database.

     $row=mysqli_fetch_array($query);
    if ($row['access']==1)
    {
        $_SESSION['id']=$row['userid'];
        $_SESSION['username']=$row['username'];
        ?>
        <script>
            window.alert('Login Success, Welcome Admin!');
            window.location.href='admin/';
        </script>
        <?php
    }
    elseif ($row['access']==2){
        $_SESSION['id']=$row['userid'];
        $_SESSION['username']=$row['username'];
        ?>
        <script>
    
            var username = '<?php echo $_SESSION['username']; ?>';
            var notification_text = "Login Success, Welcome "+username+"!";
            window.alert(notification_text);
            window.location.href='user/';
        </script>
        <?php
    }
    else{
        $_SESSION['id']=$row['userid'];
        $_SESSION['username']=$row['username'];
        ?>
        <script>
            var username = '<?php echo $_SESSION['username']; ?>';
            var notification_text = "Login Success, Welcome "+username+"!";
            window.alert(notification_text);
            window.location.href='supplier/';
        </script>
        <?php
    }
    

    This is how you can access jquery inside PHP and can work on your notifications. P.S - They are not called notification, you can simply call them as an jquery alert.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法