ravisan1 Posted September 8, 2014 Share Posted September 8, 2014 (edited) 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 September 8, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 8, 2014 Share Posted September 8, 2014 Can you post the code which sets $_SESSION["Email"]...the one that appears to remove $_SESSION['patchurl']? Also, please surround the code with tags. It will make your post and code easier to follow. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 8, 2014 Share Posted September 8, 2014 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 Quote Link to comment Share on other sites More sharing options...
ravisan1 Posted September 8, 2014 Author Share Posted September 8, 2014 (edited) 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 September 8, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 8, 2014 Share Posted September 8, 2014 @ravisan you need to use bbcode tags when posting code not <code></code> I have modified you post for you. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 8, 2014 Share Posted September 8, 2014 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>'; ?> Quote Link to comment Share on other sites More sharing options...
ravisan1 Posted September 8, 2014 Author Share Posted September 8, 2014 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 .. Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 8, 2014 Share Posted September 8, 2014 In your second script, PHP may be having a hard time parsing this out: <?phpsession_start(); Change it to: <?php session_start(); Now see if $_SESSION['patchurl'] is set. Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted September 9, 2014 Solution Share Posted September 9, 2014 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. Quote Link to comment Share on other sites More sharing options...
ravisan1 Posted September 9, 2014 Author Share Posted September 9, 2014 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 :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.