gpt4 book ai didi

php - 没有数据库的简单登录脚本

转载 作者:可可西里 更新时间:2023-10-31 22:52:44 24 4
gpt4 key购买 nike

如何创建不需要数据库的简单登录脚本。我希望它是安全的。

好吧,这个脚本呢,我只是用我的 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 包含这个和一些内容:

session_start();

if (!isset($_SESSION["inloggning"]) || $_SESSION["inloggning"] !== true) {
header("Location: login.php");
exit;
}

最佳答案

这不是一个理想的解决方案,但这里有一个简单粗暴的示例,展示了如何在 PHP 代码中存储登录信息:

<?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>

关于php - 没有数据库的简单登录脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1381205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com