激活页面的参数

时间:2012-02-07 21:40:55

标签: php codeigniter

我对此非常感到困惑,因此我的伙伴创建了这个模板系统。

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

这是我正常网址的样子:

kansasoutlawwrestling.com/kowmanager/activate/10000/da54d6fad5fa5fadf

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

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

    激活控制器:

    <?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 - 错误的应显示激活表单

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

4 个答案:

答案 0 :(得分:1)

查看 Tank Auth

这是一个已经执行此操作的CI库,但由于存在关键差异,您不希望传递超过必须的数量。因此,只需生成HASH(例如加密),即可找到用户ID和同时激活。

复制和更少的检查和更少的问题;粘贴网址。还消除了必须执行所有额外检查的ID有效性+哈希有效性。

但正如我所说,看看坦克验证代码,并提取激活部分所需的内容,它非常直接,而且已经用于CI。

答案 1 :(得分:1)

出于好奇......如果删除以下行会怎样?

if(!is_string($param2))

你刚才:

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

答案 2 :(得分:1)

我首先简化一下params检查:

$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不是数字(userid)或
         - 如果param2不是字符串(甚至也不是空字符串),则$ error再次为TRUE。
  3. 最后,如果错误为FALSE(初始化),我们将“activate_form”值传递给视图,否则(即如果上述任何条件导致错误设置为TRUE),我们将传递“ error_page“value。

    此外,根据文档,自定义路由应在固定路径后

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

答案 3 :(得分:1)

您无需为帐户激活创建新的控制器/模块,只需在现有的auth控制器/模块中添加新方法。

如果您设置了具有条件的路线并且它们失败,则会显示错误或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约束,这样你只需查询它。