Re: Cookie, header, output problems
| From: | Vahan Yerkanian | Date: | Tue, 14 Jan 2003 13:09:33 +0000 |
| Subject: | Re: Cookie, header, output problems | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-131621@lists.php.net to get a copy of this message | ||
You should not add any output after doing a "Location:" redirect. It's
recommended to add an exit() after the header(). Proper browsers should
halt their renderer and redirect when they find a Location: header.
HTH,
Vahan
J. Alden Gillespy wrote:
I'm creating an e-commerce website, and I just need clarification as to whether a MySQL query is considered as "browser output". I ask because I have the following code, but the cookie isn't being set: if ($op == "login") { if (!isset($username) && !isset($userpass)) { } else { if ($username == "" || $userpass == "") { $errormsg = "Invalid username or password. Please try again."; } else { if (mysql_num_rows(mysql_query("select * from$cart->user_tablewhere username=\"$username\" and userpass=\"$userpass\"", $cart->dblink)) == 1) { setcookie("jackloren_user", "$username:$userpass", time() + 2592000); // expires in 30 days header("Location: http://www.jackloren.com/"); // echo "test"; /* if ($rd != "") { redirectURL($rd,""); } else { redirectURL("",""); } */ } else { $errormsg = "Invalid username or password. Please try again."; } } } } However, if I comment out the header and uncomment the echo statement, then the cookie is added, no problem. It seems as if the browser is freaking out when there isn't any output after a cookie is set and a header is sent. I'm also having another similar problem with this function: function auth($redirect_url) { global $HTTP_COOKIE_VARS; if (!isset($HTTP_COOKIE_VARS['jackloren_user'])) { header("Location: http://www.jackloren.com/login.php"); // echo "test"; } else { $userinfo = explode(":", $HTTP_COOKIE_VARS['jackloren_user']); $username = $userinfo[0]; $userpass = $userinfo[1]; echo "$username | $userpass<br>"; if (mysql_num_rows(mysql_query("select * from$cart->user_tablewhere username=\"$username\" and userpass=\"$userpass\"", $cart->dblink)) != 1) { redirectURL("login.php", $redirect_url); } else { echo "success"; // nothing } } } If I leave the script the way it is, the browser isn't redirected no matter what I put in the header() statement. However, if I uncomment the echo statement, giving the browser some output, then the redirect works fine. Has anyone experienced this? And if so, how do I fix it? Thanks. J. Alden Gillespy (aka Dogga) Microsoft Beta Tester: - Office 11 - Content Management Server (CMS) 2002 - Systems Management Server (SMS) 2003