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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.