how to get cookie contents

Discussion in 'PHP' started by shadow_boi, Dec 1, 2008.

  1. #1
    Hi all.

    I got a problem when retrieving contents from cookies that I have set.

    Here are the codes:

    
    // NOW SET COOKIES
    setcookie('km_rt_user_id', $id, time()+120, '/', '', 0);
    setcookie('km_rt_user_name', $user_name, time()+120, '/', '', 0);
    				
    echo "<br />Now cookies have been dropped. Checking...<br />";
    echo "{$_COOKIE['km_rt_user_id']}  -  {$_COOKIE['km_rt_user_name']}";
    
    Code (markup):
    The errors I am getting are:
    Notice: Undefined index: km_rt_user_id
    Notice: Undefined index: km_rt_user_name

    But, I can see the cookies have been set when I check the browser cookies.

    Any help appreciated.

    Thanks.
     
    shadow_boi, Dec 1, 2008 IP
  2. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    
    setcookie("km_rt_user_id", $id, time()+120);
    setcookie("km_rt_user_name", $user_name, time()+120);
    
    PHP:
    And why would you expire a cookie after 2 minutes? Normally you use like 1 hour :)

    If the script still returns a notice, you can disable notices.. Just put this in top of your php file that sets the cookie:

    error_reporting (E_ALL ^ E_NOTICE);

    (think it's that).
     
    elias_sorensen, Dec 2, 2008 IP
  3. shadow_boi

    shadow_boi Peon

    Messages:
    374
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your reply. I used 2 minz for testing. xd

    I already tried your codes, but no luck.


    It is so weird, after the cookie is dropped, for some reason it cannot detect the cookie.

    
    setcookie('km_rt_user_name', $user_name, time()+60);
    				
    if(isset($_COOKIE['km_rt_user_name'])) {				
    	echo "lala";
    }
    
    Code (markup):
    So, it cant even printout lala.

    so strange.
     
    shadow_boi, Dec 2, 2008 IP
  4. shadow_boi

    shadow_boi Peon

    Messages:
    374
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here are the codes from my login php.

    
    <?php
    
    include_once('db_connect.php');
    include_once('global_function.php');
    
    
    if(isset($_COOKIE['km_rt_user_id']))
    {
    	echo "You have already logged in. Now directing you to homepage." ;	
    	echo '<script type="text/javascript">';
    	echo "\n<!--\n";
    	echo "setTimeout('Redirect()',5000);";
    	echo "\nfunction Redirect()";
    	echo "{\n";
    	echo "\tlocation.href = 'login.php';\n";
    	echo "}\n";
    	echo "\n--></script>\n";
    
    }
    
    else // COOKIE IS NOT SET YET
    {
    	echo "cookie is not set yet.";
    	
    	$error = array(); $user_name = null; $password = null;
    	
    	if(isset($_POST['submitted'])) // FORM IS SUBMITTED
    	{	
    		if(empty($_POST['user_name']))	
    		$error[] = 'You forgot to enter your User Name.';
    		else $user_name = escape_data($_POST['user_name']); 
    		
    		if(empty($_POST['password']))
    		$error[] = 'You forgot to enter your Password.';
    		else $password = escape_data($_POST['password']);
    		
    		if(empty($error)) // USER_NAME AND PASSWORD ARE FILLED
    		{
    		$query = mysql_query("SELECT id, user_name FROM login
    					   WHERE user_name = '" . $user_name . "' 
    					   AND password = '" . MD5('$password') . "'
    					  ") or die(mysql_error());
    			
    		// Save result
    		list($id, $user_name) = mysql_fetch_row($query);
    
    		// If id is not empty then a combination was found
    		if(!empty($id)) {
    				echo "Record found!!! Welcome " . $user_name . " !";
    				
    				// NOW SET COOKIES
    				setcookie('km_rt_user_id', $id, time()+60);
    				setcookie('km_rt_user_name', $user_name, time()+60);
    				
    				echo "<br />Now cookies have been dropped. Checking...<br />";
    				
    				//echo "{$_COOKIE['km_rt_user_id']}  -  {$_COOKIE['km_rt_user_name']}";
    				if(isset($_COOKIE['km_rt_user_name'])) {
    				
    				echo "lala";
    
    				$the_name = $_COOKIE["km_rt_user_name"];echo $the_name;
    				}
    								
    				exit();			
    			}
    			
    			else {
    				echo "no record found.";
    				}
    				
    		}
    		
    		else {// THERE ARE SOME ERRORS WITH USER_NAME AND PASSWORD
    			foreach($error as $msg)
    				echo "<p>" . $msg . "</p>";
    		}
    	}
    
    	
    ?>
    
    	<form action="login.php" method="post">
    	<p>User Name: <input type="text" name="user_name" size="20" maxlength="30" /></p>
    	<p>Password: <input type="password" name="password" size="20" maxlength="30" /></p>
    	<p><input type="submit" name="submit" value="Login" /></p>
    	<input type="hidden" name="submitted" value="TRUE" />
    	</form>
    	
    <?php
    } // END OF COOKIE IS NOT SET YET
    ?>
    
    PHP:
     
    shadow_boi, Dec 2, 2008 IP
  5. shadow_boi

    shadow_boi Peon

    Messages:
    374
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oh , it seems like it is not possible to set a cookie and then retrieve the cookie content right after it.
    It needs a page load again.

    I simply refreshed the page, the cookie content showed up.
     
    shadow_boi, Dec 2, 2008 IP
  6. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    OR, you could try to use ob_start(); and in the ending line ob_end_flush();
     
    elias_sorensen, Dec 2, 2008 IP
  7. Mr.Shan0

    Mr.Shan0 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    header wouldn't set if you are printing some date before header like:

    <?php
    echo "testing";
    header("Location: http://www.example.com/");
    ?>
    Code (markup):
    so that you need to add ob_start(); at the top & ob_end_flush(); at the end :)
     
    Mr.Shan0, Dec 3, 2008 IP
  8. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #8
    Yes. Cookie does not automatically detected if there is no cookie before. You have to reload the page or you can use redirection into the same page.
     
    xrvel, Dec 4, 2008 IP