【发布时间】:2020-08-22 21:19:28
【问题描述】:
我将我的框架设置为 MVC 类型,其中核心库重定向 url。这很有用,如果链接是:Some_class/some_method,它将从 Some_class 加载 some_method。此外,我还使用 .htaccess 文件将所有内容配置为通过 index.php。
index.php
<?php
session_start();
require_once 'config.php';
if (!isset($_SESSION['username']) && empty($_GET['url'])) { ?>
<a href="Users/login">Login</a><br>
<a href="Users/register">Register</a>
<?php } ?>
config.php中的代码
// Redirect to the homepage if not signed in
if(!isset($_SESSION['user'])){
header('location: http://localhost/index.php');
}
用户控制器中的代码
public function login(){
// Code to verify the entered credentials
// If entered data is valid
$_session['user'] = $_POST['user'];
}
public function log_out(){
session_unset();
}
如果有人从 users 控制器使用 log_out 方法注销,则该方法会执行。如果代码是正确的,它应该重定向到 index.php,因为 config.php 中的代码说如果没有设置$_SESSION['user'],它应该重定向到index.php。但是,每当有人注销时,我都会收到标题中提到的错误。我做错了什么?
【问题讨论】:
-
config.php 检查会话变量,但您还没有调用
session_start,所以它永远不会被设置。重定向后也可能是exit。 -
您尝试过什么调试问题?为什么不在重定向发生之前直接启动 XDebug 并设置断点?
标签: php http-redirect