Code:
<?php
// Starts Session
session_start();
// Checks if they are logged in or not.
if (isset($_SESSION['id'])) {
// Put stored session variables into local PHP variable
$uid = $_SESSION['id'];
$usname = $_SESSION['username'];
// What should it say if they are logged in?
$result = "My Account Details:<br /> Id: ".$uid. <br /> Username: ".$usname;
} else {
// What should it say if they are logged out?
$result = "You are not logged in yet";
}
?>
<!-- Created by TheXenon from Rune-Server.org --!>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- Title of Web Page --!>
<title><?php echo $usname ;?> - MyAccount</title>
</head>
<body>
<!-- show results (if logged in or logged out) --!>
<?php
echo $result;
?>
</body>
</html>
<!-- Created by TheXenon from Rune-Server.org --!>
- Step 7
Code:
<?php
session_start();
if ($_POST['username']) {
// Include the databas connection script
include_once("xenon.php");
// Set the posted data from the form into local variables
$usname = strip_tags($_POST['username']);
$paswd = strip_tags($_POST['password']);
$usname = mysqli_real_escape_string($dbCon, $usname);
$paswd = mysqli_real_escape_string($dbCon, $paswd);
$paswd = md5($paswd); // using md5 just for testing purposes
$sql = "SELECT id, username, password FROM members WHERE username = '$usname'";
$query = mysqli_query($dbCon, $sql);
$row = mysqli_fetch_row($query);
$uid = $row[0];
$dbUsname = $row[1];
$dbPassword = $row[2];
// Check if the username and the password they entered was correct
if ($usname == $dbUsname && $paswd == $dbPassword) {
// Set session
$_SESSION['username'] = $usname;
$_SESSION['id'] = $uid;
// Now direct to users feed
// If login is correct goto account page
header("Location: account.php");
} else {
// Wrong password or username output
echo "<h2>Oops that username or password combination was incorrect.
<br /> Please try again.</h2>";
}
}
?>
<!-- Made by TheXenon from Rune-Server.org --!>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- WebPage Title --!>
<title>The Xenon's PHP Login Tutorial</title>
<style type="text/css">
/* HTML Font */
html {
font-family: Verdana, Geneva, sans-serif;
}
/* H1 CSS */
h1 {
font-size: 24px;
text-align: center;
}
/* Wrapper CSS */
#wrapper {
position: absolute;
width: 100%;
top: 30%;
margin-top: -50px;/* half of #content height*/
}
/* Form CSS */
#form {
margin: auto;
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<div id="wrapper">
<!-- Title --!>
<h1>TheXenon's Tutorial Login</h1>
<!-- Login Form --!>
<form id="form" action="index.php" method="post" enctype="multipart/form-data">
<!-- Username Text Box --!>
Username: <input type="text" name="username" /> <br />
<!-- Password Text Box --!>
Password: <input type="password" name="password" /> <br />
<!-- Login Button --!>
<input type="submit" value="Login" name="Submit" />
</form>
</body>
</html>
<!-- Created by TheXenon from Rune-Server.org --!>
- Step 8
Code:
<?php
$dbCon = mysqli_connect("localhost", "root", "yourpasshere", "members");
?>
- Step 9
Code:
<?php
session_start();
session_destroy();
if (isset($_SESSION['username'])) {
// Logged out text
$msg = "You are now logged out";
} else {
// Error Logging out
$msg = "<h2>Could not log you out</h2>";
}
?>
<!-- Created by TheXenon from Rune-Server.org --!>
<html>
<body>
<?php echo $msg; ?><br>
<!-- Where should we go? --!>
<p><a href="/login-test">Click here</a> to return to our home page </p>
</body>
</html>
<!-- Created by TheXenon from Rune-Server.org --!>
Step 10 -