Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how would i fix my code, any login doesnt work but it should retrieve it from the table table: user hw1.php code: Protected Page Welcome

how would i fix my code, any login doesnt work but it should retrieve it from the table

table: user

image text in transcribed

hw1.php

code:

// Start the session

// 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

hw1login.php

code:

// Connect to the database $conn = new mysqli('localhost', '', '', ''); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

// 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, set a cookie named "userid", store the user ID in the session, and redirect to the hw1.php page if ($stmt->num_rows > 0) { $stmt->bind_result($id); $stmt->fetch(); setcookie('userid', $id, time() + (86400 * 30), "/"); $_SESSION['userid'] = $id; header('Location: hw1.php'); exit; } else { $error = 'Incorrect username or password'; header('Location: hw1login.php'); exit; } } ?>

Login

Login

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

Students also viewed these Databases questions

Question

List six functions of neuroglia.

Answered: 1 week ago

Question

how would you have done things differently?

Answered: 1 week ago

Question

What were the reasons for your conversion or resistance?

Answered: 1 week ago

Question

How was their resistance overcome?

Answered: 1 week ago