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

html タグの前にセッション開始を配置する必要があります

于 2012-07-20T16:56:16.393 に答える
1

空白行やスペースを含む何かがブラウザーに送信されるに、スクリプトを追加する必要があります。?>これは、このスクリプトの前に含まれている PHP スクリプト ファイル ( ) を閉じて、エディター、FTP クライアント、または別のアプリケーションがファイルの末尾に空白行を追加した場合に偶然発生する可能性があります (これはよくあることです)。これが起こらないようにするには、単純に PHP スクリプトを無制限のままにしておきます (?>ファイル全体が PHP の場合は除外してください)。また、漂遊echo(または同様のもの、たとえばprintprint_varなど) が問題を引き起こしている可能性もあります。

/home/XZXZ/support/database.incあなたの警告では、これはオンラインで起こっているよう16です。

また、Saurabh で触れたように、関数はユーザーをリダイレクトしないため、毎回の後に配置する必要があります。ヘッダーとしてブラウザーに送信するだけで、残りの処理はブラウザーに任せます。ただし、サーバーは、ユーザーをリダイレクトしていることがわからないため、引き続きスクリプトを実行します。したがって、スクリプトを強制終了して、追加のコードが実行されないようにする必要があります。die();header("Location: ...");header()Location: ...

于 2012-04-17T20:26:10.453 に答える
0

あなたが持っているものも教えてください/home/XZXZ/support/database.inc。クエリを実行している場所がわかりません。/home/XZXZ/support/database.incしかし、エラーを見ると、リダイレクトを設定しようとしているように感じます。また、die()を使用してリダイレクトした後に配置することをお勧めしますheader

于 2012-04-17T20:30:28.820 に答える