2012-05-23 163 views
0

我想弄清楚爲什麼當一個userID和/或registrationKey沒有放入URL中時,它不顯示404錯誤頁面。應該發生什麼情況是激活控制器被加載時,它應該查看userID和registrationKey是否在URL中,如果不是,則轉到錯誤頁面。不顯示錯誤頁面

網址:siteurl.com/cms/activate/1/d87f0d8a7f0a8

<?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($userID = NULL, $registrationKey = NULL) 
{ 
    //Config Defaults Start 
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg 
    $cssPageAddons = '';//If you have extra CSS for this view append it here 
    $jsPageAddons = '';//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*/ 

    if(!is_numeric($userID) || !preg_match('/^[A-Za-z0-9]+$/', $registrationKey)) 
    { 
     show_404(); 
    } 
    else 
    { 
     $registrationDetails = $this->usersmodel->getRegistrationDetails($userID); 
     if (!is_null($registrationDetails)) 
     { 
      $regKeyDate = $registrationDetails->registrationKeyDate; 

      if ($this->usersmodel->activateUser($userID, $registrationKey)) 
      { 
       $message = 'User was activated successfully! You may now login!';     
      } 
      else 
      { 
       $message = 'User was not activated successfully! Please try again with the right credentials!'; 
      } 
     } 
     else 
     { 
      $message = 'Two or more days have passed since you registered! You will need to register again!';  
     } 
     $bodyContent = $this->config->item('defaultTemplate') ."/usermanagement/forms/activate";//which view file 
    } 

    $this->data['message'] = $message; 
    $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 */ 

任何想法?

+0

你確定你確實到達了「show_404();」線?即你確定你的陳述是正確的嗎? – Laurence

+0

爲什麼不對? –

回答

1

我認爲代碼看起來很好,但是您可能需要對自己的問題進行一些解決。如果你不通過你的URI傳遞參數,並且show_404()函數沒有被調用,那麼你需要看看這些變量包含什麼。由於userID不是數字或registrationKey不僅僅是字母數字。如果你在他們之前var_dump聲明,並且不通過你的URI參數,會發生什麼?

+0

NULL這兩個變量 –

+0

哦,我想我明白了,它看起來應該調用show_404函數,但它不會返回false或任何內容,腳本會一直持續到您的視圖文件。試試'show_404();返回false;' – thefoyer

+0

我不能爲其他答案添加評論,但對於Codeigniter你是正確的,它只是'show_404' no'$ this->' – thefoyer