Jump to content

Redirect with session vars saved


ccravens

Recommended Posts

Hello,

 

I am having issues (and have had for some time) about redirecting while maintaining session variables.

 

I am trying to redirect the user back to the registration page after failing my validation script. This is the page after a user hits the "Register" button to submit their information. My code would look something like this:

 

<?php

session_start();

 

if(!session_is_registered("errors")) {

session_register("errors"); }

$errors = array();

 

if(empty($somefield))

$errors['somefield'] = "Please enter the information";

 

if(count($errors))

{

header("Location: http://my-url.com/Register.php");

exit();

}

 

and when I try to access $errors['somefield'] within Register.php, it is empty. Register.php does have a session_start() call at the top.

 

Thank you so much for your help! I appreciate any input into this issue.

Link to comment
https://forums.phpfreaks.com/topic/39263-redirect-with-session-vars-saved/
Share on other sites

Thank you for the help. The method I used is an example from an old book (2002) that I guess is outdated.

 

The code works when I store the session variable firstname. For example:

 

$_SESSION['firstname'] = "Please insert your first name";

 

However, am I not able to store an array into a session variable? For example:

 

$errors['firstname'] = "Please insert your first name";

$_SESSOION['errors'] = $errors;

 

Thank you! :-)

 

Chad

you spelt $_SESSION wrong. its $_SESSION not $_SESSOION

 

but maybe you wrote that just then. you should be able to.

 

example:

 

<?php
session_start();

$_SESSION['array_variable'] = array("Foo" => "Bar");
echo $_SESSION['array_variable']['Foo'];
?>

 

The above will echo Bar. it works. tested.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.