man5 Posted June 17, 2016 Share Posted June 17, 2016 So I have a code that is suppose to save form sessions and then redirect to another website. It does redirect but the sessions are never saved when I go back to my site. And yes, I do have session_start() at the very top of the page. And also, the sessions do get saved on locahost server but not live server. Do you know why this is happening? Here's the code example. if(isset($_POST['submit'])) { $name = trim($_POST['name']); $email = trim($_POST['email']); $_SESSION['name'] = $name; $_SESSION['email'] = $email; $errors = array(); $db->beginTransaction(); if(empty($name)) { $errors[] = 'Name is required.'; } if(empty($email)) { $errors[] = 'Email is required.'; } if(empty($errors)) { $db->commit(); $new_url = 'https://www.google.ca/'; ?> <script> window.location.href = '<?php echo $new_url; ?>'; </script> <?php exit(); } else { $db->rollBack(); } } Quote Link to comment https://forums.phpfreaks.com/topic/301352-php-page-redirect-doesnt-save-sessions/ Share on other sites More sharing options...
ginerjm Posted June 17, 2016 Share Posted June 17, 2016 (edited) That code doesn't really show us anything. A session is not just for a form - it is everything that you specifically save as a session variable, regardless if it has anything to do with a form. Assuming that you have the session start at the top of THIS script, what does your following script have for code? And just what is that little snippet (poorly placed IMHO) doing for you? Is that the 'following' script? Why aren't you just using a header command from php? Edited June 17, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/301352-php-page-redirect-doesnt-save-sessions/#findComment-1533768 Share on other sites More sharing options...
Jacques1 Posted June 17, 2016 Share Posted June 17, 2016 You can actually debug session problems yourself: Open the developer tools of your browser (or the Firebug extension in case you're using Firefox), and you'll get a detailed overview of all HTTP messages and cookies. This allows you to find out where exactly things go wrong. Typical session problems are: There's output before the session_start() call, and output buffering is off (this is a server setting, so it can be different on your local test server and the production server). You're using different domains like yoursite.com and www.yoursite.com. Does the problem occur only in this particular script, or do you have trouble with sessions in general? Quote Link to comment https://forums.phpfreaks.com/topic/301352-php-page-redirect-doesnt-save-sessions/#findComment-1533769 Share on other sites More sharing options...
Solution man5 Posted June 19, 2016 Author Solution Share Posted June 19, 2016 It seems like the sessions are now being saved, despite redirecting to another page. I didn't change anything. I guess the online servers need a bit of time to be updated. Quote Link to comment https://forums.phpfreaks.com/topic/301352-php-page-redirect-doesnt-save-sessions/#findComment-1533825 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.