1 Replies - 477 Views - Last Post: 02 February 2012 - 05:32 AM Rate Topic: -----

#1 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

session variable not being tranferred!

Posted 02 February 2012 - 02:56 AM

Hi to all,

after several tries i decided to post the question here as i tried several combinations but to no avail for the problem below:

All i need is to display in home page 'sign in' if user is still not logged in and hello username if user is logged in:
Here is the code i have:
if (!$_SESSION['session'] == 'loggedin') {
                  echo '<div id ="signinregister"><h3>Welcome';
                  include_once 'User.php';
                  $user = new User();
                  $user->getFullname($userId);
                   echo '</h3></div>'; 

                } else 
                     {

                        echo '<div id ="signinregister">Welcome!Please
                        <a href="login.php">Sign in</a>
                        or
                        <a href="#">register</a></div>';
                }



when home page loads 'sign in or register' come correctly but once user logs in by the below script and session variable is set to loggedin and then redirects to the home page, the if statement doesn't execute as i want it!

<?php

session_start();
//include_once 'configDb.php';
include_once 'User.php';
$user = new User();
/*if ($user->get_session())
{
header("location:index.php");
}*/

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
$login = $user->login($_POST['userName'], $_POST['userPassword']);//method to log in
if ($login) 
{
// Login Successful
//$_SESSION['sessionsetasloggedin'] == true;
session_start();
$_SESSION['session'] = 'loggedin';    
header("location:index.php");
} 
else 
{
// Login Failed
$msg= 'Username and/or Password wrong';
echo '<h3>'.$msg. '</h3>';
}
}
?>




Any help is greatly appreciated...

Is This A Good Question/Topic? 0
  • +

Replies To: session variable not being tranferred!

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: session variable not being tranferred!

Posted 02 February 2012 - 05:32 AM

You have multiple session_start() calls, just put one at the very start of the page. Make sure there is one at the top of index.php as well.

Call exit(); after your header() call.

Learn to use indentation. Your code should look like this:

<?php
session_start();

include_once 'User.php';
$user = new User();

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
    $login = $user->login($_POST['userName'], $_POST['userPassword']);//method to log in
    if ($login) 
    {
        // Login Successful
        $_SESSION['session'] = 'loggedin';    
        header("location:index.php");
        exit();
    } 
    else 
    {
        // Login Failed
        $msg= 'Username and/or Password wrong';
        echo '<h3>'.$msg. '</h3>';
    }
}
?>


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1