如何防止用户更改/删除其他用户数据

时间:2011-05-21 01:23:25

标签: php mysql codeigniter multi-user

我正在创建多用户应用程序。使用php codeigniter创建我正在寻求最佳实践或阻止用户更改/删除其他用户数据的最佳方法。

我有一些关系表。

  1. tbl_users(user_id,user_name,password)
  2. tbl_stores(store_id,user_id,store_name,store_url)
  3. tbs_products(product_id,store_id,product_name)
  4. 每个用户都有自己的商店,每个商店可能有很多产品。

    那么,阻止用户删除其他用户数据的最佳方法是什么?

    实际上,我已经在模型中创建了一些代码,但由于我是新手,我不确定,如果这是正确的方法,我的代码就是这样。

    function remove_product($product_id)
    {
        $this->db->from('products as p');
        $this->db->join('stores as s', 's.id = p.store_id');
        $this->db->where('p.id', $product_id);
        $this->db->where('s.user_id', $this->session->userdata('id'));
    
        if($this->db->count_all_results())
        {
            $this->db->where('id', $product_id);
            return $this->db->delete($this->table);
        }
    }
    

    请告知此方法是否正确,或者是否有更好的方法来执行此操作。

    此致

1 个答案:

答案 0 :(得分:1)

在删除某些内容之前,请检查其所有者是否与会话用户相同。

相关问题