Page 1 of 1

setcookie issues

Posted: Thu Feb 04, 2010 7:01 pm
by socalocmatt
I can not, for the life of me, figure out why this is not setting a cookie:

Code: Select all

 
        if ($remember=="1") {
            setcookie("wbclothes", "123456",time()*60*60*24*90);
            echo "1";
        } else {
            setcookie("wbclothes", "123456",time()*60*60*24);
            echo "1";
        }
 
When I echo $_COOKIE['wbclothes'] I don't get anything. I then used print_r($_COOKIE) and only receive:
Array ( [_csoot] => 1265329542040 [_csuid] => 4978e12904dc08ee )

I also tried:

Code: Select all

 
setcookie("wbclothes", "123456",time()+3600);
echo "1";
 
With the same results.

No wbclothes?!?!? :banghead:

I also cleared my cookies, switched browsers, and used another computer to test just to make sure it wasn't on my computer's end.

Re: setcookie issues

Posted: Sun Feb 07, 2010 4:05 am
by greyhoundcode
PHP Manual wrote:setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
Is it possible that some whitespace has been sent to the browser before your call to setcookie()? What you reproduced here is obviously only a snippet, but for instance could something like this have happened:

Code: Select all

 
<?php // Note the redundant line above this one
    setcookie('CookieName', 'ApplicationData');
?>

Re: setcookie issues

Posted: Sun Feb 07, 2010 10:03 am
by social_experiment
I added "/" to your setcookie parameters and the cookie was set.

Code: Select all

<?php
 if ($remember=="1") {
             setcookie("wbclothes", "123456",time()*60*60*24*90,"/");
             echo "1";
         } else {
             setcookie("wbclothes", "123456",time()*60*60*24,"/");
             echo "1";
         }
?>

Re: setcookie issues

Posted: Tue Feb 09, 2010 11:15 am
by socalocmatt
I deleted the code and retyped it. It worked fine for 2 days. Now, today, no cookie is being set again. I double checked and there is no white space, and no output prior to setting the cookie. To set the cookie, I have used:

Code: Select all

 
setcookie("wbclothes",$user[0],(time()*60*60*24)); (worked for 2 days)
setcookie("wbclothes",$user[0],(time()*60*60*24),"/");
setcookie("wbclothes",$user[0],(time()*60*60*24),"/","www.domain.com"); (worked for one day)
I also took out the () around time with mixed results. I also did a cut and paste from W3C for sh!ts and giggles with no luck.
 
Here is the code is the entire code:

Code: Select all

 <?
$_POST['action'];
 
//Begin Login
if ($action=="login") {
 
include "include/dbconnect.php";
$uname=trim($_POST['un']);
$pword=trim($_POST['pw']);
 
if (($uname!="")||($pword!="")) {
 
$secureit='94541';
$pword=sha1($securit.trim($pword));
 
$qPASS="SELECT * FROM cusers WHERE uname='$uname' AND upass = '$pword' AND active = '1'";
$qrhPASS=mysql_query($qPASS,$link);
$countPASS=mysql_num_rows($qrhPASS);
    if ($countPASS==1) {
        $user=mysql_fetch_row($qrhPASS);
        setcookie("wbclothes",$user[0],(time()*60*60*24));
        $login=1;
    } else if ($countPASS==0) {
        $error=1;
        $errorTXT="Invalid Username or Password";
    } else {
        $error=1;
        $errorTXT="An error has occured. The site administrator has been contacted regarding this error. \n
        Please call customer service at (800) 707-9692 to complete your request.";
    }
} else {
    $error=1;
    $errorTXT="Invalid Username or Password";
 
}
}
//End Login
$URLreferer=parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
if ($URLreferer=="") {
} else if (($URLreferer=="domainname.com")||($URLreferer=="www.domainname.com")){
} else {
    header("Location: http://www.domainname.com/?popLogin=true");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link href="include/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="include/jquery.js"></script>
<script type="text/javascript" src="include/thickbox.js"></script>
<? if ($login==1) { ?>
<script language="javascript" type="text/javascript">
setTimeout(removeLogin(),500);
function removeLogin() {
    parent.tb_remove();
    parent.location.reload(1)
}
</script>
<? } ?>
</head>
 
<body>
<div id="formArea">
<table width="450" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><div align="center">
      <p><strong>Account Login</strong></p>
      <p align="left">To log into your account, please enter your user name and password. If you have forgotten your user name and/or password, please <a href="forgot_login.php">click here</a>. If you do not have a user name, please <a href="register.php">click here</a> to register for an account. </p>
    </div></td>
  </tr>
</table>
<form action="login.php" method="post" enctype="application/x-www-form-urlencoded" name="loginForm" id="loginForm">
<div id="error" class="error" align="center"><? if ($error==1) echo $errorTXT; ?></div>
<table width="300" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td width="120" align="right">Username: </td>
    <td><input name="un" type="text" id="un" /></td>
  </tr>
  <tr>
    <td align="right">Password:</td>
    <td><input name="pw" type="password" id="pw" /></td>
  </tr>
</table>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center" valign="middle"><span class="breadcrumbs">
    </td>
  </tr>
  <tr>
    <td align="center"><input name="action" type="hidden" id="action" value="login" />
    <input type="submit" name="Submit" value="Submit" /></td>
  </tr>
</table>
</form>
&nbsp;<br />
<table width="450" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center"><a href="forgot_login.php">Forgot Username</a> | <a href="forgot_login.php">Forgot Password</a> | <a href="register.php">Register for an Account</a> </td>
  </tr>
</table>
</div>
</body>
</html> 
I should also mention that the login form is being brought in as an iframe in a thickbox layer.

I have tried FF, IE, and Safari with no luck. What throws me for a loop is that when I get it to work, it will work for a day or two, and then the page will stop working when I have not touched the code. :crazy:

Re: setcookie issues

Posted: Tue Feb 09, 2010 2:03 pm
by socalocmatt
Update:
I reset the time from:

Code: Select all

 
setcookie("wbclothes",$user[0],(time()*60*60*24));
 
to:
 
setcookie("wbclothes",$user[0],time()+(60*60*24*7));
 
and it works, for now. I'm curious if I dont touch the code again, will I receive the same problem in a couple more days.