使用会话和多个用户创建一个简单的 PHP 登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23631184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a simple PHP login with sessions and multiple users
提问by DexterTheCat
I am searching for a answer how to create a $_SESSION login.
我正在寻找如何创建 $_SESSION 登录名的答案。
The result I get every time is Dexter. Even when I just press the login button. I am using sessions and no MySQL or other database. I am just in the beginning to learn PHP and have looked around here and used google but I can't relate was I am doing wrong.
我每次得到的结果都是德克斯特。即使我只是按下登录按钮。我正在使用会话,但没有使用 MySQL 或其他数据库。我刚刚开始学习 PHP,在这里环顾四周并使用了谷歌,但我无法理解是我做错了。
The login page looks like this:
登录页面如下所示:
<?php
session_start();
$_SESSION['usernamne1'] = "Dexter";
$_SESSION['usernamne2'] = "River";
$_SESSION['usernamne3'] = "Miro";
$_SESSION['password1'] = "meow1";
$_SESSION['password2'] = "meow2";
$_SESSION['password3'] = "meow3";
?>
<?php //Header include fil
$page_title = 'Login'; //Dynamic titel
include('includes/header.html');
?>
<?php
echo "<h3>Login!</h3>";
echo "<br />";
?>
<form method="post" Action="sida7logged.php">
<fieldset><legend>Fyll i dina anv?ndaruppgifter</legend>
<p><label>Username: <br />
<input name="usernamne" type="text"></label></p>
<p><label>Password: <br />
<input name="password" type="password"></label></p>
<input type="Submit" value="Login">
</fieldset>
</form>
<?php //Footer include file
include('includes/footer.html');
?>
And when logged in:
并在登录时:
<?php
$page_title = 'Logged in'; //Dynamisc title
include('includes/header.html');
?>
<?php
session_start();
if($_SESSION['usernamne1']==true || ($_POST['username']=="Dexter"
&& ($_SESSION['password1']==true || $_POST['password']="meow1"))) {
$_SESSION['usernamne1']=true;
echo "Hello Dexter";
}
elseif($_SESSION['usernamne2']==true || ($_POST['username']=="River"
&& ($_SESSION['password2']==true || $_POST['password']="meow2"))) {
$_SESSION['usernamne2']=true;
echo "Hello River";
}
elseif($_SESSION['usernamne3']==true || ($_POST['username']=="Miro"
&& ($_SESSION['password3']==true || $_POST['password']="meow3"))) {
$_SESSION['usernamne1']=true;
echo "Hello Miro";
}
else {
echo "Please login";
}
?>
<?php //Footer include file
include('includes/footer.html');
?>
回答by Agli Panci
You have a better example here: Easy login script without database
这里有一个更好的例子:Easy login script without database
<?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>