Nile is partly correct. The setcookie and header parts need to be placed before anything is outputted to the browser. Basically, you would need to do something like the following:
index.php
Code:
<?php
/* These are our valid username and passwords */
$user = 'test';
$pass = 'test';
if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {
header('Location: newlogin.php');
}
else {
$welcome = 'Welcome back ' . $_COOKIE['username'];
}
}
else {
header('Location: newlogin.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<link rel = "stylesheet" type = "text/css"
href="../externalCSS.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php echo $welcome; ?>
<form method="post" name="cookie" action="newlogin.php">
<table align="center">
<tr>
<td ><sub><img src="../design images/logo.jpg" width="76" height="87"/></sub><b>Four Seasons</b></td>
</tr>
<tr><td><hr color="#CC0033"/></td></tr>
<tr><td align="center">
<table style= "border-style:solid;border-color:#CC0033;border-width:thin;align:center">
<tr><td><label for="username">Username: </td><td><input name="username" t id="username" type="text" size="25" /></label></td></tr>
<tr><td><label for="password">Password: </td><td><input name="password" id="password" type="password" size="25" /></label></td></tr>
<tr><td><input type="checkbox" name="setcookie" value="setcookie" /> Remember Me</td></tr>
<tr align="center"><td colspan="2"><input type="submit" name="submit" value="Submit" /><input type="reset" name="reset" value="Reset"/></td></tr></table>
</td></tr>
</form>
<!--The follwing row is just for pushing the footer to the bottom-->
<tr height="90"><td></td></tr>
<tr><td><br /><hr color="#CC0033"/></td></tr>
<tr><td align="center"><p class="smallFont">King Saud University. All rights reserved,2008</p></td></tr>
</table>
</body>
</html>
newlogin.php
Code:
<?php
/* These are our valid username and passwords */
$user = 'test';
$pass = 'test';
if (isset($_POST['username']) && isset($_POST['password'])) {
if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) {
if (isset($_POST['rememberme'])) {
/* Set cookie to last 1 year */
setcookie('username', $username, time()+60*60*24*365, '', '');
setcookie('password', md5($password), time()+60*60*24*365, '', '');
}
else {
/* Cookie expires when browser closes */
setcookie('username', $username, false, '', '');
setcookie('password', md5($password), false, '', '');
}
$body = '<h1>Registered!</h1><p>Thank you <b>'.$username.'</b>, your information has been added to the database, you may now <a href='adminHome.htm' title='Login'>log in</a>.</p>';
}
else {
$body = 'Username/Password Invalid';
}
}
else {
$body = 'You must supply a username and password.';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php echo $body; ?>
</body>
</html>
Hope this helps.
Bookmarks