Php URL中控制器后的多个变量
我想知道如何处理我的路线和控制器 用户在我的网站上注册后,他们会收到一封电子邮件,其中包含激活控制器的链接,该链接要求用户输入密码以确认帐户。但是,我正在尝试向我的控制器添加一个if语句,以便它验证url中activate末尾是否有两个参数,如果没有,那么它将显示我的错误页面。还需要验证第一个参数是否为数字 由于某种原因,我有一些设置正确,但不确定是什么 这是我在这条路线上用的Php URL中控制器后的多个变量,php,codeigniter,Php,Codeigniter,我想知道如何处理我的路线和控制器 用户在我的网站上注册后,他们会收到一封电子邮件,其中包含激活控制器的链接,该链接要求用户输入密码以确认帐户。但是,我正在尝试向我的控制器添加一个if语句,以便它验证url中activate末尾是否有两个参数,如果没有,那么它将显示我的错误页面。还需要验证第一个参数是否为数字 由于某种原因,我有一些设置正确,但不确定是什么 这是我在这条路线上用的 $route['activate/:num/:any'] = "activate"; 控制器: <?php i
$route['activate/:num/:any'] = "activate";
控制器:
<?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()
{
//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*/
if (is_numeric($this->uri->segment(3)) OR $this->uri->segment(4) == '')
{
$bodyContent = "error_page";
}
else
{
$bodyContent = "activate_form";//which view file
}
$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";
这将允许您将两个参数传入激活控制器的索引函数,如下所示:
public function index($param1, $param2)
然后,您将能够执行以下操作:
if (is_numeric($param1) OR $param2 == '')
{
$bodyContent = "error_page";
}
else
{
$bodyContent = "activate_form";
}
希望这对您有所帮助。将您的路线更改为:
$route['activate/:num/:any'] = "activate/index/$1/$2";
这将允许您将两个参数传入激活控制器的索引函数,如下所示:
public function index($param1, $param2)
然后,您将能够执行以下操作:
if (is_numeric($param1) OR $param2 == '')
{
$bodyContent = "error_page";
}
else
{
$bodyContent = "activate_form";
}
希望这能对你有所帮助。我同意我的观点,他的回答是基于你最初的要求。因此,我们将以他的初步回答为基础。。你们的问题在于你们的if-else
你需要
public function index($param1, $param2)
不过我会把它改成更像
public function index($param1 = NULL, $param2 = NULL)
另请注意,如果您已将控制器设置为基于功能(例如激活功能)的多个视图已用完,则这是针对控制器的索引功能。然后您需要将“index”()替换为“activate”()。
第二,从你的说法来看,如果有人在控制器上着陆,你希望它404或给出一些错误。如果参数不符合要求。这就是为什么我改变了上述方式,这样你的函数就不会寻找未设置的参数,也可能会错误地未设置。因此,我们将它们默认为NULL,以防万一
所以我的解决办法是做一些类似于
$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(/*whatever your constraint for $param2 (!preg_match()) or something*/)
{
$x++;
}
}
if($x !== 0)
{
$bodyContent = "error_page";
}
else
{
$bodyContent = "activate_form";
}
}
我同意我的观点,他的答案是基于你们最初的要求。所以我们将以他的最初答案为基础。你们的问题在于你们的if-else
你需要
public function index($param1, $param2)
不过我会把它改成更像
public function index($param1 = NULL, $param2 = NULL)
另请注意,这是针对控制器的索引功能,如果您已将控制器设置为基于该功能的多个视图已用完,例如激活功能,则需要将“索引”()替换为“激活”()。
第二,从你的说法来看,如果有人在控制器上着陆,你希望它404或给出一些错误。如果参数不符合要求。这就是为什么我改变了上述方式,这样你的函数就不会寻找未设置的参数,也可能会错误地未设置。因此,我们将它们默认为NULL,以防万一
所以我的解决办法是做一些类似于
$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(/*whatever your constraint for $param2 (!preg_match()) or something*/)
{
$x++;
}
}
if($x !== 0)
{
$bodyContent = "error_page";
}
else
{
$bodyContent = "activate_form";
}
}
但只有一个问题,当我转到这个url时,它给了我一个问题。它应该只显示404页面。如果我做这样的测试,它会显示一些奇怪的消息。我添加了错误控制器,它是默认的404覆盖控制器,但它是相同的模板页面代码。不确定为什么它会给我这个错误。仅在上问题当我转到这个url时,它给了我一个问题。它应该只显示404页面。如果我做这样的测试,它会显示一些奇怪的消息。我添加了错误控制器,它是默认的404覆盖控制器,但它是相同的模板页面代码。不确定为什么它会给我这个错误。