Skip to content

Commit ab548dc

Browse files
author
devjewel01
committed
update
1 parent 502b538 commit ab548dc

File tree

144 files changed

+70877
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+70877
-0
lines changed

Extra/indexOld.php

+983
Large diffs are not rendered by default.

Extra/login 2

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit ac37c8edff91c36d29678ce524054cc4c2f882fa

Extra/loging/config.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/* Database credentials. Assuming you are running MySQL
3+
server with default setting (user 'root' with no password) */
4+
define('DB_SERVER', 'localhost');
5+
define('DB_USERNAME', 'root');
6+
define('DB_PASSWORD', '');
7+
define('DB_NAME', 'tuitionPoint');
8+
9+
/* Attempt to connect to MySQL database */
10+
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
11+
12+
// Check connection
13+
if($link === false){
14+
die("ERROR: Could not connect. " . mysqli_connect_error());
15+
}
16+
?>

Extra/loging/db.sql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE users (
2+
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
3+
username VARCHAR(50) NOT NULL UNIQUE,
4+
password VARCHAR(255) NOT NULL,
5+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
6+
);

Extra/loging/index.php

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
// Include config file
3+
require_once "config.php";
4+
5+
// Define variables and initialize with empty values
6+
$username = $password = $confirm_password = "";
7+
$username_err = $password_err = $confirm_password_err = "";
8+
9+
// Processing form data when form is submitted
10+
if ($_SERVER["REQUEST_METHOD"] == "POST")
11+
{
12+
13+
// Validate username
14+
if (empty(trim($_POST["username"]))) {
15+
$username_err = "Please enter a username.";
16+
} elseif (!preg_match('/^[a-zA-Z0-9_]+$/', trim($_POST["username"]))) {
17+
$username_err = "Username can only contain letters, numbers, and underscores.";
18+
} else {
19+
// Prepare a select statement
20+
$sql = "SELECT id FROM users WHERE username = ?";
21+
22+
if ($stmt = mysqli_prepare($link, $sql)) {
23+
// Bind variables to the prepared statement as parameters
24+
mysqli_stmt_bind_param($stmt, "s", $param_username);
25+
26+
// Set parameters
27+
$param_username = trim($_POST["username"]);
28+
29+
// Attempt to execute the prepared statement
30+
if (mysqli_stmt_execute($stmt)) {
31+
/* store result */
32+
mysqli_stmt_store_result($stmt);
33+
34+
if (mysqli_stmt_num_rows($stmt) == 1) {
35+
$username_err = "This username is already taken.";
36+
} else {
37+
$username = trim($_POST["username"]);
38+
}
39+
} else {
40+
echo "Oops! Something went wrong. Please try again later.";
41+
}
42+
43+
// Close statement
44+
mysqli_stmt_close($stmt);
45+
}
46+
}
47+
48+
// Validate password
49+
if (empty(trim($_POST["password"]))) {
50+
$password_err = "Please enter a password.";
51+
} elseif (strlen(trim($_POST["password"])) < 6) {
52+
$password_err = "Password must have atleast 6 characters.";
53+
} else {
54+
$password = trim($_POST["password"]);
55+
}
56+
57+
// Validate confirm password
58+
if (empty(trim($_POST["confirm_password"]))) {
59+
$confirm_password_err = "Please confirm password.";
60+
} else {
61+
$confirm_password = trim($_POST["confirm_password"]);
62+
if (empty($password_err) && ($password != $confirm_password)) {
63+
$confirm_password_err = "Password did not match.";
64+
}
65+
}
66+
67+
// Check input errors before inserting in database
68+
if (empty($username_err) && empty($password_err) && empty($confirm_password_err)) {
69+
70+
// Prepare an insert statement
71+
$sql = "INSERT INTO users (username, password) VALUES (?, ?)";
72+
73+
if ($stmt = mysqli_prepare($link, $sql)) {
74+
// Bind variables to the prepared statement as parameters
75+
mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
76+
77+
// Set parameters
78+
$param_username = $username;
79+
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
80+
81+
// Attempt to execute the prepared statement
82+
if (mysqli_stmt_execute($stmt)) {
83+
// Redirect to login page
84+
header("location: login.php");
85+
} else {
86+
echo "Oops! Something went wrong. Please try again later.";
87+
}
88+
89+
// Close statement
90+
mysqli_stmt_close($stmt);
91+
}
92+
}
93+
94+
// Close connection
95+
mysqli_close($link);
96+
}
97+
?>
98+
99+
<!DOCTYPE html>
100+
<html lang="en">
101+
102+
<head>
103+
<meta charset="UTF-8">
104+
<title>Sign Up</title>
105+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
106+
<style>
107+
body {
108+
font: 14px sans-serif;
109+
}
110+
111+
.wrapper {
112+
width: 360px;
113+
padding: 20px;
114+
}
115+
</style>
116+
</head>
117+
118+
<body>
119+
<div class="wrapper">
120+
<h2>Sign Up</h2>
121+
<p>Please fill this form to create an account.</p>
122+
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
123+
<div class="form-group">
124+
<label>Username</label>
125+
<input type="text" name="username" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username; ?>">
126+
<span class="invalid-feedback"><?php echo $username_err; ?></span>
127+
</div>
128+
<div class="form-group">
129+
<label>Password</label>
130+
<input type="password" name="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $password; ?>">
131+
<span class="invalid-feedback"><?php echo $password_err; ?></span>
132+
</div>
133+
<div class="form-group">
134+
<label>Confirm Password</label>
135+
<input type="password" name="confirm_password" class="form-control <?php echo (!empty($confirm_password_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $confirm_password; ?>">
136+
<span class="invalid-feedback"><?php echo $confirm_password_err; ?></span>
137+
</div>
138+
<div class="form-group">
139+
<input type="submit" class="btn btn-primary" value="Submit">
140+
<input type="reset" class="btn btn-secondary ml-2" value="Reset">
141+
</div>
142+
<p>Already have an account? <a href="login.php">Login here</a>.</p>
143+
</form>
144+
</div>
145+
</body>
146+
147+
</html>

Extra/loging/login.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
// Initialize the session
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.php";
13+
14+
// Define variables and initialize with empty values
15+
$username = $password = "";
16+
$username_err = $password_err = $login_err = "";
17+
18+
// Processing form data when form is submitted
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 = mysqli_prepare($link, $sql)){
41+
// Bind variables to the prepared statement as parameters
42+
mysqli_stmt_bind_param($stmt, "s", $param_username);
43+
44+
// Set parameters
45+
$param_username = $username;
46+
47+
// Attempt to execute the prepared statement
48+
if(mysqli_stmt_execute($stmt)){
49+
// Store result
50+
mysqli_stmt_store_result($stmt);
51+
52+
// Check if username exists, if yes then verify password
53+
if(mysqli_stmt_num_rows($stmt) == 1){
54+
// Bind result variables
55+
mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
56+
if(mysqli_stmt_fetch($stmt)){
57+
if(password_verify($password, $hashed_password)){
58+
// Password is correct, so start a new session
59+
session_start();
60+
61+
// Store data in session variables
62+
$_SESSION["loggedin"] = true;
63+
$_SESSION["id"] = $id;
64+
$_SESSION["username"] = $username;
65+
66+
// Redirect user to welcome page
67+
header("location: welcome.php");
68+
} else{
69+
// Password is not valid, display a generic error message
70+
$login_err = "Invalid username or password.";
71+
}
72+
}
73+
} else{
74+
// Username doesn't exist, display a generic error message
75+
$login_err = "Invalid username or password.";
76+
}
77+
} else{
78+
echo "Oops! Something went wrong. Please try again later.";
79+
}
80+
81+
// Close statement
82+
mysqli_stmt_close($stmt);
83+
}
84+
}
85+
86+
// Close connection
87+
mysqli_close($link);
88+
}
89+
?>
90+
91+
<!DOCTYPE html>
92+
<html lang="en">
93+
<head>
94+
<meta charset="UTF-8">
95+
<title>Login</title>
96+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
97+
<style>
98+
body{ font: 14px sans-serif; }
99+
.wrapper{ width: 360px; padding: 20px; }
100+
</style>
101+
</head>
102+
<body>
103+
<div class="wrapper">
104+
<h2>Login</h2>
105+
<p>Please fill in your credentials to login.</p>
106+
107+
<?php
108+
if(!empty($login_err)){
109+
echo '<div class="alert alert-danger">' . $login_err . '</div>';
110+
}
111+
?>
112+
113+
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
114+
<div class="form-group">
115+
<label>Username</label>
116+
<input type="text" name="username" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username; ?>">
117+
<span class="invalid-feedback"><?php echo $username_err; ?></span>
118+
</div>
119+
<div class="form-group">
120+
<label>Password</label>
121+
<input type="password" name="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>">
122+
<span class="invalid-feedback"><?php echo $password_err; ?></span>
123+
</div>
124+
<div class="form-group">
125+
<input type="submit" class="btn btn-primary" value="Login">
126+
</div>
127+
<p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
128+
</form>
129+
</div>
130+
</body>
131+
</html>

Extra/loging/logout.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
// Initialize the session
3+
session_start();
4+
5+
// Unset all of the session variables
6+
$_SESSION = array();
7+
8+
// Destroy the session.
9+
session_destroy();
10+
11+
// Redirect to login page
12+
header("location: login.php");
13+
exit;
14+
?>

Extra/loging/welcome.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
// Initialize the session
3+
session_start();
4+
5+
// Check if the user is logged in, if not then redirect him to login page
6+
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
7+
header("location: login.php");
8+
exit;
9+
}
10+
?>

Extra/sign/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)