<!—Login.html --!>
<form action="" method="POST">
<p>Username:</p>
<input type="text" name="username" />
<p>Password</p>
<input type="password" name="password" />
<br />
<input type="submit" name="doLogin" value="Log me in" />
</form>
<?php
// Login.php
session_start(); // Starts the session so we're able to communicate later on.
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'HFLogin';
$conn = mysql_connect($db_host, $db_user, $db_pass); // Sets a connection to a database
mysql_select_db($db_name , $conn); // Selecting a db
// Post variables
$username = trim(strip_slashes($_POST['username'])); // Username variable to later use.
$password = trim(strip_slashes(sha1($_POST['password']))); // Sets a password variable to later use.
// Selects a user from the database where username and password is equal to the inputs.
$getLogin = mysql_query("SELECT username, password, id FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."' LIMIT 1") or die(mysql_error());
// Creates an array of the fetched results from the query.
$thisUser = mysql_fetch_array($getLogin);
// Creates an variable counting the results from the query.
$countResults = mysql_num_rows($getLogin);
// If a result is found then login.. (we can do this since we checked both the password and username in the query)
if($countResults == 1):
// Sets a session (isLoggedIn) equals to true. Now we can check if this session is active on the memberpage.
$_SESSION['isLoggedIn'] = true;
$_SESSION['username'] = $username;
// Redirect to a member page.
header("Location : memberPage.php");
else:
header("Location : login.html");
endif;
?>
<?php
// membersOnly.php
session_start();
// Check if our sessions are inactive
if($_SESSION['isLoggedIn'] != TRUE && ! isset($_SESSION['username'])):
header("Location : login.html");
// If they're active THEN show our members only site.
else:
echo "Hello ". $_SESSION['username'];
echo "<br />";
echo "You are logged in, hurra. Underneath you will see some members only functions.";
echo "<br />";
// Logut link!
echo "<a href=\"logOut.php\">Log me out</a>";
endif;
?>
<?php
// doLogout.php
session_start(); // Starts the sessions to retrieve our earlier sat sessions.
// Unset our username session
unset($_SESSION['username'];
// Unset our isLoggedIn session
unset($_SESSION['isLoggedIn'];
// Destroy all sessions
session_destroy();
// Redirect to our login-page
header("Location : login.html");
?>
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Copyright © 2024, NextGenUpdate.
All Rights Reserved.