Asked  9 Months ago    Answers:  5   Viewed   72 times

I have a simple form which passes a session variable and it simply fails to load on the second page. I had it running on another server, and after moving it to a new one, it no longer works. I have same PHP version (PHP 5) on both, and it works on one and not on the other - the $_SESSION array is just completely empty.

I checked to see if the session id's were the same, and they are exactly the same on both pages of the form (NOT on both servers, these are obviously different).

session_start(); is the first line of code on all pages of the form.

First Page

session_start();

echo "session id ".session_id();

$_SESSION["gencode"] = $gencode;

Second Page

session_start();

echo "session id ".session_id();  

echo $_SESSION["gencode"];

Again, I had it working exactly the same on another server, after the move this part broke, should I be looking for a setting somewhere on the server? Both are Linux, if the session id is echoing that means the same session exists, correct?

Any advice would help.

 Answers

25
  1. Check the php.ini on both servers and confirm the session settings are the same.
  2. var_dump($_SESSION) to see what is in the session. You may see something interesting.
Wednesday, March 31, 2021
 
BradM
answered 9 Months ago
15

the php-memcached extension supports session locking

http://us3.php.net/memcached

http://us1.php.net/manual/en/memcached.sessions.php

the memcache and memcached extensions look syntactically similar so it may not be too much of a headache to give it a try. (memcached has a stable version 2.1.0 released 2012-08-07).


if you are set on using memcache 2.2.7 you will most likely have to implement the lock yourself by setting some "session_is_locked" variable in your session and then releasing/unsetting it when the script is done writing to the session. Then you'd always need to check if that variable is set before continuing with any scripts which write to the session.

Wednesday, March 31, 2021
 
penpen
answered 9 Months ago
48

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

Saturday, May 29, 2021
 
LaKaede
answered 7 Months ago
67

if you use session at any point session_start() should be the first line of just before any printing functions.

start session at the starting of the page then try to print echo " $_SESSION["s"]";

Saturday, May 29, 2021
 
csi
answered 7 Months ago
csi
87

session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.

Thursday, July 29, 2021
 
jab
answered 4 Months ago
jab
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share