Echoing content of php session, then clearing it

Associate
Joined
15 Feb 2006
Posts
1,872
Location
hell
Hi guys

Bit of a weird issue with PHP that only seems to affect Chrome and not Firefox.

I am trying to populate the contents of a json object using PHP.If someone signs up to my site, I create a PHP session variable identifying them as a new registration. They then get redirected back to the homepage using the header function.

On the homepage I have the below script. The correct output should read {regType' : 'normal'} (which works in Firefox). In Chrome it appears as 'blank' - which is odd as that session update only happens after the echo!

Any ideas?!


PHP:
    <script>
              
      dataLayer = [
                  <?php
                   // session_start();
                    if(isset($_SESSION['register']) && !empty($_SESSION['register']))
                    {
                      $regtype = $_SESSION['register'];
                      echo "{'regType' : '" . $regtype . "'}";
                      $_SESSION['register'] = "blank";                 
                    }
                      ?>
                   
                   ];
      
    </script>
 
Soldato
Joined
6 Aug 2007
Posts
2,516
How about something like:

PHP:
if (isset($_SESSION['register']))
{
    $regType = $_SESSION['register'];
    unset($_SESSION['register']);

    echo json_encode(compact('regType'));
}
 
Associate
Joined
10 Nov 2013
Posts
1,808
When you do the redirect using 'header' are you exiting the script immediately afterwards? It's possible that you're doing something after calling header which could be changing your session variable - if you're redirecting to the same page then the session variable could be being set to 'blank' before the redirect happens.
 
Associate
OP
Joined
15 Feb 2006
Posts
1,872
Location
hell
When you do the redirect using 'header' are you exiting the script immediately afterwards? It's possible that you're doing something after calling header which could be changing your session variable - if you're redirecting to the same page then the session variable could be being set to 'blank' before the redirect happens.


This is the code on the page before (register.php). I have tried adding die() and exit() and it doesn't seem to help. Note that the correct $_SESSION value appears when I remove "unset" on the page that the user gets redirected to. It's as if the unset line is appearing above the echo line so the variable ends up being blank. Not sure why this would happen


PHP:
    if(isset($error))
    {
        header( 'Location:'. $actual_link . "?error=" . $error );
    }
    else
    {
    
        $password = hash('sha256',$password);
        
        $date = date("Y-m-d H:i:s");
        $sql="INSERT INTO users (user_name, user_password,user_email, user_join_date) VALUES ('$username', '$password','$email', '$date') ";
        mysql_query($sql);       
        $finduserid="SELECT user_id from users WHERE user_name = '$username'";
        $user_id = mysql_query($finduserid);
        $row = mysql_fetch_array($user_id);
        $user_id = $row['user_id'];
        $expire = time() + 31556926;
        setcookie("user_id",$user_id, $expire, '/');
        setcookie("password",$password, $expire, '/');
        $_SESSION["register"] = "normal";
        header( 'Location:'. $actual_link);
        die();
    }
 
Back
Top Bottom