CodeIgniter Forums
Why is my 20 digit random number always the same? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Why is my 20 digit random number always the same? (/thread-53736.html)



Why is my 20 digit random number always the same? - El Forum - 08-06-2012

[eluser]Dandy_andy[/eluser]
I'm having some trouble with a random number that is not random. I am basically generating a unique 20 digit code but when it's written to the db, the same number appears (95% of the time). I'm wondering if this is down to some caching or something?

Controller:-
Code:
public function send($per_id = NULL) {

  $securitycode = $this->Enter_detail->gen_string();
  $this->load->model('Email');
  $this->load->model('Notifications');
  $data['type'] = 'Most popular';
  if ((!is_numeric($per_id)) or ($per_id < 0)) {$per_id = 0;} //get page number variable from URI and set if not unsigned integer
  if ($this->input->post('gift'))
   {
   $mathcode = strip_tags($this->input->post('code'));
   //first check if math captcha is correct
   if ($mathcode != $this->session->userdata('realcd'))
    {
    $error = $this->notificationmssgs->errors();
    $this->session->set_flashdata('error', $error[22]);
    redirect('home/page');
    }
   //check if member has sent per_id a message in last 24 hours
   if ($this->Enter_detail->check_if_gift_before($this->session->userdata('mem_id'), $per_id))
    {
    $error = $this->notificationmssgs->errors();
    $this->session->set_flashdata('error', $error[20]);
    redirect(base_url('home/page/search'));
    }  
   $filtermssg = $this->Messagefilter->filter($this->input->post('message'));
   $this->Enter_detail->submit_gift($this->input->post('gift'), $per_id, $filtermssg, $securitycode);
   $this->Email->add_notification($per_id, $this->session->userdata('mem_id'), 'has sent you a gift!');
   $this->Notifications->add_notification($per_id, $this->session->userdata('mem_id'), 'gift', $securitycode);
   $notification = $this->notificationmssgs->notifications();
   $this->session->set_flashdata('message', $notification[11]);
   redirect('home/page/search');
   }
  $data['type'] = $this->input->post('type');
  //check that member is permitted to message per_id
  if ($this->Extract_detail->check_if_banned($per_id)) {
   $error = $this->notificationmssgs->errors();
   $this->session->set_flashdata('error', $error[14]);
   redirect(base_url('home/page/search'));
   }
  //get username of per_id
  $data['username'] = $this->Extract_detail->get_username($per_id);
  $data['per_id'] = $per_id;
  
  //choose which gifts to load based on member selection
  $data['gifts'] = $this->Extract_detail->gift_selection($data['type']);
  
  $this->load->view('blocks/spacer');
  $this->load->view('blocks/notificationbar', $data);
  $this->load->view('gift-send', $data);
  $data['bottomscript'] = '[removed][removed]';
  $this->load->view('blocks/footer', $data);  

}//SEND

and the relevant functions are:-

Code:
public function gen_string() {

  $this->load->helper('string');
  return random_string('numeric', 20);

}//gen_string

public function submit_gift($id, $per_id, $message, $seccode) {

  $this->load->helper('date');
  $datetime = unix_to_human(time(), TRUE, 'eu');
  $data = array(
   'id' => $seccode,
   'gift_id' => $id,
   'mem_id' => $this->session->userdata('mem_id'),
   'per_id' => $per_id,
   'message' => $message,
   'datetime' => $datetime
   );
  $this->db->insert('network_gifts', $data);

}//submit_gift

Ignoring the fact that there are a few things I will do to optimise the scripts, why is it that when the DB is written, the same value appears for id?


Why is my 20 digit random number always the same? - El Forum - 08-06-2012

[eluser]skunkbad[/eluser]
Its probably a problem with the datatype of the field you are trying to store the number in.


Why is my 20 digit random number always the same? - El Forum - 08-06-2012

[eluser]Dandy_andy[/eluser]
Hi, thanks - it appears you are right. I had set the data type as a BIGINT(20) but changing the random generated number to 19 digits seems to have solved this problem. I am still a bit puzzled though as to why the 20 digit number cannot be stored properly in a BIGINT(20) field but problem solved. Thx.


Why is my 20 digit random number always the same? - El Forum - 08-06-2012

[eluser]Dandy_andy[/eluser]
I have just checked and in fact the number I was getting most of the time is the largest number possible using BIGINT as a field type - thanks again for your help. It's often the obvious things I don't check!