我有如下這個基本功能:添加一個檢查到PHP函數?
function buy()
{
$item_id = (int)$this->uri->segment(3);
if ($item_id > '0')
{
$item = $this->db->where('shop_id', $item_id)->get('shop')->row();
if ($item)
{
$player = $this->user->info($this->user->id());
if ($player->users_money >= $item->shop_req_money && $player->users_credits >= $item->shop_req_credits)
{
$this->db->update('users_items', array('users_id' => $this->user->id(), 'users_motors_id' => '0'), array('users_items_id' => $item->users_items_id));
$this->db->update('users', array('users_money' => $player->users_money - $item->shop_req_money, 'users_credits' => $player->users_credits - $item->shop_req_credits), array('users_id' => $this->user->id()));
$this->db->query('UPDATE users SET users_money=users_money+' . $item->shop_req_money . ', users_credits=users_credits+' . $item->shop_req_credits . ' WHERE users_id=' . $this->db->escape($item->shop_users_id));
$this->db->delete('shop', array('shop_id' => $item->shop_id));
$this->session->set_flashdata('success', true);
}
else
$this->session->set_flashdata('error', true);
}
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
,我編輯它像添加額外的檢查,如果REG IP是不一樣的另外一個如果是設置一個錯誤信息。
function buy()
{
$item_id = (int)$this->uri->segment(3);
if ($item_id > '0')
{
$item = $this->db->where('shop_id', $item_id)->get('shop')->row();
if ($item)
{
$player = $this->user->info($this->user->id());
$players = $this->user->info($item->shop_users_id);
if ($players->users_reg_ip === $player->users_reg_ip)
{
$this->session->set_flashdata('errorip', true);
}
elseif ($player->users_money >= $item->shop_req_money && $player->users_credits >= $item->shop_req_credits)
{
$this->db->update('users_items', array('users_id' => $this->user->id(), 'users_motors_id' => '0'), array('users_items_id' => $item->users_items_id));
$this->db->update('users', array('users_money' => $player->users_money - $item->shop_req_money, 'users_credits' => $player->users_credits - $item->shop_req_credits), array('users_id' => $this->user->id()));
$this->db->query('UPDATE users SET users_money=users_money+' . $item->shop_req_money . ', users_credits=users_credits+' . $item->shop_req_credits . ' WHERE users_id=' . $this->db->escape($item->shop_users_id));
$this->db->delete('shop', array('shop_id' => $item->shop_id));
$this->session->set_flashdata('success', true);
}
else
$this->session->set_flashdata('error', true);
}
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
它的工作原理事情是這樣的,但基本上我問如果多數民衆贊成這樣做,還是應該有這一點,但更好的別的東西,我可以做similiar的正確方法是什麼?如果對我的問題有任何意見,我會非常感激。提前致謝!
'檢查reg ip是否與另一個'不一樣,但是在條件'$ players-> users_reg_ip === $ player-> users_reg_ip'中。相反,也許'!==' – Ghost 2014-09-23 03:22:32
我可以直接將它包含在elseif語句中,但在這種情況下,如果他沒有信用/金錢並且他試圖從相同的IP購買,它將顯示相同的錯誤。所以,如果用戶沒有錢或信用卡,只是'錯誤'被設置爲產生錯誤信息,但如果他想從同一個IP購買,我想添加一個名爲'errorip'的額外錯誤信息,你明白我的意思? – MobEn 2014-09-23 03:24:59
哦,沒關係,既然你給了上下文,順便說一句,現在我明白了嗎?這是CodeIgniter嗎? – Ghost 2014-09-23 03:29:29