Display username once user has logged in

徘徊边缘 提交于 2019-12-12 00:47:48

问题


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

    }
}

回答1:


In your index.php write the following code:

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



回答2:


Create a session variable which will save the username you got from the query then echo that session variable in your page.




回答3:


I think, you missed the echo before. Change

<?
$_SESSION['username']
?>

to

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



回答4:


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



回答5:


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'];
?>



回答6:


i think you missed to start session on your index.php page so enter this line at the starting of your page

session_start();


来源:https://stackoverflow.com/questions/10829580/display-username-once-user-has-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!