2013-07-09 55 views
0

失败以下是在PHP写在一个单独的文件中的一段代码称为core.php中并且这被包括在register.php重定向在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("[email protected]","Registration $retuserkey",$email,"Subject"); 
} 

电子邮件之前和之后被发送重定向。这两封电子邮件都被收到,但重定向失败。

我在做什么错?

+0

301重定向失败,如果发送头文件,javascript重定向应该可以工作 – DevZer0

回答

0

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

+0

我已经在header()之前评论过echo echo语句,但仍然没有重定向 – user2563758

+0

检查mailer-> sendForReg或dbcontroller-> dbregister是否有任何输出。 –

+0

是的。电子邮件发送和dbregister存储在表中的数据...它的完成。 – user2563758

0

你不能做:

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

东西后已是echo倒是出来:http://php.net/manual/en/function.header.php

如果您将您的echo语句移动到只在未发生重定向时才打印,那么您应该可以。

+0

我已经评论回声头2回声语句之前的标题()仍然重定向不发生 – user2563758

+0

@ user2563758你看到什么时候页面被访问?错误日志中是否有消息? – Jon

+0

错误日志中没有消息。用户坐在同一个页面中,永远不会有任何消息或重定向 – user2563758

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

header()功能用来/ *重定向到在当前目录中的不同网页被请求的*/ header()功能发送一个原始的HTTP报头到客户端

NB:使用权URL

+1

解释为什么这是通过编辑答案。 –

+0

什么是“N.B.”意思? –

+0

@韦恩·康拉德,[注意]是mean = nota bene –