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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.