我对php很陌生,我想知道这样做是否安全.
我现在有一个登录系统来保护几个页面。
文件:
代码:
not_logged_in.php:
<html>
You are not logged in!
</html>
test.php:
<?php
$logged_in = false;
function protect_page() {
if($logged_in == false) {
header('Location: index.php');
exit();
}
}
?>
login.php:
<?php
include "test.php";
$logged_in = true;
?>
logout.php:
<?php
include "test.php";
$logged_in = false;
?>
protected_page_1.php:
<?php
include "test.php";
protect_page();
?>
<html>
Content
</html>
protected_page_2:
<?php
include "test.php";
protect_page();
?>
<html>
Content
</html>
unprotected_page_1:
<html>
Content
</html>
我完全理解login.php页面只是登录,您不需要提供密码,但这只是目前的测试.
感谢您的阅读!
发布于 2017-08-15 14:19:36
我认为使用这个$logged_in变量的方法太松散了。
我建议你利用会议时间。
session.php:
<?php
session_start(); // start on top of your page before any output
if(!isset($_SESSION['loggedin'])) {
$_SESSION['loggedin'] = false;
}
function loggedin()
{
return $_SESSION['loggedin'];
}
?>
在任何有保护内容的页面中。
<?php
include 'session.php';
if(!logged_in()) {
include 'login.php';
exit();
}
// some info
?>
login.php将有一个表单可以登录。(对于$_SESSION['loggedin'] = true;
,每个页面都可以包括session.php。
发布于 2017-08-15 13:52:08
是的,它被保护了。也许您可以将显示用户是否已被记录的变量存储在会话存储中,从而使其更加高效。
https://stackoverflow.com/questions/45694250
复制相似问题