[英]How can I make a login system that accepts only two people with sessions in PHP?
如何創建一個只有兩個人在PHP中使用會話的登錄系統?
我想創建一個只有兩個用戶可以登錄的登錄系統。 我真的不想出於自己的原因使用數據庫。真正的問題是網站上有多個登錄,如果你在網站的另一部分登錄另一個帳戶並進入“管理面板”管理員面板將打開,讓任何用戶創建具有管理員權限的新用戶。
我的代碼:
的index.php
<?php
session_start();
define('DS', TRUE);
define('USERNAME', $_SESSION['username']);
define('SELF', $_SERVER['PHP_SELF'] );
$userx = $_SESSION['username'];
if (!USERNAME or isset($_GET['logout']))
include('login/login.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ADMIN PANEL</title>
<meta name="author" content="INTmAker" />
<meta name="author" content="Leonx">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
You are logged in as <?php echo($userx); ?> | <a href="?logout=1">Logout</a>
<br>
<?php
if($userx != "Leonx" || $userx != "INT"){
echo 'You are not an Admin.';
return false;
} else {
echo ' <div>
<input type="text" id="username" name="username" placeholder="Username" value=""/>
<input type="password" id="password" name="password" placeholder="Password" value=""/>
<input type="submit" id="postform" onclick="post()" value="Submit">
</div>
<input type="checkbox" onclick="admin()">Admin access?
<input type="checkbox" onclick="password()">Pokaži lozinku';
}
?>
<script>
function post() {
var username = $('#username').val();
var password = $('#password').val();
$.post('multipass.php', {postuser:username, postpass:password},
function(data)
{
alert("Uspjesno dodan korisnik!");
});
}
function post2() {
var username = $('#username').val();
var password = $('#password').val();
$.post('adminpass.php', {postuser:username, postpass:password},
function(data)
{
alert("Uspjesno dodan admin!");
});
}
function password() {
var x = document.getElementById("password");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
function admin(){
document.getElementById('postform').setAttribute('onclick','post2()');
}
</script>
</body>
</html>
的login.php
<?php defined('DS') OR die('No direct access allowed.');
include('users.php');
if(isset($_GET['logout'])) {
$_SESSION['username'] = '';
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_POST['username'])) {
if($users[$_POST['username']] !== NULL && $users[$_POST['username']] == $_POST['password']) {
$_SESSION['username'] = $_POST['username'];
header('Location: ' . $_SERVER['PHP_SELF']);
}else {
//invalid login
echo "<p>Korisničko ime ili lozinka su krivi!</p>";
}
}
echo '<!DOCTYPE html>
<html>
<head>
<title>EDITOR LOGIN</title>
<link rel="stylesheet" type="text/css" href="login/style.css">
</head>
<body>
<div class="header">
<h2>Admin</h2>
</div>
<form method="post" action="'.SELF.'">
<div class="input-group">
<label for="username">Korisničko ime</label> <input type="text" id="username" name="username" value=""/>
</div>
<div class="input-group">
<label for="password">Lozinka</label> <input type="password" id="password" name="password" value="" />
</div>
<input type="submit" class="btn" name="submit" value="Login" class="button"/>
</form>
</body>
</html>';
exit;
?>
我的期望 :如果登錄用戶的名字不是Leonx或INT,那么我希望用戶只收到一條消息“你不是管理員”。 如果用戶名是Leonx或INT,那么我想要顯示from的輸入字段。
如果你只需要Leonx或INT。 像這樣更改你的過濾器
if($userx != "Leonx" AND $userx != "INT")
你只需要更換你的||
與AND
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.