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
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;
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.