Jump to content

Losing data from session variables


davidp

Recommended Posts

Hey all,

 

I am having a situation where I have a singleton class with a static array in it.  I insert an element into that array, and then I redirect the user to another page.  At that page, I then retrieve that array (it's stored in the session vars), but the array is then empty when I retrieve it.  I can't figure out why.

 

Here are some pertinent code snippets.

 

First, the class with the static array:

class Logger
{
private static $instance = NULL;
private static $messages = array();

//Private constructor to prevent this class from being instantiated multiple times
private function __construct()
{
}

//The clone function is private to prevent cloning of this class
private function __clone()
{
}

//Returns the singleton instance of this class
public static function GetInstance()
{
	if (!self::$instance)
	{
		self::$instance = new Logger;
	}

	return self::$instance;
}

public static function GetMessageCount()
{
	return count(self::$messages);
}

public static function LogMessage($new_message)
{
	self::$messages[] = $new_message;
}

public static function GetMessages()
{
	return self::$messages;
}

public static function ClearMessages()
{
	self::$messages = array();
}
}

 

Here is the code where I insert something into the aformentioned array (the process is that a user tries to log in, but the login fails, and so we insert a message into the array saying that the login credentials failed).


//Retrieve the message logging instance from the session data to be able to pass messages to the user.
$message_log = $_SESSION['user_messages'];
$user = $_SESSION['user_account'];

//Create a new instance of the database management class and
//try to connect to the database
$dbm = new DBM();
if ( !$dbm->isConnected() )
{
$message_log::LogMessage("We are sorry, but we are unable to access the database at this time.  Please try again later.");
}
else
{
//Retrieve the user login information
$useremail_dirty = $_POST['useremail'];
$password_dirty = $_POST['userpassword'];

//Check to see if the email we were given exists in the database, and if the password matches
$doesPasswordMatch = $dbm->doesPasswordMatch ( $useremail_dirty, $password_dirty );
if ( $doesPasswordMatch )
{
	//Correct login information was received.  Login the user and redirect appropriately.
	$user->Login();
	header ( "Location: ".BASE_URL."/userpage.php" );
	exit;
}
else
{
	//If an incorrect email or password was given, report that information to the user.
	$message_log::LogMessage("Incorrect login information.");
}
}

//The user has failed to login.  Redirect to the appropriate page.
header ( "Location: ".BASE_URL."/loginfailed.php" );

 

And finally, given an unsuccessful login attempt, this is where I pick back up the array of messages to display the messages to the user:

$message_log = $_SESSION['user_messages'];
$all_messages = $message_log::GetMessages();

print $message_log::GetMessageCount()."<br>";
print count($all_messages)."<br>";

foreach ($all_messages as $message_string)
{
$prepared_string = prepare_text_for_html_output($message_string);
echo $prepared_string."<br>";
}

$message_log::ClearMessages();

 

Unfortunately, the "all_messages" variable has nothing in it...it's like the messages that I logged in the previous PHP script never even existed.  I am calling session_start, so don't worry about that (even though it's not seen in these code snippets).  What could be going wrong?

Link to comment
https://forums.phpfreaks.com/topic/229697-losing-data-from-session-variables/
Share on other sites

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.