2

Hello i am having problems holding sessions from page to page, code worked on my previous servers running php5 but not on my recent server, i am wondering whether its a bug?

<?php
session_start();
$_SESSION['session'] = $_POST['session'];

header("location: www.mysite.com/page1.php");
?>


<?php
session_start();

echo "Good morning" . $_SESSION['session'];   //returns empty session always.
?>

ANy ideas? session is held on first page but not on the second.

hakre
  • 193,403
  • 52
  • 435
  • 836
user1302936
  • 31
  • 1
  • 3

6 Answers6

2

In case you missed it, make sure you do a session_start() at every page you're using the $_SESSION variable.

You should check your php.ini file and see what's going on.

Make sure session.use_cookies = 1 and session.save_handler = files.

Use this test page to see whether it's a general PHP problem or just your code.

<?php
    session_start();

    if(isset($_SESSION)){
        echo "Session variable exists<br/>";

        if(!isset($_SESSION['test'])){
            $_SESSION['test'] = "Success!";
            echo "Variable has been set, refresh the page and see if stored it properly.";
        }else{
            echo $_SESSION['test'];
        }
    }else{
        echo "No session variable has been created.";
    }
?>

If that worked, then it's got to do with your code.

If you're setting your session variable to $_POST['session'] am I to assume you submitted a form with an input with the name session?

This setup should work.

index.php

<form action='page0.php' method='POST'>
    <input type='hidden' name='session' value='SPAAAAACE' />
    <input type='submit' />
</form>

Page0.php

<?php
    session_start();
    $_SESSION['session'] = $_POST['session'];

    header("location: www.mysite.com/page1.php");
?>

Page1.php

<?php
    session_start();

    echo "Good morning" . $_SESSION['session'];
?>

For completeness and debugging purposes

In case you are using cookie-less sessions, you have to manually add the SID (session id) to the header redirect like this

header("location: www.mysite.com/page.php?".htmlspecialchars(SID));

If the problem still persists, it could be a permission issue.
Maybe you're not allowed to read the session file stored on the server?

Update: OP commented that it was a permission issue and the problem is now resolved

ShadowScripter
  • 7,314
  • 4
  • 36
  • 54
  • Hello i found the problem thanks to you, it was a permissions issue. – user1302936 Mar 30 '12 at 13:05
  • @user1302936 Hah, I had the same problem at work a couple of months ago. Didn't have admin clearance. Anyway, good thing you were able to solve it. Would you please mark my answer as "Accepted", it's the green check sign at the top left of my post. – ShadowScripter Mar 30 '12 at 13:10
0

Turn on error reporting temperately with: error_reporting(E_ALL) This may spit out an error related to your problem. Most likely an undefined index session notice.

You should always have a check in place on Super Globals.

<?php
session_start();
$_SESSION['session'] = (isset($_POST['session']))?$_POST['session']:null;

header("Location: www.mysite.com/page1.php");
die;
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

Your code seems correct though I'm pretty sure $_POST['session'] is empty.

You should try this :

<?php
session_start();
$_SESSION['session'] = 'John Doe';

header("location: www.mysite.com/page1.php");
?>


<?php
session_start();

echo "Good morning" . $_SESSION['session'];   //returns empty session always.
?>

To see if this works or not. I guess it will.

IF not, take a look at your cookies, maybe they are disabled.

Then, if it works, I probably because $_POST['session'] is null or empty, are you sure you posted something like <input type="text" name="session" /> ?

Cyril N.
  • 38,875
  • 36
  • 142
  • 243
-1

You need to pass the session id with the redirect.

Also make sure you use session_start() at the top of EVERY page that needs a session

Al_
  • 2,479
  • 7
  • 29
  • 43
-1

First try using

<?php session_start();

instead of

<?php
session_start();

If the problem still exists, then open your script in Netbeans editor and see whether any unexpected characters found at very beginning of the the script.

In addition, please make sure that $_POST['session'] has a value to assign in $_SESSION['session'].

A.N.M. Saiful Islam
  • 2,118
  • 5
  • 28
  • 34
  • It doesn't change anything, the code is the same from the compiler perspective. – Cyril N. Mar 30 '12 at 12:36
  • I know, but depending on scripting environment (os and ide) sometimes few unexpected characters inserted into the script which does not shown in many editors. Still sometimes I face the problem. – A.N.M. Saiful Islam Mar 30 '12 at 12:38
-3

You will have to call

session_start();

on the first line of every page you want to retain the session in

ping localhost
  • 479
  • 3
  • 22