SlideShare a Scribd company logo
1 of 3
how would i fix my code, any login doesnt work but it should retrieve it from the table
table: user
hw1.php
code:
<?php
// 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');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Protected Page</title>
</head>
<body>
<h1>Welcome to the protected page</h1>
</body>
</html>
hw1login.php
code:
<?php
// Start the session
session_start();
// Connect to the database
$conn = new mysqli('localhost', '<database>', '<username>', '<password>');
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;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<?php if (isset($error)) { ?>
<p><?php echo $error; ?></p>
<?php } ?>
<form method="post">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

More Related Content

Similar to how would i fix my code- any login doesnt work but it should retrieve.docx

Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)
Joao Lucas Santana
 
Who Needs Ruby When You've Got CodeIgniter
Who Needs Ruby When You've Got CodeIgniterWho Needs Ruby When You've Got CodeIgniter
Who Needs Ruby When You've Got CodeIgniter
ciconf
 
How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdf
Appweb Coders
 
User Login in PHP with Session & MySQL.pdf
User Login in PHP with Session & MySQL.pdfUser Login in PHP with Session & MySQL.pdf
User Login in PHP with Session & MySQL.pdf
Be Problem Solver
 

Similar to how would i fix my code- any login doesnt work but it should retrieve.docx (20)

Php (1)
Php (1)Php (1)
Php (1)
 
Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Building Persona: federated and privacy-sensitive identity for the Web (Open ...Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Building Persona: federated and privacy-sensitive identity for the Web (Open ...
 
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
 
PHP and Databases
PHP and DatabasesPHP and Databases
PHP and Databases
 
Php update and delet operation
Php update and delet operationPhp update and delet operation
Php update and delet operation
 
PHP || [Student Result Management System]
PHP || [Student Result Management System]PHP || [Student Result Management System]
PHP || [Student Result Management System]
 
23.simple login with sessions in laravel 5
23.simple login with sessions in laravel 523.simple login with sessions in laravel 5
23.simple login with sessions in laravel 5
 
Php session
Php sessionPhp session
Php session
 
Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
Who Needs Ruby When You've Got CodeIgniter
Who Needs Ruby When You've Got CodeIgniterWho Needs Ruby When You've Got CodeIgniter
Who Needs Ruby When You've Got CodeIgniter
 
Diving into php
Diving into phpDiving into php
Diving into php
 
How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdf
 
Connecting_to_Database(MySQL)_in_PHP.pptx
Connecting_to_Database(MySQL)_in_PHP.pptxConnecting_to_Database(MySQL)_in_PHP.pptx
Connecting_to_Database(MySQL)_in_PHP.pptx
 
Building scalable products with WordPress - WordCamp London 2018
Building scalable products with WordPress - WordCamp London 2018Building scalable products with WordPress - WordCamp London 2018
Building scalable products with WordPress - WordCamp London 2018
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
You're Doing it Wrong - WordCamp Orlando
You're Doing it Wrong - WordCamp OrlandoYou're Doing it Wrong - WordCamp Orlando
You're Doing it Wrong - WordCamp Orlando
 
Security in laravel
Security in laravelSecurity in laravel
Security in laravel
 
User Login in PHP with Session & MySQL.pdf
User Login in PHP with Session & MySQL.pdfUser Login in PHP with Session & MySQL.pdf
User Login in PHP with Session & MySQL.pdf
 
1cst
1cst1cst
1cst
 

More from JacobUasThomsoni

I a study of general incorporation laws in the Uniled States- Eric Hit.docx
I a study of general incorporation laws in the Uniled States- Eric Hit.docxI a study of general incorporation laws in the Uniled States- Eric Hit.docx
I a study of general incorporation laws in the Uniled States- Eric Hit.docx
JacobUasThomsoni
 
I have to submit this assignment after one hour Scope Management Plan.docx
I have to submit this assignment after one hour  Scope Management Plan.docxI have to submit this assignment after one hour  Scope Management Plan.docx
I have to submit this assignment after one hour Scope Management Plan.docx
JacobUasThomsoni
 

More from JacobUasThomsoni (20)

I can put it in a infographic or poster if you just help me with all t.docx
I can put it in a infographic or poster if you just help me with all t.docxI can put it in a infographic or poster if you just help me with all t.docx
I can put it in a infographic or poster if you just help me with all t.docx
 
Human Factors Assignment For your individual ( any medical product )pr.docx
Human Factors Assignment For your individual ( any medical product )pr.docxHuman Factors Assignment For your individual ( any medical product )pr.docx
Human Factors Assignment For your individual ( any medical product )pr.docx
 
I am working on an assignment that im builidng a game- I am wanting to.docx
I am working on an assignment that im builidng a game- I am wanting to.docxI am working on an assignment that im builidng a game- I am wanting to.docx
I am working on an assignment that im builidng a game- I am wanting to.docx
 
I a study of general incorporation laws in the Uniled States- Eric Hit.docx
I a study of general incorporation laws in the Uniled States- Eric Hit.docxI a study of general incorporation laws in the Uniled States- Eric Hit.docx
I a study of general incorporation laws in the Uniled States- Eric Hit.docx
 
Human computer interaction 1- Find five design issues with Codeblocks.docx
Human computer interaction 1- Find five design issues with Codeblocks.docxHuman computer interaction 1- Find five design issues with Codeblocks.docx
Human computer interaction 1- Find five design issues with Codeblocks.docx
 
Hypertension has been known to be called the -Silent Killer- due to wh.docx
Hypertension has been known to be called the -Silent Killer- due to wh.docxHypertension has been known to be called the -Silent Killer- due to wh.docx
Hypertension has been known to be called the -Silent Killer- due to wh.docx
 
I have to submit this assignment after one hour Scope Management Plan.docx
I have to submit this assignment after one hour  Scope Management Plan.docxI have to submit this assignment after one hour  Scope Management Plan.docx
I have to submit this assignment after one hour Scope Management Plan.docx
 
I have no idea what this means List all the kinds of eggs that can be.docx
I have no idea what this means List all the kinds of eggs that can be.docxI have no idea what this means List all the kinds of eggs that can be.docx
I have no idea what this means List all the kinds of eggs that can be.docx
 
If David Jones wants to save for his child's college tuition in 20 yea.docx
If David Jones wants to save for his child's college tuition in 20 yea.docxIf David Jones wants to save for his child's college tuition in 20 yea.docx
If David Jones wants to save for his child's college tuition in 20 yea.docx
 
If artificial intelligent systems that were designed to diagnose disea.docx
If artificial intelligent systems that were designed to diagnose disea.docxIf artificial intelligent systems that were designed to diagnose disea.docx
If artificial intelligent systems that were designed to diagnose disea.docx
 
If an investor requires a real return of 3- and the average inflation.docx
If an investor requires a real return of 3- and the average inflation.docxIf an investor requires a real return of 3- and the average inflation.docx
If an investor requires a real return of 3- and the average inflation.docx
 
If a survey of 2001 adults in a fecont year- 701 made a Now Years teso.docx
If a survey of 2001 adults in a fecont year- 701 made a Now Years teso.docxIf a survey of 2001 adults in a fecont year- 701 made a Now Years teso.docx
If a survey of 2001 adults in a fecont year- 701 made a Now Years teso.docx
 
If a Himalayan rabbit is crossed to an albino rabbit- could one of the.docx
If a Himalayan rabbit is crossed to an albino rabbit- could one of the.docxIf a Himalayan rabbit is crossed to an albino rabbit- could one of the.docx
If a Himalayan rabbit is crossed to an albino rabbit- could one of the.docx
 
If A and B are two independent events- then P(AB)-P(B) True False.docx
If A and B are two independent events- then P(AB)-P(B) True False.docxIf A and B are two independent events- then P(AB)-P(B) True False.docx
If A and B are two independent events- then P(AB)-P(B) True False.docx
 
If 2+3i3+2i-a+ib- than Proof a2+b2-1.docx
If 2+3i3+2i-a+ib- than Proof a2+b2-1.docxIf 2+3i3+2i-a+ib- than Proof a2+b2-1.docx
If 2+3i3+2i-a+ib- than Proof a2+b2-1.docx
 
Identifying the weaknesses of a company is a factor that should be ana.docx
Identifying the weaknesses of a company is a factor that should be ana.docxIdentifying the weaknesses of a company is a factor that should be ana.docx
Identifying the weaknesses of a company is a factor that should be ana.docx
 
Identify the specific social- political- and economic challenges that.docx
Identify the specific social- political- and economic challenges that.docxIdentify the specific social- political- and economic challenges that.docx
Identify the specific social- political- and economic challenges that.docx
 
Identify what is inaccurate in the author's diagram that shows- Figure.docx
Identify what is inaccurate in the author's diagram that shows- Figure.docxIdentify what is inaccurate in the author's diagram that shows- Figure.docx
Identify what is inaccurate in the author's diagram that shows- Figure.docx
 
Identify the equation used to prepare a budget- Multiple Choice Assets.docx
Identify the equation used to prepare a budget- Multiple Choice Assets.docxIdentify the equation used to prepare a budget- Multiple Choice Assets.docx
Identify the equation used to prepare a budget- Multiple Choice Assets.docx
 
Identify some of the issues and concerns associated with connecting de.docx
Identify some of the issues and concerns associated with connecting de.docxIdentify some of the issues and concerns associated with connecting de.docx
Identify some of the issues and concerns associated with connecting de.docx
 

Recently uploaded

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

how would i fix my code- any login doesnt work but it should retrieve.docx

  • 1. how would i fix my code, any login doesnt work but it should retrieve it from the table table: user hw1.php code: <?php // 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'); } ?> <!DOCTYPE html> <html> <head> <title>Protected Page</title> </head> <body> <h1>Welcome to the protected page</h1> </body> </html> hw1login.php code:
  • 2. <?php // Start the session session_start(); // Connect to the database $conn = new mysqli('localhost', '<database>', '<username>', '<password>'); 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; } } ?> <!DOCTYPE html> <html> <head> <title>Login</title> </head> <body> <h1>Login</h1>
  • 3. <?php if (isset($error)) { ?> <p><?php echo $error; ?></p> <?php } ?> <form method="post"> <label>Username:</label> <input type="text" name="username"><br> <label>Password:</label> <input type="password" name="password"><br> <input type="submit" value="Login"> </form> </body> </html>