【问题标题】:Parameters for activation page激活页面参数
【发布时间】:2012-02-29 08:41:18
【问题描述】:

我完全被这件事难住了,我创建这个模板系统的朋友也是如此。

我有一个注册页面,向用户发送一封电子邮件,其中包含指向帐户激活页面的链接,他们必须在该页面中填写密码才能确认。链接里面是他们的 user_id 和一个用于注册密钥的随机字符串。

这是我正常的 url 的样子:

kansasoutlawwrestling.com/kowmanager/activate/10000/da54d6fad5fa5fadf

我想要做的是,如果这些陈述中的任何一个为真,那么它会显示我的 404 错误页面:

  1. url 中没有 user_id
  2. 网址中没有注册码
  3. url中没有这两个参数

激活控制器:

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

class Activate extends CI_Controller 
{ 

public function __construct()
{
    parent::__construct();
    $this->load->library('kow_auth');            
}   

public function index($param1 = NULL, $param2 = 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 = '<script src="http://www.kansasoutlawwrestling.com/kowmanager/assets/js/activatevalidate.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*/

    $x = 0;
    if(($param1 !== NULL)&&($param2 !== NULL))
    {
        //params not null yay..
        if((isset($param1))&&((trim($param1) !== '')||(!empty($param1))))
        {
            if(!is_numeric($param1))
            {
              $x++;
            } 
        }
        if((isset($param2))&&((trim($param2) !== '')||(!empty($param2))))
        {
            if(!is_string($param2))
            {
              $x++;
            } 
        }
    }
    else
    {
        $x++;
    }


    if($x !== 0)
    {
       $bodyContent = "error_page";
    }
    else
    {
       $bodyContent = "activate_form";
    }



    $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['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view.
    $this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php
    $this->data['bodyType'] = $bodyType;
    $this->data['bodyContent'] = $bodyContent;
    $this->load->view('usermanagement/index', $this->data);
}

function activate_submit()
{        
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric');

    $user_id            = $this->uri->segment(3);
    $registration_key   = $this->uri->segment(4);

    if (($registration_key == '') OR ($user_id == ''))
    {
        echo json_encode(array('error' => 'yes', 'message' => 'URL was not complete!')); 
    }
    else
    {
        if (!$this->form_validation->run()) 
        {
            echo json_encode(array('error' => 'yes', 'message' => 'There was a problem submitting the form! Please refresh the window and try again!'));    
        }
        else
        {                           
            if ($this->kow_auth->activate_user($user_id, $registration_key, $this->input->post('password'))) 
            {
                echo json_encode(array('sucess' => 'yes', 'message' => 'Your account has been successfully activated!'));
            } 
            else 
            {                                                           
                echo json_encode(array('error' => 'yes', 'message' => 'The activation code you entered is incorrect or expired!'));
            }
        }
    }


}

}

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

路线:

$route['activate/:num/:any'] = 'activate/index/$1/$2';
$route['404_override'] = 'error';

这是我为每个实例得到的结果:

kansasoutlawwrestling.com/kowmanager/activate - 正确

kansasoutlawwrestling.com/kowmanager/activate/10000/ - 正确

kansasoutlawwrestling.com/kowmanager/activate/10000/271cce33ab11ced5fd10aeca41323a3c - 不正确应该显示激活表单

编辑:任何人都有任何想法,因为它似乎没有任何工作。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我将从简化参数检查开始:

    $this->error = FALSE;
    
    if(NULL != $param1 AND NULL != $param2)
    {
       if(!is_numeric($param1) OR (string)trim($param2)!= '')
       {
         $this->error = TRUE;
       }
    }
    else
    {
      $this->error = TRUE;
    }
    
    $this->data['bodyContent'] = $this->error? 'error_page' : 'activate_form';
    

    这里已经很晚了,所以我可能会搞砸一些事情,但基本上:

    1. 如果两个参数都为空,则将 $error 设置为 TRUE(它们不必为空);
    2. 如果至少有一个不为空:
      - 如果 param1 不是数字(用户 ID)或
      - 如果 param2 不是字符串(甚至不是空字符串),则 $error 再次为 TRUE。

    最后,如果错误为 FALSE(如已初始化),我们将“activate_form”值传递给视图,否则(即,如果上述任何条件导致错误设置为 TRUE),我们将“ error_page”值。

    此外,根据文档,自定义路由应该在 固定路由之后进行:

    $route['404_override'] = 'error';
    $route['activate/(:num)/(:any)'] = 'activate/index/$1/$2';
    

    【讨论】:

    • 完全一样的情况?适用于前两条但不适用于最后一条路线?
    • 不,当没有参数时它可以工作但是如果只有 user_id 那么它会给出一个奇怪的错误消息,如果有两个参数它会进入 404 页面,它不应该因为它应该是显示激活视图
    【解决方案2】:

    出于好奇...如果删除以下行会发生什么?

    if(!is_string($param2))
    

    而你只有:

    if((isset($param2))&&((trim($param2) !== '')||(!empty($param2))))
    {
    $x++;
    }
    

    【讨论】:

      【解决方案3】:

      您无需创建新的控制器/模块来激活帐户,只需在现有的身份验证控制器/模块中添加一个新方法即可。

      如果您设置带有条件的路由但它们失败了,您会显示错误或 404。

      class Auth extends CI_Controller
      {
          public function __construct(){parent::__construct();}
          /**
           * Activate user account
           * $route['activate/(:num)/(:any)'] = 'auth/activate/$1/$2';
           */
          public function activate($uid, $code)
          {
      
              //if need be, double check
              if(!$uid OR !$code){show_404();} //BOTH need to exists
      
              //if $route['activate/(:num)/(:any)'] = 'auth/activate/$1/$2'; FAILS CI will show error or 404
      
              //grab $code and $uid and seek a match from DB, if failure do your own errors.
          }
      }
      

      我建议从 uri 段中删除用户 ID,并将激活码设置为 UNIQUE db 约束,这样您只需查询即可。

      【讨论】:

      • 我没有身份验证控制器。我有一个登录/注销/激活/注册控制器。我添加这个是为了让我的模板系统更容易我有
      • 在我不想要一个授权控制器之前你可能没有理解我所说的。
      • 我明白了你的问题,我才开始阅读你的回复。就像我说过的那样,只要您在路由中设置了条件,CI 就会为您处理任何 URI 段故障。将类 Auth 重命名为 Activate 并重新路由到那里。只需告诉您期望 int 或 string 的路线(CI 还将查找 allowed_uri_chars),因此您真正需要做的就是查询数据库以确保密钥正确。
      【解决方案4】:

      看看Tank Auth

      它是一个 CI 库,已经做到了这一点,但有一个关键的区别,你不想传递比你必须传递的更多的东西。因此,只需生成一个 HASH(例如加密),即可让您找到用户 ID 并同时激活。

      复制和粘贴 URL 的检查和问题更少。也无需对 ID 有效性 + 哈希有效性进行所有这些额外检查。

      但正如我所说,查看 tank auth 代码,然后提取激活部分所需的内容,这相当简单,并且已经用于 CI。

      【讨论】:

      • 如果问题是围绕着一些需要像 Tank Auth 这样的超重概念进行的,这将有一些优点。但问题似乎是在询问路由问题,而不是与实际验证处理有关。
      • @chris,我同意,我试图提供一种替代的简化方法,其中 Jeff 不需要验证两件事,并且有多个条件(坏用户、坏哈希、有效用户、坏哈希等;)
      • Tank you Jakub 但这条路线就像我的脚本一样,但我仍然无法弄清楚为什么会发生这种情况
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      相关资源
      最近更新 更多