Last Updated: February 25, 2016
·
1.205K
· chris15001900

Random string in php

Here's a function to create a random string dependent on time:

function generateString($length) {

$chars = 'ABCDEFGHIJKLMNOPRSTUWXYZabcdefghijklmnoprstuwxyz1234567890';
$generated = '';

$time = time();
$generated .= $time;

$gL = strlen($generated);

    if($gL >= $length) return substr($generated, 0, $length);

for($i=0; $i<$length-$gL; $i++) {

    $generated = $chars[rand(0,strlen($chars)-1)] . $generated;

}

return $generated;

}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside