Jump to content

random number?


RaythMistwalker

Recommended Posts

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.

Link to comment
Share on other sites

$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);

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']

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.