PHP in Windows: Header, Setcookie, w/ Login Form
Posted: Fri Aug 26, 2005 2:28 pm
I've been reading a fair amount about the issue of not being able to set a cookie after header information has already been sent in Windows, but I'm not finding a good explanation as to how to structure code based on this concept. I am putting together a simple login form, where, if the username/password combo matches against a database table, it creates a cookie and sends the user to a secure page. I want to cut down on the number of .php pages and keep the code well-organized, but I keep running into the header issue. Can somebody explain how I would modify the code below to not encounter these header issues?
feyd | Please use
Code: Select all
//BEGIN PHP FILE
<? function ShowLoginForm() { ?>
// STANDARD HTML FORM FIELDS FOR USERNAME,PASSWORD, SUBMIT BUTTON
<? } ?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<?
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
ShowLoginForm();
} else {
if (USERNAME AND PASSWORD MATCH BASED ON AUTH FUNCTION) {
***
Here is where I am unclear on what I need to do; Since the user has passed the test here,
I want to set the cookie and redirect them to the secure page. The below does not work. *****
setcookie("loggedin", $username, time()+28800, "/") or die("Could not set cookie");
header("Location:home.php");
} else {
echo "Username/Password Do Not Match";
ShowLoginForm();
}
}
?>
</BODY>
</HTML>feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]