简体   繁体   中英

Display username once user has logged in

Hello i am try to display the username after they log in.

here is my code

This is the page i would like to show it. index.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>

The workings membership.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

This is what is connecting to the data base.

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;
            }
        }

    }
}

In your index.php write the following code:

<div id="top-register">
<?
echo "Hello" .$_SESSION['username'];
?>

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

I think, you missed the echo before. Change

<?
$_SESSION['username']
?>

to

<?
echo $_SESSION['username']
?>

In class Membership change this...

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

To this...

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

I used the same nettuts tutorial for my user login page. This is what works for me to show logged in user on index.php page.

INSERT IN BODY OF INDEX.PHP PAGE:

<?php
echo "Welcome ", $_SESSION['username'];
?>

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

session_start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM