【问题标题】:PHP PDO Header redirect stopped working [duplicate]PHP PDO标头重定向停止工作[重复]
【发布时间】:2013-05-21 20:27:35
【问题描述】:

我已经在我的测试服务器上建立了一个站点,现在我将它放在一个新的服务器上来托管我的站点。我正在使用 PDO 进行连接,一旦准备好的语句执行,我就会重定向回我网站的主页,但现在我的网站在不同的服务器上,我收到了这个错误

错误

Cannot modify header information - headers already sent by (output started at /home1/mlayzell/public_html/alpha/cms/includes/my-db-functions.php:17)

它自己的代码用于会员激活,用户注册它会通过电子邮件向用户发送一封确认电子邮件,他们单击然后链接,然后帐户被更改为活动状态。这是我的代码,如果有人可以帮助我解决这个问题,将不胜感激,在此先感谢!

MY-DB-FUNCTION.PHP

<?php
require("config.php");
function connect_to_db() {
global $dbhost, $dbname, $dbuser, $dbpass, $db;

try {
    $db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.'', $dbuser, $dbpass);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} 
catch(PDOException $error) {
    echo 'ERROR: ' . $error->getMessage();
}

}
?>

PHP

if($_GET['action']==0) {

if(isset($_POST['name'],$_POST['email'],$_POST['password'])) {

    $key = md5(rand(0,1000));
    $date = date('Y-m-d H:i:s');

    $statement_user = $db->prepare("INSERT INTO `app_users` ( `use_name`, `use_key`, `use_email`, `use_password`, `use_date`, `use_status`, `use_typ_id`) VALUES (:use_name, :use_key, :use_email, :use_password, :use_date, :use_status, :use_type);");

    $statement_user->execute(array(':use_name' => $_POST['name'], ':use_key' => $key, ':use_email' => $_POST['email'], ':use_password' => $_POST['password'], ':use_date' => $date, ':use_status' => "0", ':use_type' => "0"));


        $to = $_POST['email'];  
        $subject = 'Signup | Verification';  
        $message = ' 

Thanks for signing up! 
Your account has been created, you can login with the following credentials after you have activated your account by clicking the link below. 

------------------------ 
Email: '.$email.' 
Password: '.$password.' 
------------------------ 

Please click this link to activate your account: 

http://theapplist.com/alpha/cms/users/handler-users.php?action=5&use_email='.$_POST['email'].'&use_key='.$key.' 

';

            $headers = 'From:noreply@mysite.com' . "\r\n"; 
            mail($to, $subject, $message, $headers);  

            header('location:../index.php?signup=success');
        }
        else {

            echo "FAIL";
        }
    }

【问题讨论】:

  • my-db-functions.php 第 17 行有什么内容?
  • 这是因为某处有一个神奇的空白,或者是一个邪恶的错误。你的任务是找到他们并追逐他们。祝你好运,士兵。
  • @andrewsi my-db-functions.php 中没有第 17 行,这就是我很困惑的原因!
  • @MitchellLayzell - 尝试从中删除结束 ?&gt; 标记,看看是否有区别。
  • @MitchellLayzell 听安德鲁斯的声音

标签: php redirect pdo header http-headers


【解决方案1】:

因为,您正在使用 header('location:../index.php?signup=success');

已经向浏览器输出一些数据后,该错误将总是发生。

因为,header() 函数或标头应该在您的页面甚至开始呈现任何 html 标记之前,在您的任何页面内容加载到浏览器之前发送。

所以如果你需要一个小技巧来解决这个问题 在文档的最顶部包含echo ob_start()。甚至在&lt;html&gt;

之前

像这样:

<?php echo ob_start(); ?>
<html>
<head></head>
//and keep going

【讨论】:

    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 2012-03-23
    • 2010-09-30
    • 2014-04-21
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多