Link to home
Start Free TrialLog in
Avatar of trrsrr
trrsrrFlag for United States of America

asked on

Why and Where did the $SESSION gone ?

This really confused me where this should be a simple task.
4yi the development in my localhost (using XAMPP) works perfect but when I uploaded to the hosting then it's so weird to know that the session is gone I dont know where ???
Simply the login script goes like this (incorpoorate 2 files login.php and home.php ; some codes are removed but I know you'll get the point):

login.php
+++++++++
if ( array_key_exists('login', $_POST) ) {
      if(empty($_POST['username']) || empty($_POST['password']) )
      {
            // Error message here (works)
      }
      else
      {
            // validate info with MySQL db (works)
            // set user logged in session and redirect to home.php (not working)
            session_start();
            $_SESSION['logged_in'] = 1;
            header("Location: home.php");
      }
}


home.php
+++++++++
session_start();

if( !isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != 1 )
{
      header("Location: login.php");
}

// User's logged in contents here


And the problem is... everytime a successfull login from login.php it redirect me back to login.php (no session sets too, I've checked with print_r($_SESSION) on login.php).
I can only have success login if I removed this codes from home.php
if( !isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != 1 )
{
      header("Location: login.php");
}
.. and if I put print_r($_SESSION) on home.php it will shows nothing .. means no session has been sets correctly while login.

Anyone can advice me why & where did the session gone ?

Thanks...
Avatar of trrsrr
trrsrr
Flag of United States of America image

ASKER

And top makes it more simple, I could remove query to MySQL db and the result is still the same ...
Tried this on login.php so user supposed to be able to login if they fill anything on the username & password fields ... still the session is gone somewhere ...
login.php
+++++++++
if ( array_key_exists('login', $_POST) ) { 
      if(empty($_POST['username']) || empty($_POST['password']) ) 
      {
            // Error message here (works)
      } 
      else 
      {
            session_start(); 
            $_SESSION['logged_in'] = 1;
            header("Location: home.php");
      }
}

Open in new window

SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trrsrr

ASKER

No I am not crossing domains of course... as I told before, the script/codes works perfectly on localhost (I'm using XAMPP) but this problem seems to appear when I upload it to a hosting.
add error_reporting(E_ALL); to the top of both files, so you know where the error is.

I would assume that you have some whitespace or BOM (byte order mark) before session_start() call. The error reporting setting will expose those errors.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trrsrr

ASKER

@Rok-Kralj:
Yes, I havent think about it before ...
I'll try to add error_reporting(E_ALL);  now and see what happens nthen...
I ment to write that too. On every page that contatins $_SESSION, you have to call session_start(), before any output is made. Make sure you add that line to top of evey script.
Avatar of trrsrr

ASKER

This really drove me crazy .. I dunno how can it be... seems like $_SESSION wll only last for one page and if I go to another page then the $_SESSION is lost.

I simplified the case example then made page1.php and page2.php (Codes are follow below)

If I go to page1.php it's output:
GO TO PAGE 2 Array ( [test] => TEST )

Then clicking to page2.php here is the output:
GO TO PAGE 1 Array ()

Please help me ... I really have no idea where or how to fix this issue .. perhaps you guye ever experiences this kind of problem? Or perhaps you think you can help me getting the $_SESSION between all pages?

PLEASE NOTE: This problem only appears when I uploaded files to hosting. Local development on my PC (using XAMPP) is not experienced this problem.
<?php // here is page1.php
session_start();
$_SESSION['test'] = 'TEST';
echo '<a href="./page2.php">GO TO PAGE 2</a> ';
print_r($_SESSION);
// Output: GO TO PAGE 2 Array ( [test] => TEST )
?>

<?php // here is page2.php
session_start();
echo '<a href="./page1.php">GO TO PAGE 1</a> ';
print_r($_SESSION);
// Output: GO TO PAGE 1 Array ()
?>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trrsrr

ASKER

Okay,
I tried adding error_reporting(E_ALL); and still the same.
Actually I cannot think of any errors that might occur too as both just have simple codes.

On localhost I have PHP 5.2.6 and the hosting has PHP 5.2.12

Let me know if you think you can come up with something else ...
Avatar of trrsrr

ASKER

This is really a weird situation for me ... I just found out that the hosting supports PHP 5.2.12 with register_globals ON and magic_quotes_gpc OFF
... though I'm not sure it will affects such simple codes as above. Th $_SESSION just keep lost.
I think I will have to change the login page to use cookie I dunno...

Is there anyone ever experience the same problem like this?
BTW the hosting is on FatCow
Avatar of trrsrr

ASKER

My bad ...
I just found out that the hosting allowed clients to edit PHP.ini from the control panel.
And also I found that the problem is because session_save_path is not configured properly.

So, the prolems are fixed now and I think I'll just split points for those who have tried to help me in this out...

Best regards to all...
Avatar of trrsrr

ASKER

Thanks all...