Solved

Why and Where did the $SESSION gone ?

Posted on 2010-09-16
13
374 Views
Last Modified: 2012-05-10
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
Comment
Question by:trrsrr
13 Comments
 
LVL 1

Author Comment

by:trrsrr
ID: 33695820
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
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 100 total points
ID: 33695865
Are you crossing domains?   SESSION variables do not persist when you change domain control.
0
 
LVL 1

Author Comment

by:trrsrr
ID: 33695996
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
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33696940
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
 
LVL 13

Assisted Solution

by:AielloJ
AielloJ earned 100 total points
ID: 33700388
trrsrr:

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

AielloJ
0
 
LVL 1

Author Comment

by:trrsrr
ID: 33705276
@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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33708185
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
 
LVL 1

Author Comment

by:trrsrr
ID: 33732603
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
 
LVL 12

Accepted Solution

by:
Rok-Kralj earned 300 total points
ID: 33732638
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
 
LVL 1

Author Comment

by:trrsrr
ID: 33732792
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
 
LVL 1

Author Comment

by:trrsrr
ID: 33749435
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
 
LVL 1

Author Comment

by:trrsrr
ID: 33758006
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
 
LVL 1

Author Closing Comment

by:trrsrr
ID: 33758026
Thanks all...
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now