标头功能不能正常使用PHP

时间:2012-11-28 07:36:43

标签: php header

我在成功注册后重定向页面,代码正在正常工作,但标题功能不起作用&页面不重定向。页面没有重定向。我的代码如下所示

  <?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL ^ E_WARNING);

//include("lib/local_config.php");
include("lib/config.php");


$activation=md5(uniqid(rand(), true));

if(isset($_POST["tb_submit"]))
{
    $fname=$_POST["tb_fname"];
    $email=$_POST["tb_email"];
    $pass=$_POST["tb_pass"];
    $repass=$_POST["tb_repass"];
    $contact=$_POST["tb_contact"];

    $select_email=mysql_query("select email from user_reg where email='$email'");
    $rows=mysql_fetch_row($select_email);
    $e_id=$rows[0];

    if(!$email==$e_id && $pass==$repass)
    {
        $insert="insert into user_reg ( fname ,email ,password ,contact,Activation)    values      ('$fname' , '$email' , '$pass' ,'$contact' ,'$activation')";
        $insert_query=mysql_query($insert);

        if(mysql_affected_rows($connect) == 1)
        //if(mysql_affected_rows() == 1)
        {
            $to = $_POST['tb_email'];
            $subject = "Fundu App Creator - Registration confirmation mail ";
            $message = <<<DEMO
                      <html>
                      <head>

                      <link href='http://www.fundumobi.com/app/templatemo_style.css' type='text/css'  
                       rel='stylesheet' />
                     <link href='http://www.fundumobi.com/app/styles/frm.css' rel='stylesheet' 
                       type='text/css' />
                     <style type='text/css'>

                     .style1 {
                    font-size: 14px;
                    font-style: italic;
                    font-weight: bold;

                      }
                      .style2 {color: #FFFFFF}

                       </style>
                      </head>
                     <body>
                     <table width='50%' border='0' align='center'>
                     <tr>
                      <th height='100' bgcolor='#006784'>
                      <div id='site_logo'></div>    </th>
                     </tr>
                    <tr>
                    <td height='247'><div >
                      <div align='center' class='style1'>
                        <p>Your Registration is successful !!!! </p>
                        <p>Now Please click the following Button to complete the registration process:</p>
                        <p>&nbsp;</p><br/>
                        <div ><a href='http://www.fundumobi.com/app/activation.php?act=$activation'>      
                       <img src='images/button66535179.png'/></a></div>
                      </div></td>
                     </tr>
                     <tr>
                       <td height='64' bgcolor='#006784'><div 
                      align='center'>www.funduappcreator.com</div>    </td>
                     </tr>
                    </table>
                    </body>
                    </html>
                    DEMO;

            $from = "contact@fundumobi.com";

            mail($to, $subject, $message, 'From:'.$from);
            header("location:mesg.php");
        }
        else
        { // If it did not run OK.
            header("location:index.php?er=2");
        }
    }
}

?>

6 个答案:

答案 0 :(得分:3)

输出内容后无法发送标题。在这里,您将在PHP标记之前输出whitespcace:

  <?php
^^ remove this whitespace

检查错误日志,你会发现:

  

警告:无法修改标头信息 - 已发送的标头

答案 1 :(得分:1)

您的重定向代码后应该有exit;。请参阅this comment

答案 2 :(得分:1)

尝试使用输出缓冲区

<?php ob_start() ?>

位于此代码的顶部

答案 3 :(得分:1)

使用它。它对我有用。将此函数放在代码顶部:

function move_to(){
    header("Location:page_example.php");
}

并在任何地方调用该函数:

move_to()

答案 4 :(得分:0)

使用headers_sent函数来检测问题。 http://php.net/manual/en/function.headers-sent.php

  

检查标头是否或已经发送到哪里。

     

您不能使用header()函数添加任何标题行一次   标题块已经发送。使用此功能,您可以在   至少可以防止获得与HTTP头相关的错误消息另一个   选项是使用输出缓冲。

// An example using the optional file and line parameters, as of PHP 4.3.0
// Note that $filename and $linenum are passed in for later use.
// Do not assign them values beforehand.
if (!headers_sent($filename, $linenum)) {
    header('Location: http://www.example.com/');
    exit;

// You would most likely trigger an error here.
} else {

    echo "Headers already sent in $filename on line $linenum\n" .
          "Cannot redirect, for now please click this <a " .
          "href=\"http://www.example.com\">link</a> instead\n";
    exit;
}

代码:

<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL ^ E_WARNING);

//include("lib/local_config.php");
include("lib/config.php");


$activation=md5(uniqid(rand(), true));

if(isset($_POST["tb_submit"]))
{
    $fname=$_POST["tb_fname"];
    $email=$_POST["tb_email"];
    $pass=$_POST["tb_pass"];
    $repass=$_POST["tb_repass"];
    $contact=$_POST["tb_contact"];

    $select_email=mysql_query("select email from user_reg where email='$email'");
    $rows=mysql_fetch_row($select_email);
    $e_id=$rows[0];

    if(!$email==$e_id && $pass==$repass)
    {
        $insert="insert into user_reg ( fname ,email ,password ,contact,Activation)    values      ('$fname' , '$email' , '$pass' ,'$contact' ,'$activation')";
        $insert_query=mysql_query($insert);

        if(mysql_affected_rows($connect) == 1)
        //if(mysql_affected_rows() == 1)
        {
            $to = $_POST['tb_email'];
            $subject = "Fundu App Creator - Registration confirmation mail ";
            $message = <<<DEMO
                      <html>
                      <head>

                      <link href='http://www.fundumobi.com/app/templatemo_style.css' type='text/css'  
                       rel='stylesheet' />
                     <link href='http://www.fundumobi.com/app/styles/frm.css' rel='stylesheet' 
                       type='text/css' />
                     <style type='text/css'>

                     .style1 {
                    font-size: 14px;
                    font-style: italic;
                    font-weight: bold;

                      }
                      .style2 {color: #FFFFFF}

                       </style>
                      </head>
                     <body>
                     <table width='50%' border='0' align='center'>
                     <tr>
                      <th height='100' bgcolor='#006784'>
                      <div id='site_logo'></div>    </th>
                     </tr>
                    <tr>
                    <td height='247'><div >
                      <div align='center' class='style1'>
                        <p>Your Registration is successful !!!! </p>
                        <p>Now Please click the following Button to complete the registration process:</p>
                        <p>&nbsp;</p><br/>
                        <div ><a href='http://www.fundumobi.com/app/activation.php?act=$activation'>      
                       <img src='images/button66535179.png'/></a></div>
                      </div></td>
                     </tr>
                     <tr>
                       <td height='64' bgcolor='#006784'><div 
                      align='center'>www.funduappcreator.com</div>    </td>
                     </tr>
                    </table>
                    </body>
                    </html>
DEMO;

            $from = "contact@fundumobi.com";

            mail($to, $subject, $message, 'From:'.$from);
            if (!headers_sent($filename, $linenum)) 
            {
                header("location:mesg.php");
            }
            else
            {
                echo "Headers already sent in $filename on line $linenum\n" .
                  "Cannot redirect, for now please click this <a " .
                  "href=\"http://www.example.com\">link</a> instead\n";
                exit;
            }
        }
        else
        { // If it did not run OK.
            header("location:index.php?er=2");
        //use exit after redirection request
        exit;
        }
    }
}

?>

p.s:也看看“DEMO”;在你的代码中,它应该在行的开头,不应该吗? 重定向后使用exit。 删除之前的空格

答案 5 :(得分:-1)

试试这肯定会有效

<script type="text/javascript">
<!--
   window.location="http://www.newlocation.com";
//-->
</script>