简体   繁体   中英

PHP header redirection error

I want to redirect my page after the script ends to other site using the code:

header("Refresh:3; url=http://www.googe.com");

I got error in browser

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/bootstrap/header.php:33) in /Applications/MAMP/htdocs/bootstrap/emailto.php on line 70

Please find below my code:

    <?php include('header.php'); ?>
  <!-- Sidebar -->
        <div id="sidebar-wrapper">
            <?php include('links.php'); ?>
        </div>
        <!-- /#sidebar-wrapper -->


     <!-- Page Content -->
        <div id="page-content-wrapper">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">

                        <form class="form-horizontal" role="form" method="post" action="">
                                <div class="form-group">
                                    <label for="to" class="col-sm-2 control-label">To</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="to" name="to" placeholder="To" value="<?php echo $_REQUEST['emails']; ?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="Subject" class="col-sm-2 control-label">Subject</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="subject" name="subject" placeholder="subject" value="subject">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="message" class="col-sm-2 control-label">Message</label>
                                    <div class="col-sm-10">
                                        <textarea class="form-control" rows="4" name="message"></textarea>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-sm-10 col-sm-offset-2">
                                        <input id="submit" name="submit" type="submit" value="Send email" class="btn btn-primary">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-10 col-sm-offset-2">
                                        <! Will be used to display an alert to the user>
                                    </div>
                                </div>
                        </form>

                         </div>
                </div>
            </div>
        </div>
        <!-- /#page-content-wrapper -->
        <div class="container-fluid">
        <?php



            if(isset($_REQUEST['submit'])) {

                    $to = $_REQUEST['to'];
                    $subject =$_REQUEST['subject'];
                    $body= $_REQUEST['message'];
                    $from= "admin@vision.com";
                    $headers= "From: $from";

                if($to && $subject && $body) {

                    mail($to, $subject, $body, $headers);
                    echo "your email has been sent!";
                    header("Refresh:3; url=http://www.googe.com");


                } else {

                    echo "please fill up all fields...";
                }

            }



        ?>


    </div> <!-- end of div container -->



    <?php include('footer.php'); ?>

You've already sent data to the client. You must sent the headers before you sent anything at all.

simply copy your submit button code to top of the page before start any html means before include header.php file.

if(isset($_REQUEST['submit'])) {

                $to = $_REQUEST['to'];
                $subject =$_REQUEST['subject'];
                $body= $_REQUEST['message'];
                $from= "admin@vision.com";
                $headers= "From: $from";

            if($to && $subject && $body) {

                mail($to, $subject, $body, $headers);
                echo "your email has been sent!";
                header("Refresh:3; url=http://www.googe.com");


            } else {

                echo "please fill up all fields...";
            }

        }

Place your code before Html tag works as if html is getting before content will be sent and the header relocation will create a problem and won't work. Try using this as stated below.

if(isset($_REQUEST['submit'])) {
            $to = $_REQUEST['to'];
            $subject =$_REQUEST['subject'];
            $body= $_REQUEST['message'];
            $from= "admin@vision.com";
            $headers= "From: $from";

        if($to && $subject && $body) {

            mail($to, $subject, $body, $headers);
            echo "your email has been sent!";
            header("Refresh:3; url=http://www.googe.com");


        } else {

            echo "please fill up all fields...";
        }

    }

..... ....... .........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM