PHP PDO标头重定向停止工作

时间:2013-05-21 20:27:35

标签: php redirect pdo header http-headers

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

错误

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

它自己的代码用于成员资格激活,用户注册它会通过电子邮件向用户发送他们点击然后链接的确认电子邮件,然后该帐户变为活动状态。这是我的代码,如果有人可以帮我解决这个问题,非常感谢,提前感谢!

我-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";
        }
    }

1 个答案:

答案 0 :(得分:1)

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

将某些数据输出到浏览器已经后,该错误将始终

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

所以如果你需要一个小的黑客来解决这个问题 在文档的最顶部添加echo ob_start()。甚至在<html>

之前 像这样:

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