Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Cookies needing to be set twice?

    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.

    Code:
    <?php
    
    	if (!isset($_COOKIE['first_name']))	{
    	
    		header("Location: index.php?pageid=login");
    		exit();
    		
    	} 
    	
    		require_once('../mysql_connect.php');
    
    		$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;
    						}
    				
    					</style>
    					
    						<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>';
    					
    				}
    				
    				mysql_close();
    				
    			} 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>
    
    </form>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #4
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •