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.
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).
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.
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:
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.
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
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.