i can anyone help me, i am unsure when this code will not set a cookie. Can anyone see why?
<?php
if (isset($_POST['cookie'])) {
$id = $_POST['cookie'];
// correct username and password so set cookie
setcookie("user_id", "$id", time()+3600);
// redirect
echo "<meta http-equiv=\"refresh\" content=\"1; URL=index.php\">";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Jake Welton</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
require_once('header.php');
$user = $_POST['username'];
$pass = $_POST['password'];
// Initialize an error array
$errors = array();
// Check for a username
if (empty($user)) {
$errors[] = 'You forgot to enter your username.';
} else {
$username = mysqli_real_escape_string($dbc, trim($user));
}
// Check for a password
if (empty($pass)) {
$errors[] = 'You forgot to enter your password.';
} else {
$password = mysqli_real_escape_string($dbc, trim($pass));
}
// if a username and password have both been entered
if (empty($errors)) {
//----------------------------------------------------------------------------------------------------------
$realPass = SHA1($password);
$sql = "SELECT * FROM `users` WHERE username='$username' AND password='$realPass'";
$result = mysqli_query($dbc, $sql);
if(mysqli_num_rows($result) == 0) {
die('
<p style="text-align: center; font-size: 25px;"><br>Incorrect username or password</p>
<br>
<center>
<form action="login.php" method="POST">
<fieldset style="width:500px; height: 170px;">
<legend>
  Please login or register  
</legend><br>
Username: <input type="text" name="username">
<br><br>
Password: <input type="password" name="password">
<br><br><br>
<input type="hidden" name="submitted" value="TRUE">
<input style="font-family:Arial, Helvetica, sans-serif;color:#6294A8;font-size:12px; height: 25px; width: 100px" type="submit" value="Login" name="login">
<div style="text-align: right;">
<a href="reset_password.php">Forgotten Password</a>
        
        
        
        
        
        
        
        
     
<a href="register.php">Register   </a>
</div>
</fieldset>
</form>
</center>
<br><br><br><br>
');
} else {
// If we're here, the login was correct, now we want the UserID?
$row = mysqli_fetch_assoc($result);
$userID = $row['user_id'];
// display welcome message
echo "<p style=\"text-align: center; font-size: 25px;\"><br>Hello $username</p>";
echo "
<form action=\"login.php\" method=\"POST\">
<input type=\"hidden\" name=\"cookie\" value=\"$userID\">
</form>
<meta http-equiv=\"refresh\" content=\"1; URL=login.php\">
";
}
// if a username or password was not entered
} else {
echo '<br>
<p style="margin-left: 350px;" class="error">The following error(s) occurred:<br /><br>';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '<br>
<center>
<form action="login.php" method="POST">
<fieldset style="width:500px; height: 170px;">
<legend>
  Please login or register  
</legend><br>
Username: <input type="text" name="username">
<br><br>
Password: <input type="password" name="password">
<br><br><br>
<input type="hidden" name="submitted" value="TRUE">
<input style="font-family:Arial, Helvetica, sans-serif;color:#6294A8;font-size:12px; height: 25px; width: 100px" type="submit" value="Login" name="login">
<div style="text-align: right;">
<a href="reset_password.php">Forgotten Password</a>
        
        
        
        
        
        
        
        
     
<a href="register.php">Register   </a>
</div>
</fieldset>
</form>
</center>
<br><br><br><br>';
}
require_once('footer.php');
?>
</body>
</html>
Many Thanks,J


Sign In
Create Account


Back to top










