0

I'm working with a login.php page. when users login successfully, system will save their usernames into session.like below code:

if ($user != null) {
    $_SESSION['username'] = $user["username"];
    $_SESSION['userid'] = $user["id"];
    return 0;
}

Then the page will be located to main.php. Main.php will read the session, like below code:

session_start();
if (isset($_SESSION['username']) == false || empty($_SESSION['username']))
{
    // transfer into login page
}
else
{
    echo $_SESSION['username'];
}

The main page can't read the $_SESSION['username'], the value is null. What should I do now?

6
  • 4
    Did you put session_start(); on top of the page where you SET the session values. i.e. login.php ? Commented Mar 5, 2013 at 7:50
  • what var_dump($_SESSION) returns? Commented Mar 5, 2013 at 7:50
  • if (!isset($_SESSION['username']) || empty($_SESSION['username'])) Commented Mar 5, 2013 at 7:51
  • try var_dump($_SESSION) before and after SET session values on Login.php file and see what it show. Commented Mar 5, 2013 at 7:53
  • Your code is incomplete $user will always be null Commented Mar 5, 2013 at 7:57

4 Answers 4

2

You should add session_start() at the $user check condition page.

session_start();
if ($user != null)
{
    $_SESSION['username'] = $user["username"];
    $_SESSION['userid'] = $user["id"];
    return 0;
} 
Sign up to request clarification or add additional context in comments.

10 Comments

From the code, $user will always be NULL, the upper code is missing!
@WaqarAlamgir how can you say so.?? or you can say might be $user is null
It works.Is there any shortcut for not writing "session_start()" everywhere? Currently,I write this code in a included file.
@roast_soul yes you can make one common file which used in every page and include it in index.php file so you don't need to write it over and over again
@Dipesh Parmar:but I have to include it over and over.
|
1

You Must add session_start() when you use SESSION variable

session_start(); //at the beginning of file 

if ($user != null) {
    $_SESSION['username'] = $user['username'];
    $_SESSION['userid'] = $user['id'];
    return 0;
} 

and then

session_start();

if (isset($_SESSION['username'])) {
    // transfer into login page
}
else
{
    echo $_SESSION['username'];
}

Comments

1

If you want to user session variable. you must start session before use session variable.

session_start();
if ($user != null) {
    $_SESSION['username'] = $user["username"];
    $_SESSION['userid'] = $user["id"];
    return 0;
}

Comments

0
session_start();
if (count($user)>0 and is_array($user)) {
    $_SESSION['username'] = $user["username"];
    $_SESSION['userid'] = $user["id"];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.