Jump to content

[SOLVED] Session variables


adam291086

Recommended Posts

Hello, I have a login script working. All is fine. This works by querying a database and if the result = 1 then set a session. What i want to do is get all the information relating to the user currently logged in. When i echo the session i get the value 1. How do i query the database to get all info on the user logged in?

Link to comment
https://forums.phpfreaks.com/topic/78235-solved-session-variables/
Share on other sites

This is the login code

<?php
// we must never forget to start the session
session_start(); 
$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
   include 'config.php';


   $userId = $_POST['txtUserId'];
   $password = $_POST['txtPassword'];

   // check if the user id and password combination exist in database
   $sql = "SELECT user_id 
           FROM tbl_auth_user
           WHERE user_id = '$userId' 
                 AND user_password = PASSWORD('$password')";

   $result = mysql_query($sql) 
             or die('Query failed. ' . mysql_error()); 

   if (mysql_num_rows($result) == 1) {
      // the user id and password match, 
      // set the session
      $_SESSION['db_is_logged_in'] = true;

      // after login we move to the main page
      header('Location: main.php');
      exit;
   } else {
      $errorMessage = 'Sorry, wrong user id / password';
   }

   include 'library/closedb.php';
}
?>


<?php

// Get the user
$sql = "SELECT * FROM tbl_auth_user WHERE `username_column_name` = '". $_SESSION['db_is_logged_in'] ."'") or die(mysql_error());

// Save it in var's
$row = mysql_fetch_row($sql);

?>

 

The data is now in an array. ($row[number]).

All you need do is change your query to get the extra data you want (in this example - username and email) and then place them in the $_SESSION array.

 

<?php
// we must never forget to start the session
session_start(); 
$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
   include 'config.php';


   $userId = $_POST['txtUserId'];
   $password = $_POST['txtPassword'];

   // check if the user id and password combination exist in database
   $sql = "SELECT user_id,username,email 
           FROM tbl_auth_user
           WHERE user_id = '$userId' 
                 AND user_password = PASSWORD('$password')";

   $result = mysql_query($sql) 
             or die('Query failed. ' . mysql_error()); 

   if (mysql_num_rows($result) == 1) {
      // the user id and password match, 
      // set the session
      $_SESSION['db_is_logged_in'] = true;

      // I added this...
      $row = mysql_fetch_assoc($result);
      $_SESSION['username'] = $row['username'];
      $_SESSION['email'] = $row['email'];

      // after login we move to the main page
      header('Location: main.php');
      exit;
   } else {
      $errorMessage = 'Sorry, wrong user id / password';
   }

   include 'library/closedb.php';
}
?>

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.