Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacLevel

macrumors newbie
Original poster
Aug 28, 2004
7
0
NYC
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:
PHP:
<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
}
}
?>
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.