Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13b54bf

Browse files
author
lisawilliams
committed
Fixed errors in login_db
PHP block was missing a final ?> and "trim" function shouldn't have a $ before it
1 parent e725f6d commit 13b54bf

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
require_once('../ch16/connection.inc.php');
3+
$conn = dbConnect('read');
4+
// create key
5+
$key = 'takeThisWith@PinchOfSalt';
6+
$sql = 'SELECT username FROM users_2way
7+
WHERE username = ? AND pwd = AES_ENCRYPT(?, ?)';
8+
// initialize and prepare statement
9+
$stmt = $conn->stmt_init();
10+
$stmt->prepare($sql);
11+
// bind the input parameters
12+
$stmt->bind_param('sss', $username, $password, $key);
13+
$stmt->execute();
14+
// store the result
15+
$stmt->store_result();
16+
// if a match is found, num_rows is 1, which is treated as true
17+
if ($stmt->num_rows) {
18+
$_SESSION['authenticated'] = 'Jethro Tull';
19+
// get the time the session started
20+
$_SESSION['start'] = time();
21+
session_regenerate_id();
22+
header("Location: $redirect");
23+
exit;
24+
} else {
25+
// if no match, prepare error message
26+
$error = 'Invalid username or password';
27+
}

authenticate/login_db.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
<?php
2+
$error = '';
3+
if (isset($_POST['login'])) {
4+
session_start();
5+
$username = trim($_POST['username']);
6+
$password = trim($_POST['pwd']);
7+
// location to redirect on success
8+
$redirect = 'http://localhost/phpsols/authenticate/menu_db.php';
9+
require_once('../includes/authenticate_mysqli.inc.php');
10+
}
11+
?>
112
<!DOCTYPE HTML>
213
<html>
314
<head>
415
<meta charset="utf-8">
516
<title>Login</title>
617
</head>
7-
818
<body>
919
<?php
1020
if ($error) {

includes/register_2way_mysqli.inc.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
require_once('../classes/Ps2/CheckPassword.php');
3+
$usernameMinChars = 6;
4+
$errors = array();
5+
if (strlen($username) < $usernameMinChars) {
6+
$errors[] = "Username must be at least $usernameMinChars characters.";
7+
}
8+
if (preg_match('/\s/', $username)) {
9+
$errors[] = 'Username should not contain spaces.';
10+
}
11+
$checkPwd = new Ps2_CheckPassword($password, 10);
12+
$checkPwd->requireMixedCase();
13+
$checkPwd->requireNumbers(2);
14+
$checkPwd->requireSymbols();
15+
$passwordOK = $checkPwd->check();
16+
if (!$passwordOK) {
17+
$errors = array_merge($errors, $checkPwd->getErrors());
18+
}
19+
if ($password != $retyped) {
20+
$errors[] = "Your passwords don't match.";
21+
}
22+
if (!$errors) {
23+
// include the connection file
24+
require_once('connection.inc.php');
25+
$conn = dbConnect('write');
26+
// create a key
27+
$key = 'takeThisWith@PinchOfSalt';
28+
// prepare SQL statement
29+
$sql = 'INSERT INTO users_2way (username, pwd)
30+
VALUES (?, AES_ENCRYPT(?, ?))';
31+
$stmt = $conn->stmt_init();
32+
$stmt = $conn->prepare($sql);
33+
// bind parameters and insert the details into the database
34+
$stmt->bind_param('sss', $username, $password, $key);
35+
$stmt->execute();
36+
if ($stmt->affected_rows == 1) {
37+
$success = "$username has been registered. You may now log in.";
38+
} elseif ($stmt->errno == 1062) {
39+
$errors[] = "$username is already in use. Please choose another username.";
40+
} else {
41+
$errors[] = 'Sorry, there was a problem with the database.';
42+
}
43+
}

0 commit comments

Comments
 (0)