Codeigniter 安全检查 uri 是否为数字,因为分页
Codeigniter 安全性中的一件事是检查 uri 是否是数字或我们期望的东西! 代码如下:
$this->load->helper('form');
$this->load->library('pagination');
$config['permitted_uri_chars'] = '0-9';
$config['base_url'] = '/member/index';
$config['per_page'] = 5;
$config['num_links'] = 10;
$query_not_voted_rows = "SELECT p_id FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').")";
$config['total_rows'] = $this->db->query($query_not_voted_rows)->num_rows();
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
if($this->uri->segment(3) == '')
{
$segment_url = 0;
}else{
$segment_url = $this->uri->segment(3);
}
$query_not_voted = "SELECT * FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').") LIMIT ".$segment_url.", ".$config['per_page'];
但是如果现在有人在 uri 段中输入:“kdkdkdd”,那么 sql 就会中断,因为 $segment_url
是 kdkdkdd 而不是数字!
那么问题来了,如何避免这种情况呢?
One thing in the security of codeigniter is checking if the uri is a number or something that we expect!
Here is the code:
$this->load->helper('form');
$this->load->library('pagination');
$config['permitted_uri_chars'] = '0-9';
$config['base_url'] = '/member/index';
$config['per_page'] = 5;
$config['num_links'] = 10;
$query_not_voted_rows = "SELECT p_id FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').")";
$config['total_rows'] = $this->db->query($query_not_voted_rows)->num_rows();
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
if($this->uri->segment(3) == '')
{
$segment_url = 0;
}else{
$segment_url = $this->uri->segment(3);
}
$query_not_voted = "SELECT * FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').") LIMIT ".$segment_url.", ".$config['per_page'];
But if now someone enters in uri segment: "kdkdkdd", then the sql breaks, because $segment_url
is kdkdkdd and not the number!
So the question is, how to escape this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的简单解决方案:
但是如果有人对此有更好的想法或教程,请......
This is my simple solution:
But if somebody have any better idea or tutorial on this please...
干得好 =)
答案 1 +
= 不错的代码
nice job =)
answer 1 +
= nice code