Codeigniter 安全检查 uri 是否为数字,因为分页

发布于 2024-10-09 11:55:35 字数 1194 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2024-10-16 11:55:35

这是我的简单解决方案:

if($this->uri->segment(3) == '')
        {
            $segment_url = 0;
        }else{
            if(!is_numeric($this->uri->segment(3))){
            redirect('404');
            }else{
            $segment_url = $this->uri->segment(3);
            }
        }

但是如果有人对此有更好的想法或教程,请......

This is my simple solution:

if($this->uri->segment(3) == '')
        {
            $segment_url = 0;
        }else{
            if(!is_numeric($this->uri->segment(3))){
            redirect('404');
            }else{
            $segment_url = $this->uri->segment(3);
            }
        }

But if somebody have any better idea or tutorial on this please...

只涨不跌 2024-10-16 11:55:35

干得好 =)

答案 1 +

<?
$cadena = "sdfio9874673o4h784";

for ($i=0; $i<strlen($cadena); $i++) {
if (is_numeric($cadena[$i])) {$cadena2.=$cadena[$i];}
}

echo $cadena2;
?>

= 不错的代码

nice job =)

answer 1 +

<?
$cadena = "sdfio9874673o4h784";

for ($i=0; $i<strlen($cadena); $i++) {
if (is_numeric($cadena[$i])) {$cadena2.=$cadena[$i];}
}

echo $cadena2;
?>

= nice code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文