1

成功注册后我重定向了一个页面,代码工作正常,但标题功能不工作&页面不重定向。页面未重定向。我的代码如下

  <?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");
        }
    }
}

?>
4

6 回答 6

3

输出内容后不能发送标头。在这里,您在 PHP 标记之前输出空格:

  <?php
^^ remove this whitespace

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

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

于 2012-11-28T07:40:48.903 回答
1

您应该有一个exit;重定向代码之后。看到这个评论

于 2012-11-28T07:41:11.403 回答
1

尝试,使用输出缓冲区

<?php ob_start() ?>

在这段代码的顶部

于 2012-11-28T07:41:48.187 回答
1

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

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

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

move_to()
于 2017-08-21T11:29:42.450 回答
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;
        }
    }
}

?>

ps:也可以看看“DEMO”;在您的代码中的位置,它应该在行首,不是吗?重定向后使用退出。在你之前删除空格

于 2012-11-28T07:58:39.557 回答
-1

试试这个,这肯定会奏效

<script type="text/javascript">
<!--
   window.location="http://www.newlocation.com";
//-->
</script>
于 2014-01-09T13:41:05.353 回答