如何防止用户更改URL中uri的ID? Codeigniter

问题描述 投票:0回答:2

出于一种保护我的页面的原因,我需要阻止用户更改URL中的ID。例如,我有以下URL:“ localhost / ci_app / classes / view_create / 4”因此,ID 4不得更改为任何ID号。有没有办法做到这一点?

此代码有效,但是每次我将4替换为$ id时,它都将不起作用。请提供此问题的解决方案。

查看

<?php if($this->uri->segment(3) == 4): ?>
   <?php echo form_open('classes/create_posts/'.$id) ?>                     
        <div class="form-group">
            <label><h5> Post Activity Name: </h5></label>
            <input type="text" placeholder="Enter Activity Name" name="act_name" id="act_name" class="form-control" required />
        </div>

        <hr>

        <button type="submit" class="btn btn-block btn-info">Send Activity <span class="iconify" data-icon="ant-design:send-outlined" data-inline="false"></span></button>
   </form>
<?php endif; ?>

Controller

public function view_create($id)
{   

    // Check login
    if(!$this->session->userdata('logged_in')){
        redirect('users/teacher_login');
    }


    $data['id'] = $id;


    $this->load->view('templates/header');
    $this->load->view('teachers/group/create', $data);
    $this->load->view('templates/footer');          
}
php codeigniter codeigniter-3
2个回答
0
投票

没有办法防止这种情况。如果$ id不正确,您可以做的是向用户显示404消息。可以在调用视图之前在控制器中完成此操作。

例如

if($id != 4) { 
    $this->load->view('NotFoundView'); 
}

0
投票

您将永远无法阻止用户更改URL,但是您几乎不可能找到其他页面。

考虑使用不是数字的ID,例如GUID / UUID(https://en.wikipedia.org/wiki/Universally_unique_identifier)。它们是数字和字母的字符串,它们是如此独特,以至于您无法猜到它们。如果使用这些,即使用户更改了URL,他们也将找不到其他页面。

© www.soinside.com 2019 - 2024. All rights reserved.