简体   繁体   English

用户登录后显示用户名

[英]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 的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 工作原理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 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: 在您的index.php中,编写以下代码:

<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. 我在用户登录页面上使用了相同的nettuts教程。 This is what works for me to show logged in user on index.php page. 这就是我可以在index.php页面上显示登录用户的方法。

INSERT IN BODY OF INDEX.PHP PAGE: 在INDEX.PHP正文中插入页面:

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

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

session_start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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