0

这是一段用 PHP 编写的代码,写在一个名为core.php的单独文件中,它包含在register.php中

if(($retuserkey = $this->dbcontroller->dbregister($email, $contact)) > 0){  
    //if user was successfuly registered send an email where he can activate his account!
    $this->mailer->sendForReg($email,$hash,$flname);

    echo "<script>alert('An activation link is sent to your email id. Please check you spam folder too. Please follow the link to complete the registration.'); window.location = './registersuccess.php';</script>";

    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.websiteaddress.com/registersuccess.php">'; 
    $url = "http://www.websiteaddress.com/registersuccess.php";
    if(!headers_sent()) {
    //If headers not sent yet... then do php redirect
        header('Location: '.$url);
        exit;
    } else {
        //If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
    }

    mail("emailaddress@gmail.com","Registration $retuserkey",$email,"Subject");
}

电子邮件在重定向之前和之后发送。两封电子邮件均已收到,但重定向失败。

我究竟做错了什么?

4

3 回答 3

0
  header('Location: http://localhost/yourFileName.php');
    exit;

header()使用函数 /* 重定向到请求的当前目录中的不同页面 */ header()函数将原始 HTTP 标头发送到客户端

注意:使用正确的网址

于 2014-02-08T14:30:42.820 回答
0

header() 必须在发送任何实际输出之前调用,并且在它之前有 echo。

于 2013-07-09T09:00:01.173 回答
0

你不能这样做:

header('Location: '.$url);
exit;

在某些东西已经被echo淘汰之后:http: //php.net/manual/en/function.header.php

如果您移动您的echo语句以便它们仅在重定向未发生时打印,那么您应该没问题。

于 2013-07-09T09:01:20.803 回答