Jump to content
man5

Php page redirect doesn't save sessions.

Recommended Posts

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();
	}
}

Share this post


Link to post
Share on other sites

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 by ginerjm

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.