note 54266 deleted from function.setcookie by betz
| From: | betz@php.net | Date: | Wed, 29 Jun 2005 10:15:48 +0000 |
| Subject: | note 54266 deleted from function.setcookie by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-91114@lists.php.net to get a copy of this message | ||
Note Submitter: paintballa198 at aol dot com
Reason: useless
----
This is a simple problem but I saw it come up in a few forums so I might as well post it.
When setting a cookie you will need to refresh the page before the browser actually reads the
cookie. Meaning if you were setting a log-in cookie through a form, and the action of the form is
the page itself (lets say you were on index php and, after the submit button is pressed and the
script is executed, index.php loads again), you would need to reload the page if you wanted to
display the cookie for example.
An easy fix for this is to add a header("Location:index.php"); function after the
setcookie() function. The browser will read the cookie the first time it reloads after the form
button is pressed.
My log in script looks like this.
<?php
include("connect.php");
$match= "SELECT id FROM members WHERE username =
'".$_POST['username']."' AND password =
'".$_POST['password']."';";
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry);
//*******create login or logout
if("LogIn" == $Loggin){
if ($num_rows > 0) {
setcookie("loggedin", "TRUE", 0);
setcookie("talentrewards_username", "$username") ;
//********refresh automatically when cookie is set.
header("Location:index.php");
}else{
echo ("Sorry, there is no username $username with the specified
password.<br><br>");
}
}
if("LogOut" == $Loggin){
setcookie("loggedin", "FALSE");
//********refresh automatically when cookie is set.
header("Location:index.php");
}
?>