adrianle Posted March 10, 2010 Share Posted March 10, 2010 hey all.. I need to come up with a PHP chunk that randomly generates a 6 digit "password" and then assigns it to a session variable of my choice. Any pointers?? Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/ Share on other sites More sharing options...
TeddyKiller Posted March 10, 2010 Share Posted March 10, 2010 $password = rand(111111, 999999); $_SESSION['sessioname'] = $password; If you'd like something slightly more complicated with numbers, and letters.. then I could provide something, though that should be the basics. Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024468 Share on other sites More sharing options...
cags Posted March 10, 2010 Share Posted March 10, 2010 Simple example of a slightly more complicated system... $valid_chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; echo generate_password( 6, $valid_chars ); function generate_password( $length, $valid_chars ) { $count = strlen( $valid_chars ) - 1; $pass = ''; for( $i = 0; $i < $length; $i++ ) { $pass .= $valid_chars[rand( 0, $count)]; } return $pass; } Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024470 Share on other sites More sharing options...
adrianle Posted March 11, 2010 Author Share Posted March 11, 2010 Thanks guys.. now how do I display (print?) that session variable value on a subsequent php page? I know it's simple but I'm having trouble finding the answer in the docs.. Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024498 Share on other sites More sharing options...
TeddyKiller Posted March 11, 2010 Share Posted March 11, 2010 This is probably your answer. Or you can echo it, by just replacing the word "print" with "echo" print $_SESSION['sessioname']; If you're doing the method cags is doing, just asign $pass to a session variable as stated in my tiny example Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024501 Share on other sites More sharing options...
adrianle Posted March 11, 2010 Author Share Posted March 11, 2010 Odd.. that's exactly how I coded it and it's just not showing on the page... total blank space.. grrr Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024512 Share on other sites More sharing options...
adrianle Posted March 11, 2010 Author Share Posted March 11, 2010 $valid_chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; echo generate_password( 6, $valid_chars ); function generate_password( $length, $valid_chars ) { $count = strlen( $valid_chars ) - 1; $pass = ''; for( $i = 0; $i < $length; $i++ ) { $pass .= $valid_chars[rand( 0, $count)]; } return $pass;} $_SESSION['PSSWD'] = $pass; This DOES generate the value.. I can see it on this page.. but on the next page where it should pull form the session variable "PSSWD" it doesn't display anything! Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024513 Share on other sites More sharing options...
cags Posted March 11, 2010 Share Posted March 11, 2010 You have session_start at the top of both pages? Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024519 Share on other sites More sharing options...
adrianle Posted March 11, 2010 Author Share Posted March 11, 2010 Yes I do... Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024528 Share on other sites More sharing options...
adrianle Posted March 11, 2010 Author Share Posted March 11, 2010 OK, very odd.. I've got it working.. mostly.. but the value of the session variable that gets generated and then inserted into the table record, does NOT match the 6-character session variable value displayed on the next page!! It's almost like the second page is displaying a separately generated variable value .. but why?? All I'm asking it to do is echo the session variable....??? Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024541 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2010 Share Posted March 11, 2010 You need some conditional logic ( and if(){} statement) so that the value is only generated and assigned to the session variable once (if it is not set.) If you are unconditionally generating and assigning the value, it will be get changed on every page request. Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024543 Share on other sites More sharing options...
adrianle Posted March 11, 2010 Author Share Posted March 11, 2010 ok.. makes sense.. unfortunately, I only know barely enough PHP to get into trouble.. can you point me to where I would find a reference to this method you're describing? Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024544 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2010 Share Posted March 11, 2010 if(!isset($_SESSION['your_variable_name_here'])){ // code to set the session variable only when it is not already set... } Quote Link to comment https://forums.phpfreaks.com/topic/194834-create-random-code-and-assign-to-a-session-variable/#findComment-1024548 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.