adi123 Posted August 23, 2010 Share Posted August 23, 2010 I have created a simple login system but need help getting the username after login is successfull. The login page <?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']); } ?> <div id="login"> <form method="post" action=""> <table width="200" border="0" align="center"> <tr> <th scope="col">Username:</th> <th scope="col"><input type="text" name="username" /></th> </tr> <tr> <td>Password:</td> <td><input type="password" name="pwd" /></td> </tr> <tr> <td colspan="2"><input type="submit" id="submit" value="Login" name="submit" /></td> </tr> </table> <p> </p> <p> <?php if(isset($response)) echo "<h4 class='alert'>" . $response . "</h4>"; ?> </p> </form> </div> Membership.php file <?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: myaccount.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"); } } myaccount page <?php require_once 'classes/Membership.php'; $membership = New Membership(); $membership->confirm_Member(); ?> <p>Welcome <?= $_SESSION['status'] ?></p Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/ Share on other sites More sharing options...
joel24 Posted August 23, 2010 Share Posted August 23, 2010 I'm assuming this function you're calling returns true or false depending if login credentials correct / incorrect. You should set the username into the session if this login attempt is successful //in your first file // 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']); if ($response == true) { $_SESSION['username'] = $_POST['username']; } } Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/#findComment-1102674 Share on other sites More sharing options...
adi123 Posted August 23, 2010 Author Share Posted August 23, 2010 that code does not work. Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/#findComment-1102681 Share on other sites More sharing options...
adi123 Posted August 23, 2010 Author Share Posted August 23, 2010 when the user logs in it prints welcome authorized rather than the username. Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/#findComment-1102682 Share on other sites More sharing options...
litebearer Posted August 23, 2010 Share Posted August 23, 2010 did you... 1. try echoing out the username? 2 store the username to a session variable? Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/#findComment-1102689 Share on other sites More sharing options...
Andy-H Posted August 23, 2010 Share Posted August 23, 2010 The login page <?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']); } ?> <div id="login"> <form method="post" action=""> <table width="200" border="0" align="center"> <tr> <th scope="col">Username:</th> <th scope="col"><input type="text" name="username" /></th> </tr> <tr> <td>Password:</td> <td><input type="password" name="pwd" /></td> </tr> <tr> <td colspan="2"><input type="submit" id="submit" value="Login" name="submit" /></td> </tr> </table> <p> </p> <p> <?php if(isset($response)) echo "<h4 class='alert'>" . $response . "</h4>"; ?> </p> </form> <> Membership.php file <?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'; $_SESSION['username'] = ucfirst($un); header("location: myaccount.php"); } else return "Please enter a correct username and password"; } function log_User_Out() { if( isset($_SESSION['status']) && isset($_SESSION['username']) ) { unset($_SESSION['status']); unset($_SESSION['username']); 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"); } } myaccount page <?php require_once 'classes/Membership.php'; $membership = New Membership(); $membership->confirm_Member(); ?> <p>Welcome <?php echo $_SESSION['username'] ?></p> Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/#findComment-1102695 Share on other sites More sharing options...
adi123 Posted August 23, 2010 Author Share Posted August 23, 2010 Andy-H thanks a lot for that code it worked. you really saved me a lot of time. thanks. Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/#findComment-1102698 Share on other sites More sharing options...
Andy-H Posted August 23, 2010 Share Posted August 23, 2010 No worries mate. Link to comment https://forums.phpfreaks.com/topic/211495-get-username-after-login/#findComment-1102700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.