无需数据库的简单登录脚本 [英] Easy login script without database
本文介绍了无需数据库的简单登录脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建不需要数据库的简单登录脚本.我希望它是安全的.
How do I create an easy login script that does not require a database. I would like it to be safe.
好吧,这个脚本呢,我只是用我在php中的知识就做到了.
Alright, what about this script, i just made it by my knowledge in php.
<?php
// Start session
session_start();
// Username and password
$ID = "admin";
$pass = "123456";
if (isset($_POST["ID"]) && isset($_POST["pass"])) {
if ($_POST["ID"] === $anvandarID && $_POST["pass"] === $pass) {
/
$_SESSION["inloggedin"] = true;
header("Location: safe_site.php");
exit;
}
// Wrong login - message
else {$wrong = "Bad ID and password, the system could not log you in";}
}
?>
safe_site.php包含以下内容:
The safe_site.php contains this and some content:
session_start();
if (!isset($_SESSION["inloggning"]) || $_SESSION["inloggning"] !== true) {
header("Location: login.php");
exit;
}
推荐答案
这不是理想的解决方案,但这是一个简单又肮脏的示例,展示了如何在PHP代码中存储登录信息:
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>
这篇关于无需数据库的简单登录脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文