PHP in Windows: Header, Setcookie, w/ Login Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cfytable
Forum Commoner
Posts: 29
Joined: Thu May 12, 2005 3:36 pm

PHP in Windows: Header, Setcookie, w/ Login Form

Post by cfytable »

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?

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

and

Code: 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=1157

I should keep a count of how many times I've responded with that... :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I was guilty of this myself in the beginning, but I wonder why it is that a lot of people think they can send a header in the middle of a page.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

scrotaye wrote:I was guilty of this myself in the beginning, but I wonder why it is that a lot of people think they can send a header in the middle of a page.
Why can't you
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I was guilty of this myself in the beginning, but I wonder why it is that a lot of people think they can send a header in the middle of a page.
Ignorance, I guess. They don't know what header means, just that it's some thing that does black magic.
cfytable
Forum Commoner
Posts: 29
Joined: Thu May 12, 2005 3:36 pm

Explanation

Post by cfytable »

As a way of explanation, I think that I, as an ASP guy, naturally look for the equivalent of Response.Redirect, and, unless I'm missing something, header() seems the closest thing in PHP, but, since the latter deals with header information of documents, it really is a totally different thing altogether, but that may be why people end up in this situation like me.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Did that topic feyd posted get you sorted? The few options are:

1. Use Output Buffering
2. Rethink the design completely
3. Use JavaScript to set the cookie
cfytable
Forum Commoner
Posts: 29
Joined: Thu May 12, 2005 3:36 pm

Post by cfytable »

I did get it working using ob_start() . I appreciate your help--that was a great overview.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ob_start() = band-aid. ;)
Post Reply