在if语句中重定向

时间:2012-05-30 18:31:57

标签: php codeigniter

我正在试图找出如何在页面加载5秒后将此页面重定向到某个页面仅当if($ this-> usersmodel-> activateUser($ userID,$ registrationKey) )返回TRUE。我不能使用jquery,因为问题是如果它返回false然后我不希望它重定向。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Activate extends CI_Controller { 

public function __construct()
{
    parent::__construct();
    $this->load->model('users/usersmodel');
}

public function index()
{
    //Config Defaults Start
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg
    $cssPageAddons = '';//If you have extra CSS for this view append it here
    $jsPageAddons =  '<script src='.base_url().'assets/peach/js/validate/activate.js></script>';//If you have extra JS for this view append it here
    $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's
    $siteTitle = '';//alter only if you need something other than the default for this view.
    //Config Defaults Start


    //examples of how to use the message box system (css not included).
    //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...');

    /**********************************************************Your Coding Logic Here, Start*/

    $userID     = $this->uri->segment(2);
    $registrationKey    = $this->uri->segment(3);

    if ( ((!is_numeric($userID)) || (is_null($userID))) || ((is_null($registrationKey)) || (!preg_match('/^[A-Za-z0-9]+$/', $registrationKey))) ) 
    {
        $bodyContent = $this->config->item('defaultTemplate') ."/404";//which view file  
    } 
    else 
    {
        $registrationDetails = $this->usersmodel->getRegistrationDetails($userID);
        if (!is_null($registrationDetails))
        {
            if ($this->usersmodel->activateUser($userID, $registrationKey))
            {
                $message = 'User was activated successfully! You may now login!';   
                $bodyContent = $this->config->item('defaultTemplate') ."/usermanagement/forms/activate";//which view file      
            }
            else
            {
                $message = 'User was not activated successfully! Please try again with the right credentials!';  
                $bodyContent = $this->config->item('defaultTemplate') ."/usermanagement/forms/activate";//which view file  

            }
            $this->data['message'] = $message;                                
        }
        else
        {
            $bodyContent = $this->config->item('defaultTemplate') ."/404";  
        }
    }

    $bodyType = "full";//type of template   

    /***********************************************************Your Coding Logic Here, End*/

    //Double checks if any default variables have been changed, Start.
    //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.      
    if(count($msgBoxMsgs) !== 0)
    {
        $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs));
    }
    else
    {
        $msgBoxes = array('display' => 'none');
    }

    if($siteTitle == '')
    {
        $siteTitle = $this->metatags->SiteTitle(); //reads 
    }

    //Double checks if any default variables have been changed, End.

    $this->data['msgBoxes'] = $msgBoxes;
    $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view.
    $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view.
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php
    $this->data['bodyType'] = $bodyType;
    $this->data['bodyContent'] = $bodyContent;
    $this->load->view($this->config->item('defaultTemplate') .'/usermanagement/index', $this->data);
}

}

/* End of file activate.php */ 
/* Location: ./application/controllers/activate.php */ 

3 个答案:

答案 0 :(得分:4)

2种方式..一个你可以使用jquery / javascript并发布/获取通过JavaScript完成的事情,如果在发布项目时返回true然后你做

setInterval(function(){window.location = 'http://urlYouWannaGoto.com/';}, 3000);

另一个如果你想去php唯一的路线,也应该使用codeigniter。如果在你的情况下是真的那么推出一个标题。

header('Refresh: 3; url=http://urlYouWannaGoto.com/'); 

就PHP路由而言,你还要确保在其他任何内容之前输出标题,这样它才能真正起作用。

答案 1 :(得分:0)

您可以通过设置刷新标头来实现这一目标。

header('refresh:5; url=some_url')

但我不喜欢这个解决方案。

编辑:

我刚看了CodeIgniter的来源 redirect()

if ( ! function_exists('redirect'))
{
  function redirect($uri = '', $method = 'location', $http_response_code = 302)
  {
    if ( ! preg_match('#^https?://#i', $uri))
    {
      $uri = site_url($uri);
    }

    switch($method)
    {
      case 'refresh' : header("Refresh:0;url=".$uri);
        break;
      default : header("Location: ".$uri, TRUE, $http_response_code);
        break;
    }
    exit;
  }
}

在实践中,他们会执行以下操作:

header("Refresh:0;url=".$uri);
exit;

那么以下内容如何:

header("Refresh:5;url=".'your_path');
exit; 

答案 2 :(得分:0)

在我看来你可以测试这个条件是真的... if($ this-&gt; usersmodel-&gt; activateUser($ userID,$ registrationKey))返回TRUE ...如果为TRUE,则初始化一个计时器5秒如果FALSE返回false ...