Hi Friends,
Today I would like to say how to create a login system using PHP, without using Databases.
We need these files:
1. index.php - Index file, where your protected content and login form is there.
2. login.php - Login Form.
3. logout.php - Script for logging out.
Code: index.php
PHP Code:
<?php
session_start(); // Initiate the session
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome</title>
<style type="text/css">
* {font-family: Tahoma; font-size: 12px;}
a {color: #0000ff; text-decoration: none;}
a:hover {color: #ff0000;}
</style>
</head>
<body>
<?php
if(isset($_SESSION["logged"])) // Check if the user had logged in.
echo 'Welcome! <a href="logout.php">Logout</a>.';
else // User did not log in.
echo 'Please <a href="login.php">Login</a>.';
?>
</body>
</html>
Code: login.php
PHP Code:
<?php
session_start(); // Initiate the session.
$err = false; // Set an Error Flag.
if(count($_POST))
if(isset($_POST["username"], $_POST["password"])) // See if the user has entered both username and password.
if($_POST["username"]=="Admin" && $_POST["password"]=="LetMeIn") // Check for the correctness of both username and password.
{
$_SESSION["logged"] = true; // Set the session as authenticated.
header('Location: index.php'); // Redirect the user to the home page.
die(); // Stop the script.
}
else
$err = true; // Username or password is wrong! Set error flag.
else
$err = true; // User didn't provide both username and password.
else
$err = false; // User logs in for the first time.
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<style type="text/css">
* {font-family: Tahoma; font-size: 12px;}
label {display: inline-block; width: 75px; margin: 5px 0px; padding: 5px;}
input {border: 0px; border-bottom: 1px solid #cccccc; width: 100px; margin: 5px 0px; padding: 3px;}
form {text-align: center;}
</style>
</head>
<body>
<?php if($err) echo '<strong>Invalid Login! Provide all values!</strong><br />'; // If the error flag is set, display error message. ?>
<form method="post">
<div><label for="username">Username:</label> <input type="text" name="username" /></div>
<div><label for="password">Password:</label> <input type="password" name="password" /></div>
<div><input type="submit" value="Login!" /></div>
</form>
</body>
</html>
Code: index.php
PHP Code:
<?php
session_start(); // Initiate the session
session_destroy(); // Destroy the session
header('Location: index.php'); // Redirect the user to the home page
?>
Now keep all the files in the same folder... Ready to execute?
Execution I personally recommend USB Web Server for fast food execution. Download it at
Usbwebserver V8 Now copy the three files in the
Root folder of USB Web Server and open the port
http://127.0.0.1:8080/ in your favourite browser and enjoy. I have also attached the set of files, which I used with this post. :)
All the coding is documented. Even then if you have any queries, do shoot them out in the replies. :)
Download the files:
Attachment 48
No comments:
Post a Comment