Final
This commit is contained in:
RobinNixon
2020-12-10 13:51:13 +00:00
parent 3aeeb4477e
commit 53063593e3
1127 changed files with 0 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
<?php
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
echo "Welcome User: " . htmlspecialchars($_SERVER['PHP_AUTH_USER']) .
" Password: " . htmlspecialchars($_SERVER['PHP_AUTH_PW']);
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die("Please enter your username and password");
}
?>
+19
View File
@@ -0,0 +1,19 @@
<?php
$username = 'admin';
$password = 'letmein';
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
if ($_SERVER['PHP_AUTH_USER'] === $username &&
$_SERVER['PHP_AUTH_PW'] === $password)
echo "You are now logged in";
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
?>
+49
View File
@@ -0,0 +1,49 @@
<?php //setupusers.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "CREATE TABLE users (
forename VARCHAR(32) NOT NULL,
surname VARCHAR(32) NOT NULL,
username VARCHAR(32) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
)";
$result = $pdo->query($query);
$forename = 'Bill';
$surname = 'Smith';
$username = 'bsmith';
$password = 'mysecret';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
$forename = 'Pauline';
$surname = 'Jones';
$username = 'pjones';
$password = 'acrobat';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
function add_user($pdo, $fn, $sn, $un, $pw)
{
$stmt = $pdo->prepare('INSERT INTO users VALUES(?,?,?,?)');
$stmt->bindParam(1, $fn, PDO::PARAM_STR, 32);
$stmt->bindParam(2, $sn, PDO::PARAM_STR, 32);
$stmt->bindParam(3, $un, PDO::PARAM_STR, 32);
$stmt->bindParam(4, $pw, PDO::PARAM_STR, 255);
$stmt->execute([$fn, $sn, $un, $pw]);
}
?>
+46
View File
@@ -0,0 +1,46 @@
<?php // authenticate.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
echo htmlspecialchars("$fn $sn : Hi $fn,
you are now logged in as '$un'");
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+54
View File
@@ -0,0 +1,54 @@
<?php // authenticate2.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
{
session_start();
$_SESSION['forename'] = $fn;
$_SESSION['surname'] = $sn;
echo htmlspecialchars("$fn $sn : Hi $fn,
you are now logged in as '$un'");
die ("<p><a href='continue.php'>Click here to continue</a></p>");
}
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+13
View File
@@ -0,0 +1,13 @@
<?php // continue.php
session_start();
if (isset($_SESSION['forename']))
{
$forename = htmlspecialchars($_SESSION['forename']);
$surname = htmlspecialchars($_SESSION['surname']);
echo "Welcome back $forename.<br>
Your full name is $forename $surname.<br>";
}
else echo "Please <a href='authenticate2.php'>Click Here</a> to log in.";
?>
+9
View File
@@ -0,0 +1,9 @@
<?php
function destroy_session_and_data()
{
session_start();
$_SESSION = array();
setcookie(session_name(), '', time() - 2592000, '/');
session_destroy();
}
?>
+23
View File
@@ -0,0 +1,23 @@
<?php // continue.php = version 2
session_start();
if (isset($_SESSION['forename']))
{
$forename = $_SESSION['forename'];
$surname = $_SESSION['surname'];
destroy_session_and_data();
echo htmlspecialchars("Welcome back $forename");
echo "<br>";
echo htmlspecialchars("Your full name is $forename $surname.");
}
else echo "Please <a href='authenticate.php'>click here</a> to log in.";
function destroy_session_and_data()
{
$_SESSION = array();
setcookie(session_name(), '', time() - 2592000, '/');
session_destroy();
}
?>
+8
View File
@@ -0,0 +1,8 @@
<?php // sessiontest.php
session_start();
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'];
?>
+14
View File
@@ -0,0 +1,14 @@
<?php
session_start();
if (!isset($_SESSION['initiated']))
{
session_regenerate_id();
$_SESSION['initiated'] = 1;
}
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'];
?>
+45
View File
@@ -0,0 +1,45 @@
<?php // authenticate.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
echo htmlspecialchars("$fn $sn : Hi $fn, you are now logged in as '$un'");
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+54
View File
@@ -0,0 +1,54 @@
<?php // authenticate2.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
{
session_start();
$_SESSION['forename'] = $fn;
$_SESSION['surname'] = $sn;
echo htmlspecialchars("$fn $sn : Hi $fn,
you are now logged in as '$un'");
die ("<p><a href='continue.php'>Click here to continue</a></p>");
}
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+13
View File
@@ -0,0 +1,13 @@
<?php // continue.php
session_start();
if (isset($_SESSION['forename']))
{
$forename = htmlspecialchars($_SESSION['forename']);
$surname = htmlspecialchars($_SESSION['surname']);
echo "Welcome back $forename.<br>
Your full name is $forename $surname.<br>";
}
else echo "Please <a href='authenticate2.php'>Click Here</a> to log in.";
?>
+23
View File
@@ -0,0 +1,23 @@
<?php // continue.php = version 2
session_start();
if (isset($_SESSION['forename']))
{
$forename = $_SESSION['forename'];
$surname = $_SESSION['surname'];
destroy_session_and_data();
echo htmlspecialchars("Welcome back $forename");
echo "<br>";
echo htmlspecialchars("Your full name is $forename $surname.");
}
else echo "Please <a href='authenticate.php'>click here</a> to log in.";
function destroy_session_and_data()
{
$_SESSION = array();
setcookie(session_name(), '', time() - 2592000, '/');
session_destroy();
}
?>
+46
View File
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<title>Using JavaScript Cookies</title>
<script>
function SaveCookie(name, value, seconds, path, domain, secure)
{
var date = new Date()
date.setTime(date.getTime() + seconds * 1000)
var expires = seconds ? ';expires=' + date.toGMTString() : ''
path = path ? ';path=' + path : ''
domain = domain ? ';domain=' + domain : ''
secure = secure ? ';secure' : ''
document.cookie = name + '=' + escape(value) + expires + path + domain + secure
}
function ReadCookie(name)
{
var dc = ';' + document.cookie
var start = dc.indexOf(';' + name + '=')
if (start == -1) return false
start += name.length + 1
var end = dc.indexOf(';', start)
end = (end == -1) ? dc.length : end
return unescape(dc.substring(start, end))
}
function DeleteCookie(name)
{
SaveCookie(name, '', -60)
}
</script>
</head>
<body>
<p>The first time this page loads no cookie should have been set and the alert window should show that the cookie with the name <b>test</b> has the value <i>false</i> (meaning it is not set).</p>
<p>After you click OK a value is assigned to the cookie <b>test</b>. To see this new cookie's value click Reload.</p>
<script>
alert("The value of the cookie 'test' is: " + ReadCookie('test'))
SaveCookie('test', 'I love cookies')
</script>
</body>
</html>
+14
View File
@@ -0,0 +1,14 @@
<?php // login.php
$host = 'localhost';
$data = 'publications';
$user = 'root'; // Change as necessary
$pass = 'mysql'; // Change as necessary
$chrs = 'utf8mb4';
$attr = "mysql:host=$host;dbname=$data;charset=$chrs";
$opts =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
?>
+16
View File
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Using Cookies</title>
</head>
<body>
<p>The first time this page loads no cookie should have been set and the message below should show that the cookie with the name <b>test</b> has the value <i>false</i> (meaning it is not set).</p>
<p>But then a value is assigned to the cookie <b>test</b>. To see this new cookie's value click Reload.</p>
<?php
$test = 'false';
if (isset($_COOKIE['test'])) $test = $_COOKIE['test'];
echo "<p><b>The value of the cookie 'test' is: $test</b></p>";
setcookie('test', 'I love cookies');
?>
</body>
</html>
+8
View File
@@ -0,0 +1,8 @@
<?php // sessiontest.php
session_start();
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'];
?>
+49
View File
@@ -0,0 +1,49 @@
<?php //setupusers.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "CREATE TABLE users (
forename VARCHAR(32) NOT NULL,
surname VARCHAR(32) NOT NULL,
username VARCHAR(32) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
)";
$result = $pdo->query($query);
$forename = 'Bill';
$surname = 'Smith';
$username = 'bsmith';
$password = 'mysecret';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
$forename = 'Pauline';
$surname = 'Jones';
$username = 'pjones';
$password = 'acrobat';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
function add_user($pdo, $fn, $sn, $un, $pw)
{
$stmt = $pdo->prepare('INSERT INTO users VALUES(?,?,?,?)');
$stmt->bindParam(1, $fn, PDO::PARAM_STR, 32);
$stmt->bindParam(2, $sn, PDO::PARAM_STR, 32);
$stmt->bindParam(3, $un, PDO::PARAM_STR, 32);
$stmt->bindParam(4, $pw, PDO::PARAM_STR, 255);
$stmt->execute([$fn, $sn, $un, $pw]);
}
?>