Question: how would i rewrite my code with cookie called user so i cant acess hw1 without login first template: Input username: hwlogin.php code: Login Login
how would i rewrite my code with cookie called user so i cant acess hw1 without login first
template:
if($_POST['user'] == "admin") {
setcookie('user', 'admin', time() + (86400 * 30), "/"); // 86400 = 1 day
header("Location: hw1.php");
} else {
setcookie("user", "", time() - 3600, '/');
}
?>
//print_r($_COOKIE);
?>
hwlogin.php code:
// Connect to the database $conn = new mysqli('localhost', '
// If the form has been submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Get the username and password from the form $username = $_POST['username']; $password = $_POST['password'];
// Prepare the SQL query to check if the user exists $stmt = $conn->prepare('SELECT id FROM user WHERE username = ? AND password = ?'); $stmt->bind_param('ss', $username, $password); $stmt->execute(); $stmt->store_result();
// If the user exists, redirect to the edit page if ($stmt->num_rows > 0) { $stmt->bind_result($id); $stmt->fetch(); $_SESSION['userid'] = $id; header('Location: hw1.php'); exit; } else { $error = 'Incorrect username or password'; } } ?>
Login
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
