【问题标题】:CodeIgniter – Ban IP and store it into Database and check databaseCodeIgniter - 禁止 IP 并将其存储到数据库并检查数据库
【发布时间】:2014-03-22 01:20:09
【问题描述】:

我正在使用 CodeIgniter 开发一个网站;我正在制作一个投票网站。问题是有些条目实际上让我一次又一次地投票。他们一直在数据库中禁止 IP。

我的测试控制器:

class Vote extends CI_Controller {

function __construct()
{
    parent::__construct();
}

public function index()
{

}

public function vote_now()
{
    if ($this->uri->segment(2)){

        $q = $this->db->query('SELECT * FROM banned_ip WHERE entry='.$this->uri->segment(2).' LIMIT 1');
        $row = $q->row_array();

        $qe = $this->db->query('SELECT * FROM entries WHERE ID='.$this->uri->segment(2).' LIMIT 1');
        $r = $qe->row_array();

        if($row['IP'] == $this->input->ip_address()){

            echo 'Already Voted.';

        }

        else {

            $insert_data_votes = array(
            'votes' => $r['votes']+1,
            );
            $this->db->where('ID', $this->uri->segment(2))->update('entries', $insert_data_votes);

            $insert_data = array(
            'IP' => $this->input->ip_address(),
            'entry' => $this->uri->segment(2),
            );
            $this->db->insert('banned_ip', $insert_data);

            redirect('foto/'.$this->uri->segment(2).'', 'refresh');

        }

    }
}}

有人知道这是什么问题吗?

谢谢。

【问题讨论】:

  • 您期望$this->uri->segment(2) 中的价值类型是什么?
  • 那是要投票的 fotoID。我需要检查 fotoID 是否附有禁止 IP。
  • 我的 Bans 数据库是.. 条目(照片)和 IP

标签: php database codeigniter vote


【解决方案1】:

在 id 不是 INT 的情况下尝试这样,最好作为函数参数传递而不是 $this->uri->segment(2)

public function vote_now($id = '') {
    $id = (int) $id;
    if ($id > 0) {
        $q = $this->db->select('IP')
                ->from('banned_ip')
                ->where('entry', $id)
                ->where('IP', $this->input->ip_address())
                ->get();

        if ($q->num_rows() > 0) {
            echo 'Already Voted.';
        } else {

            $qe = $this->db->query("SELECT * FROM entries WHERE ID='$id' LIMIT 1");
            $r = $qe->row_array();

            $insert_data_votes = array(
                'votes' => $r['votes'] + 1,
            );
            $this->db->where('ID', $id)->update('entries', $insert_data_votes);

            $insert_data = array(
                'IP' => $this->input->ip_address(),
                'entry' => $id,
            );
            $this->db->insert('banned_ip', $insert_data);

            redirect('foto/' . $id, 'refresh');
        }
    }
}

【讨论】:

  • 你的代码有一个小错误,但它就像一个魅力!谢谢明哈兹·艾哈迈德! $this->db->where('ID', $q) 把 $q 改成 $id
  • 我修改了答案,我没有意识到我错过了这个,谢谢:)
猜你喜欢
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
  • 2016-10-12
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 2018-03-06
相关资源
最近更新 更多