Jump to content

Sessions getting lost


adam291086

Recommended Posts

checking the session

<?php 
session_start();
// is the one accessing this page logged in or not?
if (!isset($_SESSION['db_is_logged_in'])
   || $_SESSION['db_is_logged_in'] !== true) 
{
// not logged in, move to login page   
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.bcyorkshire.co.uk/admin/login.php\">";
}

 

setting the session

<?php
// we must never forget to start the session
session_start(); 
$errorMessage = '';
  $userId = $_POST['txtUserId'];
   $password = $_POST['txtPassword'];
   
   mysql_real_escape_string($userId);
   mysql_real_escape_string($password);
include '../database/config.php';
// check if the user id and password combination exist in database
$conn;   
$sql = "SELECT user_name
           FROM tbl_auth_user
           WHERE user_name = '$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;



  // adds the username to session
     
   $row = mysql_fetch_assoc($result);
        $_SESSION['username'] = $row['user_name'];


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

   include '../database/closedb.php';


if ($errorMessage != '') {
?>
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>

 

 

 

Link to comment
Share on other sites

<?php 
session_start();
// is the one accessing this page logged in or not?
if ( empty($_SESSION['db_is_logged_in']) ) 
{
    // not logged in, move to login page   
    echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.bcyorkshire.co.uk/admin/login.php\">";
}
?>

Link to comment
Share on other sites

Replace...

 

if (!isset($_SESSION['db_is_logged_in'])
  || $_SESSION['db_is_logged_in'] !== true)

 

with....

 

if (!isset($_SESSION['db_is_logged_in']))

 

ps: Using the mysql PASSWORD function to store your passwords will lead to code braking if you ever upgrade mysql versions. Stick to md5.

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.