duanjing9339 2014-03-23 17:47
浏览 46
已采纳

PHP欢迎屏幕回显用户名

I am building a welcome user page - after my user logs in, I want on each page to echo the name of the user. however I am having difficulty, after looking at various forums I have resulted into posting my code in the hope someone may have an idea where I am going wrong.

I have 2 key pages - Login and the Index page.

Login.php page
    <?php
    session_start();
    require("includes/connect.php");
    ?>

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Login</title>
            <link rel="stylesheet" type="text/css" href="css/login.css">

            <!--Cited Bootstrap: Responsive web design-->
            <link href="css/bootstrap-theme.css" rel="stylesheet" type="text/css">
            <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
        </head>
        <body>
            <div class="container">
                <form class="form-signin" role="form" action="login.php" method="post">
                    <h2 class="form-signin-heading">Please sign in</h2>
                    <input type="text" class="form-control" placeholder="Username" name="username_login" required autofocus>
                    <input class="form-control" type="password"  placeholder="Password" name="user_password" required>
                    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
                    <label class="forgotten"><a href="forgottenpass.php">Forgotten password?</a></label>
                </form>

    <?php

    //if an admin or user session is already in progress then dont let them log in, redirect to 'index.php'
    if (isset($_SESSION['admin']) && ($_SESSION['admin'] == true) || isset($_SESSION['user']) && ($_SESSION['user'] == true)) {
        echo "You are already logged in";     
        //if use not logged in then
     }else
        if ( trim($_POST['username_login']) AND trim($_POST['user_password']))
        {
        //if username and password are entered, blank before user fills form
        $usr = (isset($_POST['username_login'])? $_POST['username_login']:null);
        $pwd = (isset($_POST['user_password'])? $_POST['user_password']:null);

        //$usr = mysqli_escape_string($usr); //Prevent against SQL Injection by avoiding "\" being executed
      //  $pwd = mysqli_escape_string($pwd); //Prevent against SQL Injection by avoiding "\" being executed

        if ($usr && $pwd){  
            $epwd = $pwd;
            $q = "SELECT * FROM users WHERE UName='$usr' LIMIT 1;";
            $resultset = mysqli_query($conn,$q);
            $rowcount = mysqli_num_rows($resultset);

            if ($rowcount==1){ 
                while ($userRow = mysqli_fetch_assoc($resultset)){
                    //Get the DB username and password to compare
                    $dataBaseEmail = $userRow['UName'];
                    $dataBasePass = $userRow['Password'];   
                    $userGroup = $userRow['UserLevelID'];

                }

            mysqli_free_result($resultset);
            unset($q);

            //Compare DB user and pass to those entered
            if ($usr == $dataBaseEmail && $epwd == $dataBasePass){
                //Now that we know they are activated ect, we can create a session based on their privlidges 
                if ($userGroup ==1){ //ADMIN load the console 
                    $_SESSION['admin'] = true;
                    //header("Location: admincreateuser.php");
                    echo '<div class="login-error">You are an admin user <a href="adminindex.php">Admin</a>.</div>';

                    //exit;
                }else{ //Normal User
                    $_SESSION['user'] = true;
                    $_SESSION['user'] = $dataBaseEmail;
                    //header ("Location: index.php");
                    echo '<div class="login-error">You are a standard user <a href="index.php">Standard</a>.</div>';
                    //exit;
                    }  
                }else{//user and pass do not match DB
                    echo '<div class="login-error">Incorrect Password, try again</div>';     
                }
            }else{
                echo '<div class="login-error">Error: There is no such user registered on the system. Please check the username and password entered.</div>';
            } 
        }
    }
    ?>
            </div> <!-- /container -->

      </body>
    </html>

Index.php page:

<?php
session_start();
include "includes/header.php";
include "includes/connect.php";
?>

<div class="jumbotron">
    <h1>Operating system being used:<?php echo $os?></h1>
    <p>Text</p>

<?php echo $os?>
<?php 

echo "Hello" .$_SESSION['user'];
?>

Many thanks in advance.

  • 写回答

1条回答 默认 最新

  • dryift6733 2014-03-23 17:49
    关注

    You have used $_SESSION['user'] for two purposes, you can rename any one of the $_SESSION array

    Try this,

      $_SESSION['user'] = true;
      $_SESSION['userEmail'] = $dataBaseEmail;
    

    instead of

      $_SESSION['user'] = true;
      $_SESSION['user'] = $dataBaseEmail;
    

    And in welcome screen,

    if(isset($_SESSION['user'])){
      echo "Hello" .$_SESSION['userEmail'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。