I know this may seem basic PHP to some people but believe it or not alot of people have trouble finding ways to create a random string.
I have figured out two ways to do this and have decided to put them on here just for a couple quick random string scripts in case anybody needs to use them as much as I often do (with my file hosting programs).
#1 - Allows alot more functionality than the next one as it lets you decide the beginning of the string and also the amount of characters you wish it to be. Not only does it allow those, but it allows you to choose exactly which characters you want.
CODE
//Random string PHP script.
//Created by Darren Jett / Jetteh
$rstring = "";
// That will be the beginning of the string. Leave blank if you don't want the
// string to begin with something. For instance, if you want your random string
// to begin with your company name, put the company name there.
$rstringlen = 20;
// Amount of characters you want your random string to be.
$rstringchars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789";
// The characters you want your random string to be made of.
// This can be anything
//Beginning the function.
while($rstringlen > 0)
{
$rstring .= substr($rstringchars,rand(0,strlen($rstringchars)),1);
$rstringlen -= 1;
}
echo $rstring;
// You may not want it echo'd but $rstring is now the full random string.
//Created by Darren Jett / Jetteh
$rstring = "";
// That will be the beginning of the string. Leave blank if you don't want the
// string to begin with something. For instance, if you want your random string
// to begin with your company name, put the company name there.
$rstringlen = 20;
// Amount of characters you want your random string to be.
$rstringchars = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789";
// The characters you want your random string to be made of.
// This can be anything
//Beginning the function.
while($rstringlen > 0)
{
$rstring .= substr($rstringchars,rand(0,strlen($rstringchars)),1);
$rstringlen -= 1;
}
echo $rstring;
// You may not want it echo'd but $rstring is now the full random string.
Will return something like this: O0om2bmJyfXzf8nJVAoI
#2 This is a shorter yet less customizable way to do it, though I have figured this always works.
Please note that if you actually use your session ID's for something important this may not be the best choice.
CODE
//Random string PHP script.
//Created by Darren Jett / Jetteh
session_start();
// This needs to be at the TOP of your entire page, before any headers are sent.
// Also, if you already have a session enabled just delete the above line.
session_regenerate_id();
// Regenerates the session_id so that if the same user uses the script agian it
// will give them a new random string.
$rstring = session_id();
// This gets a 32 length session id - Session ID's are completely random.
$rstringlen = 20;
// This is how long you want your random string to be. MUST be below 32 or else
// this will not work.
$rstring = substr($rstring,0,$rstringlen);
// Shortens the string to your preferred length.
echo $rstring;
// You may not want this echo'd but $rstring is your full random string.
session_destroy();
// ONLY use this if you don't already have a session enabled. Otherwise, delete it.
//Created by Darren Jett / Jetteh
session_start();
// This needs to be at the TOP of your entire page, before any headers are sent.
// Also, if you already have a session enabled just delete the above line.
session_regenerate_id();
// Regenerates the session_id so that if the same user uses the script agian it
// will give them a new random string.
$rstring = session_id();
// This gets a 32 length session id - Session ID's are completely random.
$rstringlen = 20;
// This is how long you want your random string to be. MUST be below 32 or else
// this will not work.
$rstring = substr($rstring,0,$rstringlen);
// Shortens the string to your preferred length.
echo $rstring;
// You may not want this echo'd but $rstring is your full random string.
session_destroy();
// ONLY use this if you don't already have a session enabled. Otherwise, delete it.
Will display something like this: e9b688e49590d8047948;
You may not see a big difference, as both are completely random but the first one has upper and lowercase letters allowing for more randomness.
Hopes this helps people.