PDA

View Full Version : trouble with PHP and cookies




MacLevel
Jan 24, 2005, 11:50 PM
I'm trying to set up a VERY simple, and not very secures 1 user system using cookies, I made this.... it can be found at http://macboy.maclevel.com/login.php user: test, pass: test_password
the login works, but the cookies don't.

any help would be appreciated.

this is the code:
<html><head><title>Welcome!</title></head><body>
<?php

// Define username and password
$username = "test";
$password = "test_password";

if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {

echo ( 'Welcome' . $username . '!' );

}
else{

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

?>

<h1>Login</h1>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
****<p><label for="txtUsername">Username:</label>
****<br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

****<p><label for="txtpassword">Password:</label>
****<br /><input type="password" title="Enter your password" name="txtPassword" /></p>

****<p><input type="submit" name="Submit" value="Login" /></p>

</form>

<?php

}
else {
setcookie ('username' , $username, time()+3600);
setcookie ('password' , $password, time()+3600);
?>
Login Successful! <br /> <a href="<?php echo( $_SERVER['PHP_SELF'] ); ?>" >Continue</a>

<?php
}
}
?>



Forced Perfect
Jan 30, 2005, 04:51 AM
Are the cookies being set at all? Or is it just not reading them after they've been set?

ddtlm
Jan 30, 2005, 07:13 AM
Looks to me (its late and I skimmed your code) like you are setting the cookie after printing HTML. If you are not buffering the HTML (via the PHP config file) this will not work, as cookies must be set before any HTML is sent.