MySQL进程重定向失败后使用头


Using a header after MySQL process fails to redirect

在我们的网站上,我们正在实施一个免费的注册系统,这样我们就可以允许人们访问安全的个人信息,但如果他们直接访问,我们需要重定向此文件。任何帮助都将不胜感激

<?php
include("../../account/core/init.php");
logged_in_redirect();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include '../../bundles/base/inc/head.php'; ?>
</head>
<body>
    <?php include("../../bundles/base/inc/header.php"); ?>
    <div class="container page clearfix account-activation">
    <?php if(isset($_GET['email'], $_GET['email_code'])) {
        echo 'This is where the account will be activated';
    } else {
        header('Location: https://www.oursite.org/');
        exit();
    }
    ?>
</div>
<?php include("../../bundles/base/inc/footer.php"); ?>
</body>
</html>

这是我们的out代码https://www.oursite.org/account/private/secure/activation/我们不断收到这个错误:

警告:无法修改标头信息-标头已由第17行/route/to/site/public_html/account/private/secure/activation/index.php:9中的/route/to/site/ppublic_html/aaccount/privaty/secure/aactivation/index.php发送

#

.htaccess文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?username=$1 

您通过帐户/个人资料/访问个人资料?username=user但我正试图将其重写为account/profile/user/

问题是header调用只发生在PHP逻辑中,该逻辑卡在发送到浏览器的HTML中间。因此,在Apache解析HTML&这意味着header不被PHP调用,但就Apache而言,纯HTML等同于发送的内容,相当于发送的头。

解决方案?在HTML像这样呈现之前,重构您的逻辑。

其思想是,它不是立即设置echo,而是设置一个$message变量。因此,您的if/else逻辑发生在任何HTML被吐出之前。通过这种方式,$message在发生任何事情之前被设置,然后如果需要,可以将$message的值传递给内联PHP。如果header需要重定向?好吧,这只是在<!DOCTYPE html>发生之前发生的:

<?php
include("../../account/core/init.php");
logged_in_redirect();
$message = ''
if(isset($_GET['email'], $_GET['email_code'])) {
  $message = 'This is where the account will be activated';
}
else {
  header('Location: https://www.oursite.org/');
  exit();
}
?><!DOCTYPE html>
<html lang="en">
<head>
<?php include '../../bundles/base/inc/head.php'; ?>
</head>
<body>
    <?php include("../../bundles/base/inc/header.php"); ?>
    <div class="container page clearfix account-activation">
    <?php echo $message; ?>
</div>
<?php include("../../bundles/base/inc/footer.php"); ?>
</body>
</html>
Warning: Cannot modify header information - headers already sent by.......

意味着您已经写出了内容,并且服务器已经开始将响应流式传输到客户端。您无法修改标头,因为它们已经发送。

将生成代码的标头移动到写入第一个输出之前的某个点。

将此代码移动到页面标题所在的位置:

<?php if(isset($_GET['email'], $_GET['email_code'])) {
        echo 'This is where the account will be activated';
    } else {
        header('Location: https://www.oursite.org/');
        exit();
    }

如前所述:

https://stackoverflow.com/a/8028987/3739658

修改HTTP标头的一些功能包括:

  • header/header_remove
  • session_start/session_regenerate_id
  • setcookie/setrawcookie

使用:-

ob_start();
ob_clean();

确保ob_start((是网页中的第一行。

// clean the output buffer
ob_clean();