Jump to content

session variable gets reset


Go to solution Solved by mac_gyver,

Recommended Posts

I have a session variable called $_SESSION['patchurl'] in a php file , if i get in to an else statement this session variable gets set and i go to http://yyy page.

below is the snippet of the code

<?php
session_start();
?>
<?php
echo '<script type="text/javascript">' . "\n";
if(isset($_SESSION["Email"])){
echo 'window.location="http://www.xxx";';
}
else{
$_SESSION['patchurl'] = "true";
echo 'window.location="http://yyy";';
}
echo '</script>';?>

once the patchurl session variable is set i call a php file which sets an other session variable called $_SESSION["Email"]. now what happens is the $_SESSION['patchurl'] is gone and ONLY the $_SESSION["Email"] is accessible ...can i not set two session variables? why does creating a new session varible overwrites an other one even though they are called different ? am i doing something wrong ?

Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/290923-session-variable-gets-reset/
Share on other sites

Side note: there's probably a reason you're using JavaScript to redirect the page. But just in case you're not aware, you can redirect visitors using PHP and the header() function. More information can be found here:

http://php.net/manual/en/function.header.php

Thanks for the quick response ..below is the code that sets the patchurl session and  redirect the user to register or login.

<?php
session_start();
?>
<?php
echo '<script type="text/javascript">' . "\n";
if(isset($_SESSION["Email"])){
echo 'window.location="http://www.xxx";';
}
else{
$_SESSION['patchurl'] = "true";
echo 'window.location="http://yyy";';
}
echo '</script>';?>

If the user is not registered he provides all the information and submits the information and that runs the below php file

 

below is the code where i set the email session variable and not able to get the patchurl session variable..

<?phpsession_start();
?>
<html><head></head>
<body>
<?php
// Create connection
$con=mysql_connect("db","yyyy","xxxx");
// Check connection
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("yyyy", $con);


$Email = mysql_real_escape_string($_POST['Email']);
$pwd1 = mysql_real_escape_string($_POST['pwd1']);  
$pwd2 = mysql_real_escape_string($_POST['pwd2']); 
$Cname = mysql_real_escape_string($_POST['Cname']);
$ques1 = mysql_real_escape_string($_POST['ques1']);
$ques2 = mysql_real_escape_string($_POST['ques2']);


if(isset($Email)){
$result = mysql_query("SELECT * FROM customer WHERE Email = '$Email'"); 
$get_rows = mysql_affected_rows($con);




if($get_rows >=1){
//echo "user exists";
die();
}
}


//THIS IS WERE I AM SETTING THE EMAIL SESSION VARIABLE


$_SESSION["Email"] = $Email;


//inserting all the required value to register the customer


$sql = "INSERT INTO `customer` (`Email`,`pwd1`,`pwd2`,`Cname`,`ques1`,`ques2`,`Identity`) 
VALUES
('$_POST[Email]', '$_POST[pwd1]', '$_POST[pwd2]', '$_POST[Cname]','$_POST[ques1]', '$_POST[ques2]','$numb')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }


if(isset($_SESSION["Email"])) 
{?>


// This is were the problem is after some troubleshooting i feel that once the session email is set this "patchurl session is doesn't seem to be live , i don't think i am getting in to the below if statement
<?php
   if(isset($_SESSION["patchurl"])){
   echo '<script type="text/javascript">' . "\n"; 
   echo 'window.location="http://www.zzzz.com";'; 
   echo '</script>';
   }
   else{?>
   <?php
   echo '<script type="text/javascript">' . "\n"; 
   echo 'window.location="http://www.kkkk.com";'; 
   echo '</script>';
    ?>
<?php
}
?>


</body>
</html>
Edited by Ch0cu3r

Have you tried echoing the entire contents of the SESSION array to see what it contains? For example, you could add the following after your call to session_start() in the script which sets the email variable:

<?php
session_start();
 
echo '<pre>' . print_r($_SESSION, true) . '</pre>';
 
?>

Thanks for all your help, will make sure to follow the right tags, i did print the session array and below is the observation ...

 

when printed at the top of the php file which is responsible for setting the email session i get the following 

 

Array

()
After setting the email session variable i get the following Array(    [Email] => r@10.com)

it is very clear that the $_SESSION ['patchurl'] is not being set ...i want to provide a little bit on the flow ..

 

user click on a link http://xxx.com, if he is not signed and not a register user i do the below

I set the $_SESSION [patchurl] in a php called sessionsaver.php

After setting this it goes to a registration.php where people enter all the details and click register

clicking on register takes to the next php file where i insert all the data from the previous form, it is here i check if the $SESSION[patchurl] is available, if it there i basically redirect them to the page http://xxx where they were trying to go before the registration ..

  • Solution

one of the most likely reasons for session variables not working when redirecting all over the place is that the redirects are switching back and forth between having and not having the www. on the url, because by default, a session will only match the variation of the url where it was set.

Thanks that helped, not having consistent www format was the problem. After consistently changing the url across all php fixed the issue. Thanks to both of you for providing feedback and helping me fix the issue. Have been breaking my head for the last two days :-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.