• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is my 20 digit random number always the same?

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?

public function send($per_id = NULL) {

  $securitycode = $this->Enter_detail->gen_string();
  $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]);
   //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]);
   $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]);
  $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]);
  //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/notificationbar', $data);
  $this->load->view('gift-send', $data);
  $data['bottomscript'] = '[removed][removed]';
  $this->load->view('blocks/footer', $data);  


and the relevant functions are:-

public function gen_string() {

  return random_string('numeric', 20);


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

  $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);


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?

Its probably a problem with the datatype of the field you are trying to store the number in.

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.

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!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.