CodeIgniter URI参数部分绕过"if"消息.陈述 [英] CodeIgniter URI Parameter is partially bypassing an "if" statement

查看:44
本文介绍了CodeIgniter URI参数部分绕过"if"消息.陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,亲爱的人们.我是社区的新手,但编程方面的知识却不是那么多.我来带个短篇小说.

Good day, dear people. I'm new in the community, but not that much in programming. Let me bring the short story.

我正在基于CodeIgniter框架(v 2.1.4)创建一个小应用程序.我已经配置了整个代码,因此不必在URL上编写"index.php"部分,而且我还配置了默认控制器也未写在URL中.我还确保通过"$ this-> session-> flashdata()"方法将每个控制器的每个方法链接到一个视图,该视图的div能够显示错误消息.

I'm creating a little application based on the CodeIgniter Framework (v 2.1.4). I already configured the whole code so I don't have to write the "index.php" part on the URL, and also I configured the default controller to not be written in the URL as well. I also ensured that each method of each controller was linked to a view which featured div able to show an error message, thanks to the '$this->session->flashdata()' method.

因此,假设我的默认控制器称为"inicio".为了从本地服务器运行它,我转到浏览器并在地址栏中写以下行:

So, let's say, my default controller is called 'inicio'. In order to run it from my local server, I go to my browser and write the following line in the address bar:

URL: http://localhost/vga/

并配置了application/config/routes.php,这样我就可以避免不必要的书写

And configured the application/config/routes.php so I could avoid unnecessary writting

$route['default_controller'] = 'inicio';
$route['404_override'] = '';
$route['inicio'] = '';

我放置了.htaccess的设置

I put the setup of the .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

仅此而已.这将使我进入默认控制器,该控制器由会话管理器控制,该控制器迫使我填写登录表单,以便访问该页面的内部.

And that's all. That will bring me to the default controller, which is controlled by a session manager which forces me to fill a login form, in order to access to the inner part of this page.

URL: http://localhost/vga/acceso

此登录名具有指向基本注册表格的链接,该表格的工作原理类似于超级按钮.

This login has a link to a basic registration form, which works like a charm.

URL: http://localhost/vga/acceso/registro

甚至还有一个配置文件控制器,通过它我可以查看和编辑自己的配置文件.好东西,由于ID是通过会话传递的,因此我什至不需要URI段参数.

There is even a profile controller, which lets me see and edit my own profile. Good stuff, I don't even need a URI segment parameter since the ID is being passed by session.

除了默认控制器的配置外,我没有碰过application/config/routes.php.到现在为止一切都还不错.

I didn't touch the application/config/routes.php for anything more than the configuration of the default controller. Everything is cool until this point.

现在,我来了.

我在此页面中创建了一个新的控制器来控制角色".该控制器有3种方法:

I created a new controller to control 'roles' in this page. This controller has 3 methods:

class Roles extends  CI_Controller {
    function index() {}
    function editar() {}
    function usuarios() {}
}

每种方法背后的基本思想都很简单.

The basic idea behind each method is rather simple.

索引"应列出该Web系统中现有的角色(例如admin,user,ect),并告诉我那里有多少用户(按角色分类)."editar"应该让我编辑每个角色的基本方面."usuarios"应该告诉我列出每个角色中存在的用户.

'index' should list the roles existing in this web system (such as admin, user, ect), and also tell me how much users are there, categorized by roles. 'editar' should let me edit the basic aspects of each role. 'usuarios' should show me list the users existing in each role.

我对每种方法都进行了编程,它们工作得很好.该问题取决于在异常情况下显示的该死的flashdata,特别是在"editar"和"usuarios"方法中.

I programmed each method, and they work just fine. The problem relies in a damn flashdata that is being shown under strange circumstances, specifically in the 'editar' and 'usuarios' methods.

为什么特别?好吧,与其他所有方法相比,它们拥有3个URI段:控制器,方法和仅一个参数.

Why are they special? Well, compared to every other method, they possess 3 URI Segments: Controller, Method, and just One Parameter.

这是场景:

"editar"和"usuarios"都有某些共同的代码行:

Both 'editar' and 'usuarios' have certain common lines of code:

/*
    I'll be trying to edit a specific numeric entry,
    and force it to be zero in case of empty segment. 
    The page should be redirected in this case.
*/
$id = $this->uri->segment(3, 0); 
if( $id == 0 ){
    $this->session->set_flashdata('message', 'Error #X' ); /* Only these controllers have this Error Code */
    redirect( 'roles' , 'refresh');
}

看起来不错吧?好吧,当我进入角色"索引并转到角色/usuario/1"(即现有数据)或进入角色/editar/1"(即现有数据)时,这些页面显示干净,如预期的那样.当我在浏览器上按刷新"时,或者当我从这一点转到任何页面时,麻烦就来了……因为在所有情况下,系统都会抛出错误#X"消息……并且该页面未被重定向,正如我在$ id = 0的情况下所期望的那样.因此,我假设该流绕过了if语句,足以设置$ this-> session-> flashdata('message')变量,但不足以命令重定向方法.

Looks good, right? Well, the thing is, when I'm in the 'roles' index and go to 'roles/usuario/1' (which is existing data) or to 'roles/editar/1' (which also is existing data), those pages shows clean as expected. The trouble comes when I press 'refresh' on my browser, or when I go to any page from this point... because in all the cases the system throws a 'Error #X' message... and the page IS NOT REDIRECTED, as I should expect in case of $id = 0. So, I'm assuming that the flow bypassed the if statement, enough to set the $this->session->flashdata('message') variable, but not enough to command the redirect method.

这里很头疼.然后,我开始思考,尝试进一步利用application/config/routes.php功能,因此我设置了一些规则,文件变成了这样:

Awful headache here. Then, I started to think a bit and tried to further exploit the application/config/routes.php feature, so I set some rules, and the file became like this:

$route['default_controller'] = 'inicio';
$route['404_override'] = '';
$route['inicio'] = '';
$route['roles_usuarios/(:any)'] = 'roles/usuarios/$1';
$route['roles_editar/(:any)'] = 'roles/editar/$1';

voilà,该死的消息在奇怪的情况下停止显示,仅在我希望被显示时显示(当$ this-> uri-> segment(3)未插入URL或它的值时)如果将其用作数据库查询的参数,则该数据库中不存在

And voilà, the damn message stopped to be shown under strange circumstances, and only showed when I expected to be shown ( when $this->uri->segment(3) was not inserted in the URL, or when its value didn't exist in my database, in case of using it as a parameter for a database query ).

$ this-> uri-> segment(3)如何绕过if语句?

How is it possible for the $this->uri->segment(3) to bypass the if statement?

我设法发现这可能是路由问题,因为替换了

I managed to discover that this could be a routing issue, since replacing

$route['roles_usuarios/(:any)'] = 'roles/usuarios/$1';
$route['roles_editar/(:any)'] = 'roles/editar/$1';

作者

$route['roles/usuarios/(:any)'] = 'roles/usuarios/$1';
$route['roles/editar/(:any)'] = 'roles/editar/$1';

再次触发异常,我真的不想在我的网址中使用下划线.

triggers again the anomaly, and I really do not want to use underscores in my urls.

很高兴感谢您在这里提供的帮助.

I'd gladly give thanks to any helping hand out here.

更新:已解决.让我分享我的知识.

Update: Solved it. Let me share my knowledge.

使用@AdrienXL的指南,我设法找到了htaccess的工作方式,其中包括在route.php中明确显示三段式uri路由的代码行,并为其中的任何其他路由设置通用规则在之前的角色控制器之后.

Using @AdrienXL 's guide, I managed to find a way for the htaccess to work, among with expliciting the lines of code for 3-segmented uri routes in routes.php, and setting a generic rule for any other route in the roles controller after the previous ones.

这些是现在的文件:

.htaccess

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

routes.php

routes.php

$route['default_controller'] = 'inicio';
$route['404_override'] = '';
$route['inicio'] = '';
$route['roles/editar/(:num)'] = 'roles/editar/$1';
$route['roles/usuarios/(:num)'] = 'roles/usuarios/$1';
$route['roles/(:any)'] = 'roles/index';

现在,系统可以完美运行.谢谢!

Now the system works flawlessly. Thanks!

推荐答案

更新:已解决.让我分享我的知识.

Update: Solved it. Let me share my knowledge.

使用@AdrienXL的指南,我设法找到了htaccess的工作方式,其中包括在route.php中明确显示三段式uri路由的代码行,并为其中的任何其他路由设置通用规则在之前的角色控制器之后.

Using @AdrienXL 's guide, I managed to find a way for the htaccess to work, among with expliciting the lines of code for 3-segmented uri routes in routes.php, and setting a generic rule for any other route in the roles controller after the previous ones.

这些是现在的文件:

.htaccess

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

routes.php

routes.php

$route['default_controller'] = 'inicio';
$route['404_override'] = '';
$route['inicio'] = '';
$route['roles/editar/(:num)'] = 'roles/editar/$1';
$route['roles/usuarios/(:num)'] = 'roles/usuarios/$1';
$route['roles/(:any)'] = 'roles/index';

现在,系统可以完美运行.谢谢!

Now the system works flawlessly. Thanks!

这篇关于CodeIgniter URI参数部分绕过"if"消息.陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆