Skip to content

Generating alphanumeric unique ID in codeigniter

Last updated on October 8, 2014

Today i will show you how to generate alphanumeric unique ID using Codeigniter. Some times there is requirement to generate the unique id, But most of the time we are going to write our logic. But Codeigniter Framework Provides Helpers, which simplify our need. One of them is string Helper.

How to use CodeIgniter String Helper:

To use string helper you need to first load it, then use the random_string() function. Generates a random string based on the type and length you specify. Useful for creating passwords or generating random hashes.The first parameter specifies the type of string, the second parameter specifies the length.

$this->load->helper('string');
echo random_string('alnum',20);

The following choices are available: alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1

1) alpha: A string with lower and uppercase letters only.

2) alnum: Alpha-numeric string with lower and uppercase characters.

3) numeric: Numeric string.

4) nozero: Numeric string with no zeros.

5) unique: Encrypted with MD5 and uniqid(). Note: The length parameter is not available for this type. Returns a fixed length 32 character string.

6) sha1: An encrypted random number based on do_hash() from the security helper.

That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments