Here are your files.
onetime_pass.php
[php]<?php
// A Script by Tanzeel Niazi
// For More Information or Help
// Visit www.phphelp.com
// You can also email me at [email protected]
ob_start(); // Using this function will prevent "can't modify headers" error.
session_start();
// Ensuring data is received through POST and fields are not empty.
if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
!empty($_POST['student_name']) &&
!empty($_POST['password'])
){
require_once('functions.inc.php');
/* Sanitizing the data by calling "sanitizeData" function found in "functions.inc.php" file. */
$student_name = sanitizeData($_POST['student_name']);
$password = sanitizeData($_POST['password']);
$query = "SELECT * FROM students
WHERE name = '{$student_name}' AND
password = '{$password}'
LIMIT 1
";
$result = mysqli_query($conn, $query);
/* The following IF stament will execute if only one row is returned back by the query result */
if ($result && mysqli_affected_rows($conn) == 1){
$student = mysqli_fetch_assoc($result);
/* If login cout is less than 5 following IF Statment will run, hence updating the record too in the database */
if ($student['login_count'] < 5){
$query = "UPDATE students
SET login_count = login_count + 1
WHERE name = '{$student['name']}'
LIMIT 1
";
$result = mysqli_query($conn, $query);
/*If the login count gets updated in the DB, one row will get affected. Therefore the following IF statment will execute. */
if ($result && mysqli_affected_rows($conn) == 1){
$_SESSION['name'] = $student['name'];
redirectTo('result.php');
}
// If login count is greater than 5 then this else block will execute.
} else {
$_SESSION['message'] = 'You have completed Your five logins, you cant login now';
}
} else {
$_SESSION['message'] = 'Invalid Login, Please Try again';
}
}
?>
Login Page
<?php
// If Session variable is set, following will run and then it will unset itself too.
if (isset($_SESSION['message'])){
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
<p>
<label for="student_name">Name:</label>
<input type="text" name="student_name" id="student_name" required>
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" id="password" required>
</p>
<p><input type="submit" name="submit" value="Log In"></p>
<?php
//Closing the database connection (if any) and flushing the output buffer.
if (isset($conn))
{mysqli_close($conn);}
ob_end_flush();
?>[/php]
db_connect.inc.php
[php]<?php
/* Change these values according to your database details */
define('DB_HOST', 'localhost');
define('DB_USER', 'tanzeelniazi');
define('DB_PASS', 'abc');
define('DB_INFO', 'phphelp');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_INFO);
/* If any error is found, script will die, giving user information about the error. */
if (mysqli_connect_errno()){
die (mysqli_connect_error());
}
?>[/php]
functions.inc.php
[php]<?php
require_once (‘db_connect.inc.php’);
// Use this function to redirect user (student) to a new location.
function redirectTo($new_location){
header('Location: ' . $new_location);
exit();
}
// Sanitizing Bad Input data by the users.
function sanitizeData($string){
global $conn; // This variable is coming from "db_connect.inc.php" file.
$string = strip_tags($string);
$string = trim($string);
$string = htmlspecialchars($string);
$string = mysqli_real_escape_string($conn, $string);
return $string;
}
?>[/php]
queries.sql
CREATE TABLE students(
id INT(11) AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
password VARCHAR(60) NOT NULL,
login_count INT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
result.php
[php]<?php session_start(); ?>
<?
if (!isset($_SESSION['name'])){
header('Location: onetime_pass.php');
exit();
}
?>
Result Page
Welcome <?php echo $_SESSION['name']; ?> To Your Result Page
[/php]
Alternatively You can also download all these files as a single ZIP. Please see the attachment. I hope It will make sense now. 8)
onetime_pass.zip (2.86 KB)