chicago123 Posted March 3, 2014 Share Posted March 3, 2014 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! login.php <?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) <?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 <?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 <?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! Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/ Share on other sites More sharing options...
ginerjm Posted March 3, 2014 Share Posted March 3, 2014 Why not do the same thing where you echo $response in the login.php in the index.php script.? Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/#findComment-1471287 Share on other sites More sharing options...
chicago123 Posted March 3, 2014 Author Share Posted March 3, 2014 Sorry, I'm just a beginner (high school student) and I'm not sure how to do that. Could you please give an example? Do I just add this line anywhere in the index.php script? (I don't think $response is a global variable). echo $response Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/#findComment-1471289 Share on other sites More sharing options...
ginerjm Posted March 3, 2014 Share Posted March 3, 2014 Yes you can do an echo. As for how to get the $response var to that script I'd have to see how you are getting to it. Show how your pass control to index.php from the login process Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/#findComment-1471291 Share on other sites More sharing options...
chicago123 Posted March 3, 2014 Author Share Posted March 3, 2014 Thanks for your quick reply. The login.php script attempts to validate the user and the membership.php script (I posted it above) creates a session by authorizing the user using mysql.php (also posted above) which matches the username/password with one stored in the database. I basically got the whole thing from a tutorial I watched which explains everything (if I'm not very clear about it). Here it is: http://code.tutsplus.com/articles/how-to-build-a-login-system-for-a-simple-website--net-2853 This is the download link for the code: http://cdn.tutsplus.com/net/uploads/legacy/192_loginSystem/membershipSite.zip I've created a registration system and everything else, so all I need to do now is learn how to echo the username Thanks Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/#findComment-1471300 Share on other sites More sharing options...
ginerjm Posted March 3, 2014 Share Posted March 3, 2014 Apparently you don't want to make the effort to learn here, since I've told you exactly what to do. You haven't taken the time to examine what I described, have you? I'll help those who attempt to help themselves. You are just a copier. Sorry. Good Bye. Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/#findComment-1471317 Share on other sites More sharing options...
Solution Ch0cu3r Posted March 3, 2014 Solution Share Posted March 3, 2014 The code you are using is not complete code, it is only part 1 of a video series (you can only watch the other videos by signing up to their premium service). But to answer your question, in membership.php you need to save the users username to the session when the verify_Username_and_Pass method returns true. $ensure_credentials = $mysql->verify_Username_and_Pass($un, md5($pwd)); if($ensure_credentials) { $_SESSION['status'] = 'authorized'; $_SESSION['username'] = $un; /* save the users username to session */ header("location: index.php"); Now in index.php you should be able to use echo $_SESSION['username']; to display the logged in users username Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/#findComment-1471319 Share on other sites More sharing options...
chicago123 Posted March 3, 2014 Author Share Posted March 3, 2014 Thank you! That worked! I literally JUST started learning PHP yesterday so I'm sorry if you were frustrated by my lack of understanding. Anyways, thanks for your patience. Quote Link to comment https://forums.phpfreaks.com/topic/286666-echo-username-after-login/#findComment-1471341 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.