liamloveslearning Posted May 5, 2010 Share Posted May 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/ Share on other sites More sharing options...
litebearer Posted May 5, 2010 Share Posted May 5, 2010 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053590 Share on other sites More sharing options...
liamloveslearning Posted May 5, 2010 Author Share Posted May 5, 2010 works like a charm! thanks Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053607 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 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 ([ ]). Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053609 Share on other sites More sharing options...
teamatomic Posted May 5, 2010 Share Posted May 5, 2010 Depending on where in your entry/login sequence you want to set the id. $_SESSION['sessid']= md5("$username".rand()); or $ip=$_SERVER['REMOTE_ADDR']; $_SESSION['sessid']= md5("$ip".rand()); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053613 Share on other sites More sharing options...
liamloveslearning Posted May 5, 2010 Author Share Posted May 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053616 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 Single quotes don't have variable interpolation, and if you're just echoing the variable you don't need quotes at all. <?php echo $sessid; ?> Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053620 Share on other sites More sharing options...
liamloveslearning Posted May 5, 2010 Author Share Posted May 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053629 Share on other sites More sharing options...
liamloveslearning Posted May 5, 2010 Author Share Posted May 5, 2010 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053640 Share on other sites More sharing options...
litebearer Posted May 5, 2010 Share Posted May 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053647 Share on other sites More sharing options...
liamloveslearning Posted May 5, 2010 Author Share Posted May 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053658 Share on other sites More sharing options...
teamatomic Posted May 5, 2010 Share Posted May 5, 2010 Start using brackets to block your if statements. when you use an if statement with no brackets only the first statement following it is considered in the if. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053724 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 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()); } Quote Link to comment https://forums.phpfreaks.com/topic/200793-random-string-generation/#findComment-1053743 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.