<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Random_string_generator extends CI_Controller {
public function index()
{
$length = 8;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$characters_length = strlen($characters);
$random_string = '';
for ($i = 0; $i < $length; $i++) {
$random_string .= $characters[rand(0, $characters_length - 1)];
}
echo $random_string;
}
}
- Start by defining the class
Random_string_generator
that extends CI_Controller
. - Create the
index
method within the class to generate the random string. - Set the desired length of the random string, in this case, it's 8 characters.
- Define the characters that can be used in the random string, including numbers, lowercase letters, and uppercase letters.
- Determine the length of the character set.
- Initialize an empty string to hold the random string.
- Use a for loop to iterate through the desired length and append a random character from the character set to the random string.
- Output the generated random string.
- Ensure that no direct script access is allowed by checking the constant
BASEPATH
.