Skip to content

Commit fd115ad

Browse files
authored
Add files via upload
1 parent 111fddf commit fd115ad

File tree

7 files changed

+537
-2
lines changed

7 files changed

+537
-2
lines changed

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
1-
# authentication-system-mysql-php-LOGIN-SYSTEM-IN-PHP-AND-MYSQL
2-
user login system using php and mysql
1+
# LOGIN SYSTEM IN PHP AND MYSQL: User Authentication system.
2+
3+
###### VERSION 1.0.0
4+
5+
User authentication in web developemen is used to authorized and
6+
restrict users to certain pages in a web appplication.
7+
8+
## REGISTERATION SYSTEM
9+
10+
### DATABASE TABLE IN MYSQL
11+
12+
The database used is MySQL, so you'll need a MySQL database to run create the users table.
13+
Run the `sql.sql` file in MySQL database to create users table
14+
15+
### CONFIGURATION FILE
16+
17+
The PHP script to connect to the database is in `config/config.php` directory.
18+
Replace credentials to in `config.php` to match your server credentials.
19+
20+
### REGISTERATION FORM AND SCRIPT
21+
22+
The `register.php` creates a web form that allows users to register themselves.
23+
The script generates error if form input is empty and username is has been taking already by another user.
24+
25+
## LOGIN SYSTEM
26+
27+
### LOGIN FORM AND SCRIPT
28+
29+
`login.php` is the login script.
30+
When a user submit a form with the input of username and password, these inputs will be verified against the credentials data stored in the database, if there is a match then the user will be authorized and granted access to site or page.
31+
32+
### WELCOME PAGE
33+
34+
User is redirected to `welcome.php` if login is successful.
35+
36+
### PASSWORD RESET
37+
38+
logged in user can reset password for registered account.
39+
script is in `password_reset.php`
40+
41+
<hr />
42+
<small>Give this a project ⭐ if you found it useful, and follow me for more interesting future projects</small>

login.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
// Initialize sessions
3+
session_start();
4+
5+
// Check if the user is already logged in, if yes then redirect him to welcome page
6+
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
7+
header("location: welcome.php");
8+
exit;
9+
}
10+
11+
// Include config file
12+
require_once "config/config.php";
13+
14+
// Define variables and initialize with empty values
15+
$username = $password = '';
16+
$username_err = $password_err = '';
17+
18+
// Process submitted form data
19+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
20+
21+
// Check if username is empty
22+
if (empty(trim($_POST['username']))) {
23+
$username_err = 'Please enter username.';
24+
} else {
25+
$username = trim($_POST['username']);
26+
}
27+
28+
// Check if password is empty
29+
if (empty(trim($_POST['password']))) {
30+
$password_err = 'Please enter your password.';
31+
} else {
32+
$password = trim($_POST['password']);
33+
}
34+
35+
// Validate credentials
36+
if (empty($username_err) && empty($password_err)) {
37+
// Prepare a select statement
38+
$sql = 'SELECT id, username, password FROM users WHERE username = ?';
39+
40+
if ($stmt = $mysql_db->prepare($sql)) {
41+
42+
// Set parmater
43+
$param_username = $username;
44+
45+
// Bind param to statement
46+
$stmt->bind_param('s', $param_username);
47+
48+
// Attempt to execute
49+
if ($stmt->execute()) {
50+
51+
// Store result
52+
$stmt->store_result();
53+
54+
// Check if username exists. Verify user exists then verify
55+
if ($stmt->num_rows == 1) {
56+
// Bind result into variables
57+
$stmt->bind_result($id, $username, $hashed_password);
58+
59+
if ($stmt->fetch()) {
60+
if (password_verify($password, $hashed_password)) {
61+
62+
// Start a new session
63+
session_start();
64+
65+
// Store data in sessions
66+
$_SESSION['loggedin'] = true;
67+
$_SESSION['id'] = $id;
68+
$_SESSION['username'] = $username;
69+
70+
// Redirect to user to page
71+
header('location: welcome.php');
72+
} else {
73+
// Display an error for passord mismatch
74+
$password_err = 'Invalid password';
75+
}
76+
}
77+
} else {
78+
$username_err = "Username does not exists.";
79+
}
80+
} else {
81+
echo "Oops! Something went wrong please try again";
82+
}
83+
// Close statement
84+
$stmt->close();
85+
}
86+
87+
// Close connection
88+
$mysql_db->close();
89+
}
90+
}
91+
?>
92+
<!DOCTYPE html>
93+
<html lang="en">
94+
95+
<head>
96+
<meta charset="UTF-8">
97+
<title>Sign in</title>
98+
<link href="https://stackpath.bootstrapcdn.com/bootswatch/4.4.1/cosmo/bootstrap.min.css" rel="stylesheet" integrity="sha384-qdQEsAI45WFCO5QwXBelBe1rR9Nwiss4rGEqiszC+9olH1ScrLrMQr1KmDR964uZ" crossorigin="anonymous">
99+
<style>
100+
.wrapper {
101+
width: 500px;
102+
padding: 20px;
103+
}
104+
105+
.wrapper h2 {
106+
text-align: center
107+
}
108+
109+
.wrapper form .form-group span {
110+
color: red;
111+
}
112+
</style>
113+
</head>
114+
115+
<body>
116+
<main>
117+
<section class="container wrapper">
118+
<h2 class="display-4 pt-3">Login</h2>
119+
<p class="text-center">Please fill this form to create an account.</p>
120+
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
121+
<div class="form-group <?php (!empty($username_err)) ? 'has_error' : ''; ?>">
122+
<label for="username">Username</label>
123+
<input type="text" name="username" id="username" class="form-control" value="<?php echo $username ?>">
124+
<span class="help-block"><?php echo $username_err; ?></span>
125+
</div>
126+
127+
<div class="form-group <?php (!empty($password_err)) ? 'has_error' : ''; ?>">
128+
<label for="password">Password</label>
129+
<input type="password" name="password" id="password" class="form-control" value="<?php echo $password ?>">
130+
<span class="help-block"><?php echo $password_err; ?></span>
131+
</div>
132+
133+
<div class="form-group">
134+
<input type="submit" class="btn btn-block btn-outline-primary" value="login">
135+
</div>
136+
<p>Don't have an account? <a href="register.php">Sign in</a>.</p>
137+
</form>
138+
</section>
139+
</main>
140+
</body>
141+
142+
</html>

logout.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
// Start sessions
3+
session_start();
4+
5+
$_SESSION = array();
6+
7+
// Destroy all session related to user
8+
session_destroy();
9+
10+
// Redirect to login page
11+
header('location: login.php');
12+
exit;

password_reset.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
// Initialize the session
3+
session_start();
4+
5+
// Check if the user is logged in, if not then redirect to login page
6+
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
7+
header('location: login.php');
8+
exit;
9+
}
10+
11+
// Include config file
12+
require_once 'config/config.php';
13+
14+
// Define variables and initialize with empty values
15+
$new_password = $confirm_password = '';
16+
$new_password_err = $confirm_password_err = '';
17+
18+
// Processing form data when form is submitted
19+
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
20+
21+
// Validate new password
22+
if (empty(trim($_POST['new_password']))) {
23+
$new_password_err = 'Please enter the new password.';
24+
} elseif (strlen(trim($_POST['new_password'])) < 6) {
25+
$new_password_err = 'Password must have atleast 6 characters.';
26+
} else {
27+
$new_password = trim($_POST['new_password']);
28+
}
29+
30+
// Validate confirm password
31+
if (empty(trim($_POST['confirm_password']))) {
32+
$confirm_password_err = 'Please confirm the password.';
33+
} else {
34+
$confirm_password = trim($_POST['confirm_password']);
35+
if (empty($new_password_err) && ($new_password != $confirm_password)) {
36+
$confirm_password_err = 'Password did not match.';
37+
}
38+
}
39+
40+
// Check input errors before updating the database
41+
if (empty($new_password_err) && empty($confirm_password_err)) {
42+
// Prepare an update statement
43+
$sql = 'UPDATE users SET password = ? WHERE id = ?';
44+
45+
if ($stmt = $mysql_db->prepare($sql)) {
46+
// Set parameters
47+
$param_password = password_hash($new_password, PASSWORD_DEFAULT);
48+
$param_id = $_SESSION["id"];
49+
50+
// Bind variables to the prepared statement as parameters
51+
$stmt->bind_param("si", $param_password, $param_id);
52+
53+
54+
// Attempt to execute the prepared statement
55+
if ($stmt->execute()) {
56+
// Password updated successfully. Destroy the session, and redirect to login page
57+
session_destroy();
58+
header("location: login.php");
59+
exit();
60+
} else {
61+
echo "Oops! Something went wrong. Please try again later.";
62+
}
63+
64+
// Close statement
65+
$stmt->close();
66+
}
67+
68+
// Close connection
69+
$mysql_db->close();
70+
}
71+
}
72+
?>
73+
74+
<!DOCTYPE html>
75+
<html lang="en">
76+
77+
<head>
78+
<meta charset="UTF-8">
79+
<title>Reset Password</title>
80+
<link href="https://stackpath.bootstrapcdn.com/bootswatch/4.4.1/cosmo/bootstrap.min.css" rel="stylesheet" integrity="sha384-qdQEsAI45WFCO5QwXBelBe1rR9Nwiss4rGEqiszC+9olH1ScrLrMQr1KmDR964uZ" crossorigin="anonymous">
81+
<style type="text/css">
82+
.wrapper {
83+
width: 500px;
84+
padding: 20px;
85+
}
86+
87+
.wrapper h2 {
88+
text-align: center
89+
}
90+
91+
.wrapper form .form-group span {
92+
color: red;
93+
}
94+
</style>
95+
</head>
96+
97+
<body>
98+
<main class="container wrapper">
99+
<section>
100+
<h2>Reset Password</h2>
101+
<p>Please fill out this form to reset your password.</p>
102+
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
103+
<div class="form-group <?php echo (!empty($new_password_err)) ? 'has-error' : ''; ?>">
104+
<label>New Password</label>
105+
<input type="password" name="new_password" class="form-control" value="<?php echo $new_password; ?>">
106+
<span class="help-block"><?php echo $new_password_err; ?></span>
107+
</div>
108+
<div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
109+
<label>Confirm Password</label>
110+
<input type="password" name="confirm_password" class="form-control">
111+
<span class="help-block"><?php echo $confirm_password_err; ?></span>
112+
</div>
113+
<div class="form-group">
114+
<input type="submit" class="btn btn-block btn-primary" value="Submit">
115+
<a class="btn btn-block btn-link bg-light" href="welcome.php">Cancel</a>
116+
</div>
117+
</form>
118+
</section>
119+
</main>
120+
</body>
121+
122+
</html>

0 commit comments

Comments
 (0)