Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

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...
0
trrsrr
Asked:
trrsrr
3 Solutions
 
trrsrrAuthor Commented:
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

0
 
Cornelia YoderArtistCommented:
Are you crossing domains?   SESSION variables do not persist when you change domain control.
0
 
trrsrrAuthor Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Rok-KraljCommented:
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.
0
 
AielloJCommented:
trrsrr:

I believe you need to add a
   session_start();
to the top of the login.php to access the $SESSION array.

AielloJ
0
 
trrsrrAuthor Commented:
@Rok-Kralj:
Yes, I havent think about it before ...
I'll try to add error_reporting(E_ALL);  now and see what happens nthen...
0
 
Rok-KraljCommented:
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.
0
 
trrsrrAuthor Commented:
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

0
 
Rok-KraljCommented:
The problem could be that you are transferring files in FTP RAW mode instead of ASCII. But I doubt that, try it, it couldn't hurt.

But problem is, I suspect, that you are running PHP 5 and hosting is running PHP4.

Try this on both hosts.
<?php // here is page1.php
error_reporting(E_ALL);
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
error_reporting(E_ALL);
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

0
 
trrsrrAuthor Commented:
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 ...
0
 
trrsrrAuthor Commented:
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
0
 
trrsrrAuthor Commented:
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...
0
 
trrsrrAuthor Commented:
Thanks all...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now