How do I create an easy login script that does not require a database. I would like it to be safe.
Alright, what about this script, i just made it by my knowledge in
Try this:
<?php
session_start();
$userinfo = array(
'user'=>'5d41402abc4b2a76b9719d911017c592', //Hello...
);
if(isset($_GET['logout'])) {
$_SESSION['username'] = '';
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_POST['username'])) {
if($userinfo[$_POST['username']] == md5($_POST['password'])) {
$_SESSION['username'] = $_POST['username'];
}else {
header("location:403.html"); //replace with 403
}
}
?>
<?php if($_SESSION['username']): ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Logged In</title>
</head>
<body>
<p>You're logged in.</p>
<a href="logout.php">LOG OUT</a>
</body>
</html>
<?php else: ?>
<html>
<head>
<title>Log In</title>
</head>
<body>
<h1>Login needed</h1>
<form name="login" action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>System Login</strong></td>
</tr>
<tr>
<td width="78">Username:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
<?php endif; ?>
You will need a logout, something like this (logout.php):
<?php
session_start();
session_destroy();
header("location:index.html"); //Replace with Logged Out page. Remove if you want to use HTML in same file.
?>
// Below is not needed, unless header above is missing. In that case, put logged out text here.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<!-- Put logged out message here -->
</body>
</html>
There's no reason for not using database for login implementation, the very least you can do is to download and install SQLite if your hosting company does not provide you with enough DB.
You can do the access control at the Web server level using HTTP Basic authentication and htpasswd. There are a number of problems with this:
Unless you're building a site for internal use with few users, I wouldn't really recommend it.
It's not an ideal solution but here's a quick and dirty example that shows how you could store login info in the PHP code:
<?php
session_start();
$userinfo = array(
'user1'=>'password1',
'user2'=>'password2'
);
if(isset($_GET['logout'])) {
$_SESSION['username'] = '';
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_POST['username'])) {
if($userinfo[$_POST['username']] == $_POST['password']) {
$_SESSION['username'] = $_POST['username'];
}else {
//Invalid Login
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
</head>
<body>
<?php if($_SESSION['username']): ?>
<p>You are logged in as <?=$_SESSION['username']?></p>
<p><a href="?logout=1">Logout</a></p>
<?php endif; ?>
<form name="login" action="" method="post">
Username: <input type="text" name="username" value="" /><br />
Password: <input type="password" name="password" value="" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
if you dont have a database, you will have to hardcode the login details in your code, or read it from a flat file on disk.
Save the username and password hashes in array in a php file instead of db.
When you need to authenticate the user, compute hashes of his credentials and then compare them to hashes in array.
If you use safe hash function (see hash function and hash algos in PHP documentation), it should be pretty safe (you may consider using salted hash) and also add some protections to the form itself.