Welcome to WebmasterWorld Guest from 54.211.136.250

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Problem with setting cookies

   
10:26 pm on Jul 4, 2007 (gmt 0)

10+ Year Member



Hi Everyone,

I'm trying to set a cookie for a site to use to verify if a member has logged in.

I've got the cookie working fine, the problem is it only works when I have it as the first line of code. If I move it anywhere else in my php scripts, it stops setting the cookie.

Here is a sample of the code. I've put the setcookie in the spot where I need it to be, which doesn't work. If I move the setcookie to the top of the page it works.

if($submitType == "login"){
$username = $_POST['username'];
$password = $_POST['password'];

$loginStmt = "SELECT * FROM $tablename WHERE business_email = '$username' ";
$loginresult = mysql_query($loginStmt) or die("Problem with finding the login info. Please alert us and let us know you were having this problem. Our error reference is: " . mysql_error() . ". <BR><BR>The query data is: " . $loginStmt);
$num = mysql_num_rows($loginresult);
if($num > 0){
$testLogin = mysql_fetch_array($loginresult);
$mem_num = $testLogin['membership_number'];
if($mem_num == $password){
$msg = "Login Successful";
setcookie("login_successful", $testLogin['firstName'], time()+3600,"/");
} else {
$msg = "The password is incorrect.";
}
} else {
$msg = "The username was not found.";
}

} else if($submitType == "logout") {
echo "need logout script";

}
@mysql_free_result($loginresult);

10:32 pm on Jul 4, 2007 (gmt 0)

5+ Year Member



The cookie must be set before any output is sent to the browser - i.e. before any text is displayed. Is your script set up to display errors?
11:19 pm on Jul 4, 2007 (gmt 0)

10+ Year Member



Well, there is error reporting in the select statements.

I've also tried moving that whole block of code to the top of the page, but it's looking like the cookie needs to be the first thing to happen or it doesn't set.

Would the solution be to check the login, if login is successfull, forward to a new page that sets the cookie, then forward back to the original page?

I've seen many sites with logins, and they always seem to bounce between pages before the login successful message is displayed, is that what is happening?

6:15 pm on Jul 5, 2007 (gmt 0)

5+ Year Member



Your code looks correct. Remember that you can't read a cookie from inside the page that initially set it without either refreshing the page or moving to another, because the cookie data is sent as a header when loading a page.
7:35 pm on Jul 5, 2007 (gmt 0)

5+ Year Member



try this -

if($submitType == "login"){
$username = $_POST['username'];
$password = $_POST['password'];

$loginStmt = "SELECT * FROM $tablename WHERE business_email = '$username' ";
$loginresult = mysql_query($loginStmt) or die("Problem with finding the login info. Please alert us and let us know you were having this problem. Our error reference is: " . mysql_error() . ". <BR><BR>The query data is: " . $loginStmt);
$num = mysql_num_rows($loginresult);
$testLogin = mysql_fetch_array($loginresult);
$mem_num = $testLogin['membership_number'];
mysql_close();
if($num > 0 && $mem_num == $password){
$msg = "Login Successful";
setcookie("login_successful", $testLogin['firstName'], time()+3600,"/");
} else {
$msg = "The password or username is not found.";
}
}
if($submitType == "logout") {
echo "need logout script";
}
@mysql_free_result($loginresult);

4:28 am on Jul 9, 2007 (gmt 0)

10+ Year Member



As far as I know,

The set-cookie function is basically a wrapper for:
header ("set-cookie: batman = the dark knight");

Headers have to be sent before anything is output to the screen, or php will throw out an error. Therefore, setcookie() must be called before you output anything on screen, including whitespace. Therefore, I think, you should put setcookie() before print all of the or echo statements in your script.