0

您好,我尝试在他们登录后显示用户名。

这是我的代码

这是我想展示的页面。索引.php

<?php

require_once 'classes/Membership.php';
$membership = New Membership();

$membership->confirm_Member();


?>


<!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=utf-8" />
<title>Home Page</title>
<link href="css/me.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="top">
</div>

<div id="top-register">
<?
$_SESSION['username']
?>
<a href="login.php?status=loggedout">
Log Out </a> 
</div>
<div id="top-login">
<a href="index.php"> </a>
</div>


<div id="line">
<div id="banner-text">
Testing
</div>
<div id="banner">
</div>
</div>


<center>
<div id="plan"> 
<div id="plan-innder">
<a href="index.php"><img src="images/plan/starter.png" alt="Starter" width="250" height="300" /></a>



<a href="index.php"><img src="images/plan/regular.png" alt="Regular" width="250" height="300" /></a>



<a href="index.php"><img src="images/plan/advanced.png" alt="Advanced" width="250" height="300" /></a>
</div>
</div>


    enter code here

</center>
</body>
</html>

工作会员.php

<?php

require 'Mysql.php';

class Membership {

    function validate_user($un, $pwd) {
        $mysql = New Mysql();
        $ensure_credentials = $mysql->verify_Username_and_Pass($un, md5($pwd));

        if($ensure_credentials) {
            $_SESSION['status'] = 'authorized';
            header("location: index.php");
        } else return "Please enter a correct username and password";

    } 

    function log_User_Out() {
        if(isset($_SESSION['status'])) {
            unset($_SESSION['status']);

            if(isset($_COOKIE[session_name()])) 
                setcookie(session_name(), '', time() - 1000);
                session_destroy();
        }
    }

    function confirm_Member() {
        session_start();
        if($_SESSION['status'] !='authorized') header("location: login.php");
    }

 }

mysql.php

这就是连接到数据库的内容。

require_once 'includes/constants.php';

class Mysql {
    private $conn;

    function __construct() {
        $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
                      die('There was a problem connecting to the database.');
    }

    function verify_Username_and_Pass($un, $pwd) {

        $query = "SELECT *
                FROM users
                WHERE username = ? AND password = ?
                LIMIT 1";

        if($stmt = $this->conn->prepare($query)) {
            $stmt->bind_param('ss', $un, $pwd);
            $stmt->execute();

            if($stmt->fetch()) {
                $stmt->close();
                return true;
            }
        }

    }
}
4

6 回答 6

3

In your index.php write the following code:

<div id="top-register">
<?
echo "Hello" .$_SESSION['username'];
?>
于 2012-05-31T08:14:25.780 回答
1

我想,你之前错过了回声。改变

<?
$_SESSION['username']
?>

<?
echo $_SESSION['username']
?>
于 2012-05-31T08:12:50.920 回答
1

创建一个会话变量,它将保存您从查询中获得的用户名,然后在您的页面中回显该会话变量。

于 2012-05-31T08:07:29.573 回答
0

我为我的用户登录页面使用了相同的 nettuts 教程。这对我来说可以在 index.php 页面上显示登录用户。

在 INDEX.PHP 页面的正文中插入:

<?php
echo "Welcome ", $_SESSION['username'];
?>
于 2013-06-11T18:11:22.480 回答
0

在班级成员资格更改此...

    if($ensure_credentials) {
        $_SESSION['status'] = 'authorized';
        header("location: index.php");
    } else return "Please enter a correct username and password";

对此...

   if($ensure_credentials) {
        $_SESSION['status'] = 'authorized';
        $_SESSION['username'] = $un;
        header("location: index.php");
    } else return "Please enter a correct username and password";
于 2012-05-31T08:10:40.887 回答
0

我认为您错过了在 index.php 页面上开始会话,因此请在页面开头输入此行

session_start();
于 2013-12-11T08:49:33.550 回答