1

基本上,我正在制作一个评论表单,用户在其中填写有关特定产品的项目,并在提交表单时调用 process.php 来验证表单,并在提交无错误时向我发送电子邮件。然后它将用户返回到表单页面并显示已提交的错误或表示已成功提交,这一切都很好,但我现在需要做的是当正确填写表单时我仍然想要电子邮件并被返回到表单的页面,同时也将数据插入到我的数据库中。我的表单验证效果很好,直到我尝试弹出一些代码以插入数据库,然后我得到这些错误......

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/XZXZ/support/database.inc:16) in /home/XZXZ/public_html/Reviews/process.php on line 113

Warning: Cannot modify header information - headers already sent by (output started at /home/XZXZ/support/database.inc:16) in /home/XZXZ/public_html/Reviews/process.php on line 117

我只需要知道我可以在哪里弹出它以避免这些错误并且仍然可以正常工作。下面看一下 process.php 文件:

<?php

    if( isset($_POST) ){  

        //form validation vars  
        $formok = true;  
        $errors = array();  

        //sumbission data  
        $ipaddress = $_SERVER['REMOTE_ADDR'];  
        $sub_date = date('d/m/Y');  
        $sub_time = date('H:i:s');  

        //form data
        $sub_date = $_POST['sub_date'];  
        $sub_time = $_POST['sub_time'];  
        $review_title = $_POST['review_title'];
        $rating = $_POST['rating'];
        $pros = $_POST['pros'];  
        $cons = $_POST['cons'];  
        $best_uses = $_POST['best_uses'];  
        $comments = $_POST['comments'];
        $upload = $_POST['upload']; 
        $recommend = $_POST['recommend'];
        $reviewer_name = $_POST['reviewer_name'];
        $reviewer_desc = $_POST['reviewer_desc'];
        $reviewer_loc = $_POST['reviewer_loc'];



        //form validation to go here....  

    }  

        //validate review title is not empty  
    if(empty($review_title)){  
        $formok = false;  
        $errors[] = "You have not entered a title for this review";  
    }

        //validate rating is selected  
    if (isset ($_POST['rating']) && ($_POST['rating'] == '' )) {
        $formok = FALSE;
        $errors[] = "You have not selected a rating for the product";
    } 

        //validate pros is not empty  
    if(empty($pros)){  
        $formok = false;  
        $errors[] = "You have not entered any pros";   
    }

        //validate cons is not empty  
    if(empty($cons)){  
        $formok = false;  
        $errors[] = "You have not entered any cons";  
    }

        //validate name is not empty  
    if(empty($reviewer_name)){  
        $formok = false;  
        $errors[] = "You have not entered your name";  
    }

        //validate desc is not empty  
    if(empty($reviewer_desc)){  
        $formok = false;  
        $errors[] = "You have not entered your description";  
    }

        //validate location is not empty  
    if(empty($reviewer_loc)){  
        $formok = false;  
        $errors[] = "You have not entered your location";  
    } 

        //send email if all is ok  
    if($formok){  

        $headers = "From: reviews@XZXZ.com" . "\r\n";  
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

        $emailbody = "<p>You have received a new product review pending approval from XZXZ.com</p> 
                      <p><strong>Review Title: </strong> {$review_title} </p>
                      <p><strong>Rating: </strong> {rating} </p>
                      <p><strong>Pros: </strong> {$pros} </p> 
                      <p><strong>Cons: </strong> {$cons} </p> 
                      <p><strong>Best Uses: </strong> {$best_uses} </p> 
                      <p><strong>Comments: </strong> {$comments} </p>
                      <p><strong>Upload: </strong> {$upload} </p> 
                      <p><strong>Recommend: </strong> {$recommend} </p>
                      <p><strong>Name: </strong> {$reviewer_name} </p>
                      <p><strong>Description: </strong> {$reviewer_desc} </p> 
                      <p><strong>Location: </strong> {$reviewer_loc} </p>
                      <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";  

        mail("XX@XZXZ.com","New Pending Review",$emailbody,$headers);

        //insert to database    

        require("/home/XZXZ/support/database.inc");

        $SQL="INSERT INTO 'XZXZ_rvs'.'reviews_prod' (sub_date, sub_time, review_title, rating, pros, cons, best_uses, comments, upload, recommend, reviewer_name, reviewer_desc, reviewer_loc) VALUES ('$_POST[$sub_date]','$_POST[$sub_time]','$_POST[$review_title]','$_POST[$rating]','$_POST[$pros]','$_POST[$cons]','$_POST[$best_uses]','$_POST[$comments]','$_POST[$upload]','$_POST[$recommend]','$_POST[$reviewer_name]','$_POST[$reviewer_desc]','$_POST[$reviewer_loc]')";

    }
        //what we need to return back to our form  
    $returndata = array(  
        'posted_form_data' => array(  
            'review_title' => $review_title,
            'rating' => $rating, 
            'pros' => $pros,  
            'cons' => $cons,  
            'best_uses' => $best_uses,  
            'comments' => $comments,
            'upload' => $upload,
            'recommend' => $recommend,
            'reviewer_name' => $reviewer_name,  
            'reviewer_desc' => $reviewer_desc,
            'reviewer_loc' => $reviewer_loc  
        ),  
        'form_ok' => $formok,  
        'errors' => $errors  
    );
    //if this is not an ajax request  
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){  

    //set session variables  
    session_start();  
    $_SESSION['cf_returndata'] = $returndata;  

    //redirect back to form  
    header('location: ' . $_SERVER['HTTP_REFERER']);  

} 
4

3 回答 3

1

在将任何内容(包括空行或空格)发送到浏览器之前,您必须添加脚本。如果您关闭?>包含在此脚本之前的 PHP 脚本文件 ( ),并且您的编辑器、FTP 客户端或其他应用程序在文件末尾添加一个空行(这很常见),这可能会意外发生。为防止这种情况发生,只需让 PHP 脚本保持开放式(?>如果整个文件是 PHP,则省略)。也有可能是一个流浪者echo(或类似的东西,例如print,print_var等)导致了这个问题。

在您的警告中,这似乎是/home/XZXZ/support/database.inc在线上发生的16

此外,正如 Saurabh 所触及的,您必须die();在 every 之后放置,header("Location: ...");因为该header()函数不会重定向用户 - 它仅Location: ...作为标头发送到浏览器,并将其留给浏览器来完成其余的工作 - 但是,服务器,不知道您正在重定向用户,因此它将继续运行脚本。因此,您必须终止脚本以防止执行任何其他代码。

于 2012-04-17T20:26:10.453 回答
1

您必须将会话开始放在任何 html 标记之前

于 2012-07-20T16:56:16.393 回答
0

您能否也分享一下您在/home/XZXZ/support/database.inc. 我看不到您在哪里执行查询。但是看着错误,我觉得这/home/XZXZ/support/database.inc是在尝试设置重定向。die()在使用重定向之后放置一个也是一个好习惯header

于 2012-04-17T20:30:28.820 回答