View Full Version : Cookies needing to be set twice?

08-28-2006, 05:23 PM
I'm creating an page where members can update their details. The code is pretty self explanatory. My problem is that when the user has clicked to update their details, it all works fine, but when they return to the update details page, it still displays their old details in the boxes (the details in the database HAVE been altered though). This is obviously a cookie problem somewhere despite me updating the cookies when the details get updated. However, if I just leave it as it is and click update again,when I navigate back, the details are changed. The display is always one change behind the database.


if (!isset($_COOKIE['first_name'])) {

header("Location: index.php?pageid=login");



$u = $_COOKIE['username'];
$e = $_COOKIE['email'];
$fband = $_COOKIE['fav_band'];
$fmovie = $_COOKIE['fav_movie'];

if (isset($_POST['submit'])) {

if (($_POST['email']) != ($_COOKIE['email'])) {

$e = $_POST['email'];


$fband = $_POST['fav_band'];
$fmovie = $_POST['fav_movie'];

function escape_data ($data) {

global $dbc;

if (ini_get('magic_quotes_gpc')) {

$data = stripslashes($data);


return mysql_real_escape_string($data, $dbc);


$message = NULL;

if (empty($_POST['password'])) {

$p = FALSE;
$message .= '<p>You forgot to enter your password to confirm!</p>';

} else {

$p = escape_data($_POST['password']);


if (!empty($_POST['password1'])) {

if ($_POST['password1'] == $_POST['password2']) {

$np = escape_data($_POST['password1']);

} else {

$np = FALSE;
$message .= '<p>Your new passwords did not match!</p>';


} else {

$np = escape_data($_POST['password']);


if ($p && $np) {

$query = "SELECT user_id, fav_band, fav_movie, email FROM users WHERE (username='$u' AND password=MD5('$p'))";
$result = @mysql_query ($query);
$num = mysql_num_rows ($result);

if ($num == 1) {

$row = mysql_fetch_array ($result, MYSQL_ASSOC);

$query = "UPDATE users SET password=MD5('$np'), email='$e', fav_band='$fband', fav_movie='$fmovie', last_edited=NOW() WHERE user_id=$row[user_id]";
$result = @mysql_query ($query);

if (mysql_affected_rows() == 1) {

setcookie ('fav_band', $row['fav_band'], 0, '/', '', 0);
setcookie ('fav_movie', $row['fav_movie'], 0, '/', '', 0);
setcookie ('email', $row['email'], 0, '/', '', 0);

echo '<p>Your profile has been updated.</p>

<style type="text/css">

form {
display: none;


<meta http-equiv="Refresh" content="1;url=index.php">';

} else {

$message = '<p>Your profile could not be changed due to a system error. Probably space monkeys.</p>';


} else {

$message = '<p>Your username and password do not match our records.</p>';



} else {

$message .= '<p>Please try again</p>';



if (isset($message)) {

echo $message;



<form action="<?php echo $_SERVER['DOCUMENT_ROOT/index.php']; ?>" method="post">

<?php echo "<p>Change details for '" . $u . "' :" ?>

<p>E-mail address: <input type="text" name="email" size="30" maxlength="40"
value="<?php echo $e; ?>" /></p>

<p>Favourite band: <input type="text" name="fav_band" size="30" maxlength="30"
value="<?php echo $fband; ?>" /></p>

<p>Favourite movie: <input type="text" name="fav_movie" size="30" maxlength="40"
value="<?php echo $fmovie; ?>" /></p>

<p>Current password: <input type="password" name="password" size="15" maxlength="20" /></p>

<p>New password: <input type="password" name="password1" size="15" maxlength="20" /></p>

<p>Confirm new password: <input type="password" name="password2" size="15" maxlength="20" /></p>

<div align="center"><input type="submit" name="submit" value="Update details" /></div>


08-29-2006, 04:33 AM
You're not checking your queries to make sure they succeeded, so you can't really be sure what's happening. That's the first thing I would do to debug your code.

08-29-2006, 11:40 AM
it still displays their old details in the boxes (the details in the database HAVE been altered though).

The queries have succeeded as I said there.

08-29-2006, 12:04 PM
The problem is that you (re)fill the cookies from the $row array, which has been filled before the update of the data. This means the data from before the update is placed in the cookie.