Question: how would i fix my code it wont login or print error table:user cookie is called userid hw1login.php Username: Password: hw1.php code: Protected Page Welcome

how would i fix my code it wont login or print error

table:user

how would i fix my code it wont login or print error

cookie is called userid

hw1login.php

// Check if the form is submitted if($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve the username and password from the form $username = $_POST['username']; $password = $_POST['password']; // Connect to the database $conn = mysqli_connect("localhost", "database", "username", "password"); // Check the connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Prepare the SQL statement to retrieve the user with the given username and password $sql = "SELECT * FROM user WHERE username='$username' AND password='$password'"; // Execute the SQL statement $result = mysqli_query($conn, $sql); // Check if a row is returned if (mysqli_num_rows($result) == 1) { // Fetch the row and get the user ID $row = mysqli_fetch_assoc($result); $userid = $row['id']; setcookie('userid', $id, time() + (86400 * 30), "/"); // Set the user ID in the session $_SESSION['userid'] = $id; // Redirect the user to the edit page header("Location: hw1.php"); } else { // Print a message that the username and password are not correct $error = 'Username and password are not correct.'; header('Location: hw1login.php');

} // Close the database connection mysqli_close($conn); } ?>

hw1.php

code:

// If the user is not logged in, redirect to the login page

if (!isset($_COOKIE['userid']) || !isset($_SESSION['userid'])) {

header('Location: hwlogin.php');

}

?>

Protected Page

Welcome to the protected page

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!