www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Echo username after login

  1. #1
    Join Date
    Mar 2014
    Posts
    8

    resolved [RESOLVED] Echo username after login

    I'm just a beginner with PHP. I created a PHP login system. Now I want to echo the username to the logged in user on the index.php page.
    Here's the code I have so far. It would be great if someone could suggest a way of doing this. Thanks!

    index.php
    Code:
    <?php
    session_start();
    require_once 'classes/Membership.php';
    $membership = new Membership();
    
    // If the user clicks the "Log Out" link on the index page.
    if(isset($_GET['status']) && $_GET['status'] == 'loggedout') {
    	$membership->log_User_Out();
    }
    
    // Did the user enter a password/username and click submit?
    if($_POST && !empty($_POST['username']) && !empty($_POST['pwd'])) {
    	$response = $membership->validate_User($_POST['username'], $_POST['pwd']);
    }
    														
    ?>
    
    
    <!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>Login</title>
    <link rel="stylesheet" type="text/css" href="css/default.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    </head>
    
    <body>
    <div id="login">
    	<form method="post" action="">
        	<h2>Login <small>enter your credentials</small></h2>
            <p>
            	<label for="username">Username: </label>
                <input type="text" name="username" />
            </p>
            
            <p>
            	<label for="pwd">Password: </label>
                <input type="password" name="pwd" />
            </p>
            
            <p>
            	<input type="submit" id="submit" value="Login" name="submit" />
            </p>
        </form>
        <?php if(isset($response)) echo "<h4 class='alert'>" . $response . "</h4>"; ?>
    </div><!--end login-->
    </body>
    </html>
    index.php (the page that the user is redirected to after logging in)
    Code:
    <?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" />
    <link rel="stylesheet" href="css/default.css" />
    
    <!--[if lt IE 7]>
    <script type="text/javascript" src="js/DD_belatedPNG_0.0.7a-min.js"></script>
    <![endif]-->
    
    
    <title>Untitled Document</title>
    
    
    
    </head>
    
    <body>
    
    <div id="container">
    	<p>
        	You have logged in.
        </p>
        <a href="login.php?status=loggedout">Log Out</a>
    </div><!--end container-->
    
    </body>
    </html>
    membership.php
    Code:
    <?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
    Code:
    <?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;
    			}
    		}
    		
    	}
    }
    Thanks a lot!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    Is the user name saved in $_SESSION. If so, just echo that element, e.g. something like:
    PHP Code:
    <p>Hello, <?php echo $_SESSION['username']; ?>.</p>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2014
    Posts
    8
    Thanks for your reply.
    I tried to echo that element as you said, but the username was not displayed on the page. My only guess to why this is happening is that the username is not saved in $_SESSION. How do I do this? (It could be another problem as well that I'm not aware of).

    Thanks

  4. #4
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    Once the user successfully logs in, you'd have to incorporate $_SESSION['username'] = $variableStoringUsername. Next, at the top of each page, you'd need session_start(); (just noticed you have it in 1 of your pages). In the file that you wish to echo the username, it would just be echo($_SESSION['username']); . To style it and control its position, consider placing that echo within a HTML element.

  5. #5
    Join Date
    Mar 2014
    Posts
    8
    Ok thanks that worked!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles