我对会话很不安全。我正在制作一个用户可以登录的网站。我的所有页面,无论您是否登录,都在调用标题.html。所以在我的index.php
上,每个人都可以看到我有以下代码:
**index.php**
<?php
if( !isset( $_SESSION['username'])) include('resources/auth/login.php');
else exit( header('Location: home.php') );
if( !isset( $_SESSION ) ) session_start();
if( isset( $_GET['todo'] ) && $_GET['todo'] == 'logout'){
session_unset();
session_destroy();
//echo 'You have been logged out!';
}
?>
<?php include 'resources/includes/header.html';?>
<!-- A lot of code -->
<?php include 'resources/includes/footer.html';?>
会话代码在我的标头之前,我被重定向到 home.php
.我应该在标题中包含该会话代码吗?
首页.php
<?php
if( !isset( $_SESSION ) ) session_start();
?>
<?php include 'resources/includes/header.html';?>
<!-- A lot of code -->
<?php include 'resources/includes/footer.html';?>
所以我今天早些时候才想到,我实际上在我的身体里包括一个会话?因为在我的标题中.html我没有任何会话。那么我应该在我的标题中.html会话吗?万一我怎样才能以最聪明的方式做到这一点?
在
尝试访问任何$_SESSION
变量之前,必须始终运行session_start()
。
因此,最安全的编码方法是始终在脚本中的第一个<?php
之后添加它。
索引.php
<?php
session_start();
if( !isset( $_SESSION['username'])) include('resources/auth/login.php');
和
<?php
session_start();
if( !isset( $_SESSION ) )
include 'resources/includes/header.html';
include 'resources/includes/footer.html';
?>