用户名在PHP + Mysql中登录后不显示

时间:2013-04-12 10:55:48

标签: php html sql

我想在登录后显示登录用户名的用户名,下面是我的代码我不知道为什么它不起作用...我已经发布了所有3个文件:

第一个文件。 (身份验证将在何处处理)

auth.php

<?php
    //Start session
    session_start();
    //Check whether the session variable SESS_MEMBER_ID is present or not

    if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
        header("location: ../index.php");

        exit();
    }
?>

2nd(login_exec.php - 登录逻辑的工作原理)

<?php
    //Start session
    session_start();

    //Include database connection details
    require_once('connection.php');

    //Array to store validation errors
    $errmsg_arr = array();

    //Validation error flag
    $errflag = false;

    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }
    //Sanitize the POST values
    $username = clean($_POST['username']);
    $password = clean($_POST['password']);

    //Input Validations
    if($username == '') {
        $errmsg_arr[] = 'Username missing';
        $errflag = true;
    }
    if($password == '') {
        $errmsg_arr[] = 'Password missing';
        $errflag = true;
    }

    //If there are input validations, redirect back to the login form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: ../index.php");
        exit();
    }

    //Create query
    $qry="SELECT * FROM member WHERE username='$username' AND password='$password'";
    $result=mysql_query($qry);

    //Check whether the query was successful or not
    if($result) {
        if(mysql_num_rows($result) > 0) {
            //Login Successful
            session_regenerate_id();
            $member = mysql_fetch_assoc($result);
            // $_SESSION['mem_id'] = $id;
            $_SESSION['SESS_MEMBER_ID'] = $member['mem_id'];
            $_SESSION['SESS_FIRST_NAME'] = $member['username'];
            $_SESSION['SESS_LAST_NAME'] = $member['password'];
            $role = $member['role'];  

            switch ($role) {
                case 'user':
                    $redirect = '../t1.php';
                    break;
                case 'administrator':
                    $redirect = '../t2.php';
                    break;
            }
            // session_write_close();
            header('Location: ' . $redirect); 
            exit();
        }else {
            //Login failed
            $errmsg_arr[] = 'user name and password not found';
            $errflag = true;
            if($errflag) {
                $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
                session_write_close();
                header("location: ../index.php");
                exit();
            }
        }
    }else {
        die("Query failed");
    }
?>

第3个代码文件(t2.php):登录后用户将去的地方:

<?php
    require_once('connect/auth.php');
    $_SESSION['mem_id'] = $id;
    mysql_query("SELECT username FROM member WHERE id=".$_SESSION['mem_id']);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>TAILSHIFT MEDIA PORTAL</title>
  <style type="text/css">
    <!--
    .style1 {
    font-size: 36px;
    font-weight: bold;
    }
    -->
  </style>
</head>

<body>
  <div align="right"><a href="index.php"><img src="images/logout.png" /></a></div>
  <p align="center" class="style1">Welcome <u><?php echo $row['username'];  ?></u></p>
  <br /><br /><br />
  <div align=center>
    <h1>Rest Contents goes here...</h1>
  </div>
</body>
</html>

现在我需要在用户登录后访问此第3页时显示用户名。

我需要做些什么改变?

2 个答案:

答案 0 :(得分:1)

编辑:我假设$row['username']在调用echo之前以某种方式包含用户名

你需要替换它:

<?php $row['username'];  ?>

用这个:

<?php echo $row['username'];  ?>

要输出变量的值,需要调用echo函数

答案 1 :(得分:0)

我通过使用以下方法解决了这个问题:

从先前在第二页上创建的$ _SESSION中,我刚刚回显了存储用户名的数组中的位置。

处理登录的页面,在成功登录时设置以下内容:

 $_SESSION['SESS_FIRST_NAME'] = $member['username'];

“欢迎用户”-page然后按以下方式显示用户名:

<?php echo $_SESSION['SESS_FIRST_NAME']; ?>