Jump to content

get username after login


adi123

Recommended Posts

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

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.