Jump to content

Recommended Posts

Hello all,

 

I have an error handler that I need to append messages to (First name not right, Last name not right, etc)

 

I'm using a session array to handle all error messages titled GORB.

 

How come this code won't work?

 

$_SESSION['GORB']['message'][] = "First name wrong";
$_SESSION['GORB']['message'][] = "Last name wrong";

 

How can I get it to work? I already have the handler output written and functioning fine, I just need to get it to loop over an array of errors instead of just one.

 

Link to comment
https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/
Share on other sites

then $_SESSION['GORB']['message'] has been defined as a string, not an array.

You can't "push" values onto a string.

Posting the relevant code would help.

 

Also, I wouldn't use the $_SESSION superglobal array to store simple errors, complete waste of resources.

like was stated, relevant code will help, but to append, you could always use .=

$_SESSION['GORB']['message'] = "";
$_SESSION['GORB']['message'] .= "First name wrong<br/>";
$_SESSION['GORB']['message'] .= "Last name wrong<br/>";

 

EDIT:

--but scootstah's answer below is better :)

Fantastic. Assigning it as an array first totally fixed it. Thank you all for your help.

 

Maybe a noob question but I honestly thought all $_SESSION values were by default arrays?

 

Also on that note, AyKay47, would you mind telling me the ideal alternative to passing the errors through the session? They have to be carried from one PHP file to another; would the alternative be logging IP session keys with error messages in the database then simply removing them once they are displayed?

Also on that note, AyKay47, would you mind telling me the ideal alternative to passing the errors through the session? They have to be carried from one PHP file to another; would the alternative be logging IP session keys with error messages in the database then simply removing them once they are displayed?

 

Typically errors are triggered within the executing script, so they don't need to be saved.

However, if this is necessary, use $_SESSION's

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.