Thursday, 9 February 2017

How to generate a Random String

Just Put this function into your Model.

function generateRandomStr($length = 15)
  {
    $char ='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charLength = strlen($char);
      $randomStr = '';
        for ($i = 0; $i < $length; $i++)
        {
          $randomStr .= $char[rand(0, $charLength - 1)];
        }
          return $randomStr;
  }


And put this code into the end of insert function to store randomstring into your database.

  $id = $this->db->insert_id(); $token = $this->generateRandomStr(); $data = array( 'token' => $token ); $this->db->where('id',$id); $this->db->limit('1'); $this->db->update('your table name',$data);






0 comments:

Post a Comment