RaythMistwalker Posted January 4, 2010 Share Posted January 4, 2010 How would i get php to randomly select an 8 digit number from 00000000 to 99999999? I need this for my user recognition, when a user logs in a random number will be generated and saved to the database, this way if a hacker gets in using userid the system will remove them if they don't have the same 8 digit code. Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/ Share on other sites More sharing options...
Psycho Posted January 4, 2010 Share Posted January 4, 2010 $num = sprintf("%08s", (rand(0, 99999999)); OR $num = str_pad(rand(1, 99999999), 8, '0', STR_PAD_LEFT); Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/#findComment-988275 Share on other sites More sharing options...
teamatomic Posted January 4, 2010 Share Posted January 4, 2010 Generate the number in the normal random fashion then use str_pad to bring it out to eight digits. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/#findComment-988276 Share on other sites More sharing options...
RaythMistwalker Posted January 4, 2010 Author Share Posted January 4, 2010 $num = strintf("%08s", (rand(0, 99999999)); is i change the %08s to %16s and add 8 more 9's would that be correct syntax for a 16 digit number? Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/#findComment-988279 Share on other sites More sharing options...
Psycho Posted January 4, 2010 Share Posted January 4, 2010 $num = strintf("%08s", (rand(0, 99999999)); is i change the %08s to %16s and add 8 more 9's would that be correct syntax for a 16 digit number? No, it wouldn't - the '0' is the character to be padded. Also, I had a minor typo which I fixed in my previous post. $num = strintf("%016s", (rand(0, 9999999999999999)); OR $num = str_pad(rand(1, 9999999999999999), 16, '0', STR_PAD_LEFT); Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/#findComment-988285 Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 $num = sprintf("%08s", (rand(0, 99999999)); Is missing a ) Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/#findComment-988291 Share on other sites More sharing options...
roopurt18 Posted January 4, 2010 Share Posted January 4, 2010 I need this for my user recognition, when a user logs in a random number will be generated and saved to the database, this way if a hacker gets in using userid the system will remove them if they don't have the same 8 digit code. Or if a hacker gets in they will be able to prevent the user from logging in and changing their password. A savvy hacker could even log in as a user and then use wget to send requests to your server on a timer so the session never ends. Then the legitimate user would be locked out permanently. Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/#findComment-988295 Share on other sites More sharing options...
RaythMistwalker Posted January 4, 2010 Author Share Posted January 4, 2010 I need this for my user recognition, when a user logs in a random number will be generated and saved to the database, this way if a hacker gets in using userid the system will remove them if they don't have the same 8 digit code. Or if a hacker gets in they will be able to prevent the user from logging in and changing their password. A savvy hacker could even log in as a user and then use wget to send requests to your server on a timer so the session never ends. Then the legitimate user would be locked out permanently. ATM the users don't have access to password changing and all the users atm are my close friends if the pass changed could just ask me what it is and i can sort it. Is there a way to automatically expire the 8/16 digit number if it is set as $_SESSION['random_id'] Quote Link to comment https://forums.phpfreaks.com/topic/187149-random-number/#findComment-988396 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.