User authentication setup - first test
This commit is contained in:
28
var/www/login.php
Normal file
28
var/www/login.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||||
|
<head>
|
||||||
|
<title>FriendBook Login</title>
|
||||||
|
<meta http-equiv="content-type"
|
||||||
|
content="text/html; charset=utf-8"/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: #3B5998;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<div class="message"><?php if($_SESSION["loginError"]!="") { echo $_SESSION["loginError"]; } ?></div>
|
||||||
|
<h2><font color="white">Please Login</h2>
|
||||||
|
<form action="loginCheck.php" method="post">
|
||||||
|
User:
|
||||||
|
<input type="text" name="username" placeholder="Username"><br>
|
||||||
|
Password:
|
||||||
|
<input type="password" name="password" placeholder="Password"><br>
|
||||||
|
<input type="submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
39
var/www/loginCheck.php
Normal file
39
var/www/loginCheck.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
$server = "localhost";
|
||||||
|
$database = "friendBook";
|
||||||
|
$username = "web";
|
||||||
|
$password = "Password456";
|
||||||
|
|
||||||
|
try{
|
||||||
|
$connection = new PDO("mysql:host=$server;dbname=$database", $username, $password);
|
||||||
|
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
echo "Connected successfully";
|
||||||
|
if(count($_POST) > 0){
|
||||||
|
$matchingUsers = $connection->query("SELECT COUNT(*) FROM login WHERE username = '".$_POST["Username"]."' and pword = '".$_POST["Password"]."'");
|
||||||
|
if($matchingUsers > 0){
|
||||||
|
// User has been authenticated; set user as logged in
|
||||||
|
$_SESSION["loggedInUser"] = "".$_POST["Username"]."";
|
||||||
|
$_SESSION["loginError"] = "";
|
||||||
|
// Move onto landing page
|
||||||
|
header("Location: /landingPage.html");
|
||||||
|
die();
|
||||||
|
}else{
|
||||||
|
// No matching users found, send user error
|
||||||
|
$_SESSION["loginError"] = "Invalid Username or Password";
|
||||||
|
// Return to login page
|
||||||
|
header("Location: /login.php");
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
// Return to login page, as credentials were not captured
|
||||||
|
$_SESSION["loginError"] = "Login credentials not found, please try again";
|
||||||
|
header("Location: /login.php");
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(PDOException $e){
|
||||||
|
echo "Error: " . $e->getMessage();
|
||||||
|
}
|
||||||
|
?>
|
||||||
27
var/www/logout.php
Normal file
27
var/www/logout.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||||
|
<head>
|
||||||
|
<title>FriendBook Logout</title>
|
||||||
|
<meta http-equiv="content-type"
|
||||||
|
content="text/html; charset=utf-8"/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
session_unset(); // remove all session variables
|
||||||
|
session_destroy(); // destroy the session
|
||||||
|
?>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: #3B5998;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<h2><font color="white">You have been successfully logged out.</h2>
|
||||||
|
<button type="button" onclick="window.location.href = 'friends.html'">Login again? (FIXME)</button>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
session_start();
|
||||||
|
|
||||||
$server = "localhost";
|
$server = "localhost";
|
||||||
$database = "friendBook";
|
$database = "friendBook";
|
||||||
$username = "web";
|
$username = "web";
|
||||||
@@ -12,7 +14,9 @@ $user = "William"; // Testing with until we have login working
|
|||||||
<head>
|
<head>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>PHP Test Page - Connecting to MySQL</h1>
|
<h1>PHP Test Page</h1>
|
||||||
|
|
||||||
|
<h2>Connecting to MySQL</h2>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
try{
|
try{
|
||||||
@@ -34,8 +38,12 @@ $user = "William"; // Testing with until we have login working
|
|||||||
print $row['message'] . "\t";
|
print $row['message'] . "\t";
|
||||||
}
|
}
|
||||||
|
|
||||||
mysqli_close($connection);
|
$conn = null;
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
<h2>PHP Session Info</h2>
|
||||||
|
<?php
|
||||||
|
print_r($_SESSION);
|
||||||
|
?>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user