Jump to content

Random String Generation


Recommended Posts

Hi everyone, im trying to generate a random string of 32 characters, Im very new to php and after googling for a solution I thought it best to ask here, I am starting a session and I want to generate a session id, here's what I have so far

<?php session_start(); 
$_SESSION['sessid']=''; 
?>

 

I understand I cant put a rand in the comma's so Im unsure what else to do, Any help id greatly appreciate

Link to comment
https://forums.phpfreaks.com/topic/200793-random-string-generation/
Share on other sites

perhaps...

<?PHP

#################################
#
#	This function is handy
#	for creating random 
#	file names
#
#################################

function randomkeys($length){
$pattern = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for($i=0;$i<$length;$i++){
	$key .= $pattern{rand(0,61)};
}
return $key;
}
$how_long = 6;
$new_name = randomkeys($how_long);
echo $new_name;
?>

Thanks everyone, I cant implement your code teamatronic, im sure im going wrong somewhere tho, does this look correct if id like to see the str?

<?php $_SESSION['sessid']= md5("$username".rand()); ?><?php echo '$sessid'; ?>

 

and in regards to the curly brakcets, if i just swap them out with square brakcets is that okay? or is there more to it?

 

Forgive me for being so 'amateur' but I still cant set my session id, im using

<?php
    if (session_id() == "") session_start(); 
?>

 

how do i echo that if it has no variable defined? im trying to use the php.net manual but its too complicated

Okay ive managed to set and echo my session string now, the only thing is, if i post it to another page and go back the string changes, I guess this is the random fucntion. Is there a way to set it? so it wont change unless the session is destroyed?

 

im using

 

<?php
    if (session_id() == "") session_start(); 
    $_SESSION['ses_id']= md5(rand());
?>

My head is frazzled, I cant figure it out no matter what I try!!

 

Ill try to put the problem into context.

 

User visits my webpage > clicks add to cart button which adds product to the cart (and also echo's session id) > Views products in cart, then clicks 'Products' > The session id being echo'd is now different and when he visits the cart everythings gone.

 

I need to find a way to set the session.

My stomachs in knots its so confusing, Any help would be brilliant, Thanks

By the way, you shouldn't use { } to access characters in a string, this is depreciated as of PHP 5.3.0, instead use square brackets ([ ]).

 

Will the backets work in previous versions?

Yes, square brackets will work in previous versions.

 

You must call session_start always, otherwise you won't be able to access the $_SESSION array.

 

<?php
session_start();
if(!isset($_SESSION['ses_id']))
{
    $_SESSION['ses_id'] = md5(rand());
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.