Hey there,
I'm trying to pass objects in session on one of my sites. The whole purpose is to cut down on mysql queries by loading the members information once and then only updating it when needed as they move around the site. However I'm running into an issue.
When I update the object it successfully updates the database, and appears to update the object. Yet when I return to the page (or refresh the page) the object has the old information in it, and not the updated information. The database still contains the correct values. I'm totally stumped here. Passing the object by value doesn't seem to make any difference.
Any ideas on whats going on?
Thanks,
Jade
[SOLVED] Objects in sessions issue
Moderator: General Moderators
[SOLVED] Objects in sessions issue
Last edited by Jade on Thu Jun 01, 2006 4:02 pm, edited 1 time in total.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
part of the member.php class
The login script...
One of the pages I'm having problems with. Allows the member to update their login information.
At the top of every page the member has a session initilized and it holds the members object first created
from the login page.
When I hit the submit button it works fine. It shows the page with all the fields updated with the correct information.
However when I refresh the page or visit the page again it holds the old data (before the changes).
Code: Select all
//**** CLASS VARIABLES ****//
var $mid; //member id number
var $name; //members paying name
var $email; //members email address
var $password; //members login password
var $atime; //active time
var $online; //member is being shown as online (logged_in)
var $lastlogin; //last login date
var $activate; //members activation number
var $confirmed; //if member is confirmed or not
var $tutorial; //if the member has taken the tutorial or not
//******* CLASS ARRAYS *******//
var $horses;
var $humans;
//******* CLASS OBJECTS *******//
var $birthday; //birthday object (day, month)
var $money; //money object (gold, silver, bronze)
var $equinestats; //horse stats object (spirit, energy, popularity, kick, rank, words, posts, ban)
var $humanstats; //human stats object (spirit, energy, popularity, kick, rank, words, posts, ban)
var $bans; //bans object (mail, newspaper, diary, game, human, horse)
var $permissions; //things they can have ie upgrade (addressbook, uploading pics, diary, games, email, stable, herd)
var $stable; //member's personal stable ID (upgrade feature)
var $herd; //member's personal herd ID (upgrade feature)
var $gmail; //members game mail (refreshes periodically)
var $addies; //members address book entries (refreshes periodically)
/****
* Purpose: creates a new member object
* Precondition: this member must have supplied valid login information
* that matches the password and email address of this account
* Postcondition: the member's basic information is available
****/
function member($id)
{
if (!$id || !is_numeric($id)) //no member id or wrong mid
return null;
//basic member information
$this->mid = $id;
$this->money = new money($this->mid);
$this->confirmed = $this->getConfirmed();
$this->name = $this->getName();
$this->password = $this->getPass();
$this->email = $this->getEmail();
$this->tutorial = $this->getTutorial();
//set and update member ip records
$this->setIp();//call set ip function
//set and update member logged-on records for members online
//if they aren't listed as being online
//set atime and last login as well
if (!$this->online)
$this->login(); //show member as being online
//get activation number if not already confirmed
if (!$this->confirmed)
$this->activate = $this->getActivationNumber();
$this->birthday = new birthday($this->mid);
$this->equinestats = new equinestats($this->mid);
$this->humanstats = new humanstats($this->mid);
$this->bans = new bans($this->mid, $this->equinestats, $this->humanstats);
$this->permissions = new permissions($this->mid);
//load the members mail
$this->gmail = $this->getMail($gmail);
//load the members address book entries
$this->addies = $this->getAddies($addies);
//load the members horses
$this->horses = $this->getHorses($horses);
//load the members humans
$this->humans = $this->getHumans($humans);
}
function email()
{
return $this->email;
}
function name()
{
return $this->name;
}
function pass()
{
return $this->password;
}
/****
* Purpose: returns the members player name
* Precondition: member is logged in
****/
function getName()
{
$result = mysql_query("SELECT name FROM members WHERE mid='$this->mid'")
or die ('cannot select member name');
$row = mysql_fetch_array($result);
return stripslashes($row['name']);
}
/****
* Purpose: sets the members name to the appropriate value
* Precondition: member is logged in
****/
function setName($string)
{
if (!$string)
return errorMsg("you must enter a name");
$string = removeTags($string);
mysql_query("UPDATE members SET name='$string' WHERE mid='$this->mid'")
or die ('cannot update member name');
$this->name = $string;
}
/****
* Purpose: returns the members password
* Precondition: member is logged in
****/
function getPass()
{
$result = mysql_query("SELECT pass FROM members WHERE mid='$this->mid'")
or die ('cannot select member password');
$row = mysql_fetch_array($result);
return stripslashes($row['pass']);
}
/****
* Purpose: sets the members password to the new password
* Precondition: member is logged in
****/
function setPass($string)
{
if (!$string)
return errorMsg("you must enter a password");
mysql_query("UPDATE members SET pass='$string' WHERE mid='$this->mid'")
or die ('cannot update member password');
$this->password = $string;
}
/****
* Purpose: updates the members object and database records
* Precondition: updated member settings
* Postcondition: member settings updated if information is correct
****/
function updateSettings($name, $email, $oldpass, $newpass)
{
if (!$oldpass)
return errorMsg("you must enter your password into the confirm password box");
if ($oldpass != $this->password)
return errorMsg("incorrect password. Please try again");
if (($this->email != $email) && emailExists($email))
return errorMsg("email address already in use");
//correct password was given
if ($name)
$this->setName($name);
if ($email)
$this->setEmail($email);
if ($newpass)
$this->setPass($newpass);
return successMsg("Settings updated");
}The login script...
Code: Select all
$member = & new member(getMid($_POST['email'], $_POST['pass']));
if ($member->id())
{
$_SESSION['member'] = $member; //assign their session
header("Location: news.php"); //bring them to news page
exit;
}
else
$error = errorMsg("incorrect email or password");At the top of every page the member has a session initilized and it holds the members object first created
from the login page.
Code: Select all
<?php
if ($_POST['submit'])
$error = $member->updateSettings($_POST['name'], $_POST['email'], $_POST['oldpass'], $_POST['newpass']);
?>
<form action=settings.php method=post>
<?php if ($error) echo "$error<br>"; ?>
<p align="left">
Playing Name:
<input name="name" type="text" id="name" value="<?php echo $member->name(); ?>">
</p>
<p align="left">Email Address:
<input name="email" type="text" value="<?php echo $member->email(); ?>">
</p>
<p align="left">Confirm Password:
<input name="oldpass" type="password">
</p>
<p align="left">New Password:
<input name="newpass" type="password">
</p>
<p align="center">
<input name="submit" type="submit" id="submit" value="Save Changes">
</form>However when I refresh the page or visit the page again it holds the old data (before the changes).
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
You should probably write and close the session before redirecting:
And you need to load the class and start the session before accessing the object:
Code: Select all
$member = & new member(getMid($_POST['email'], $_POST['pass']));
if ($member->id())
{
$_SESSION['member'] = $member; //assign their session
session_write_close()
header("Location: news.php"); //bring them to news page
exit;
}
else
$error = errorMsg("incorrect email or password");Code: Select all
<?php
include_once 'path/to/member.php'; // include before starting session
session_start();
$member =& $_SESSION['member']; // get a reference so changes are set in the session var too
if ($_POST['submit'])
$error = $member->updateSettings($_POST['name'], $_POST['email'], $_POST['oldpass'], $_POST['newpass']);
?>
<form action=settings.php method=post>(#10850)
Wow, I know this is kinda late, but I haven't seen used in a while. I just normally see direct declaration of variables. I am glad someone still uses this way, I thought I was being left out 
Code: Select all
Var $variable- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US