What would be the best way to generate a random id, which include user_name of "stephen"?
It depends on what you want to us the random ID for.
Usually you'd want either one that changes every time its used (like for password validation) or one that stays the same for a specified time (like a session id).
Most of what you need for sessions and the like are already implemented in PHP functions.
If its something unusual then you can use the rand() function that Puckdropper mentioned.
What to note though is that the rand() function is useless unless you know the techniques for making a random number secure. Explaining this would not make sense unless we know how you're implenting that random number, and what for..
Heres an explanation of the two common uses:
Password authentication.
Most web apps store passowrds as a cryptographic hash. The hash is a "random" number of fixed length generated for any input number. The generated hash for the same number is always the same, yet guessing the input number from the hash is very hard.
PHP has functions for widely used hash algorithms like: md5 (not recommented) and sha1.
To authenticate the password, usually its sent as plain text to the server, then the server will create the hash of the password and compare it to the one in the database.
This method is very insecure, as the password is clearly visible when sent over the network, unless you're using an SSL connection.
A step up would be to create the hash of the password on the client using client side scripting. Then send the password hash over the network.
Heres some links to popular md5 and sha1 implementations in JavaScript.
http://pajhome.org.uk/crypt/md5/
http://www.movable-type.co.uk/scripts/SHA-1.html
http://www.cs.eku.edu/faculty/styer/...t/JS-SHA1.html
Unfortunately even if you send a hash of a password, its still suseptible to reply attacks. Someone sniffing the network can just view the password hash, and then resend this hash to log into the users account.
Therefore you will usually want to make sure your authentication mechanism takes a different authentication parameters each time.
How you do this is by adding a salt to the password and/or username each time it has to be authenticated. The salt is a random number that is appended to the input string before hashing the string.
So if the user has the password, "pass", then you could send the client the salt, "xyz" and the client has to make a hash of, "passxyz", and send the hash back to the server.
The server can then get the users password from the database, joe, and put it together with the salt, xyz, and make a hash. If the two hashes match then the user supplied the right password, even though it was never sent over the network.
The server can then change the salt for each authentication.
Now even though you have a hash of the password, and have salted the password before hashing it again, it still suseptible to "tampering".
An attacker could be sniffing the network and intercept the HTML password page being sent over HTTP to the user.
The attacker can then change the HTTP message, and thus the HTML page, to their liking before sending it on to the client. They could remove the javascript hashing mechanisms, and make the user send their password as plain text over the network so they can sniff it on its way back to your server.
The only way to stop this is to have the HTTP sent over an SSL connection (HTTPS). Unfortunately for most developers this is usually beyond their resources...
Browsers do have inbuilt mechanisms for password authentication defined by the HTTP protocol, like: basic authentication (I would NOT recommend), http digest authentication etc. but for developers these mecanisms are not only hard to use, but hard to control.
Session IDs:
Session IDs are suseptible to reply attacks no matter what you do. The session ID has to be sent from client to server each time to authenticate the current session. The more it is sent, the more chance someone can sniff it over the network and send it to the server trying to fool the server into giving them access.
Most implementation will also save the users IP and cross reference that with the session so only a valid IP range should be able to use a session. This is more useless then most think, IPs can be easily spoofed, and if you're sniffing the network anyways, it means youre on the same IP range?
Here's somethinig to read:
Heres a good read on session security:
http://www.cgisecurity.com/lib/SessionIDs.pdf
The best you can do with sessions is weed out the social engineering tactics (phising etc.) and brute force attacks (cracking) on session IDs as well as "traffic analysis" (good guesswork).
---
In your PHP code, when generating a unique ID for authentication in any way, some things to note are:
Never authenticate the raw unique ID. Always authenticate the HASH against your databse.
Its best to salt your Unique ID if you can (like for passwords) so that you aren't suseptible to reply attackes.
Make sure to try and eliminate brute force by giving the random number a larger range.
Example:
[PHP]$rand = sha1(rand(1, 100));[/PHP]
Thants a very weak random ID since its only 1/100. Even if its been hashed.
Do not follow a pattern that can be easily guessed through "traffic analysis". Hashing the date is an example of a bad habit for security. Hashing userid's is even worse, but it seems to be just as common. If you're using the date, only use it to lengthen your random ID, not as the random ID itself.
Note that hashes "cannot" be reversed, but they can be analyzed!
eg:
[PHP]// monday
$rand = sha1($password.'monday');
// tuesday
$rand = sha1($password.'tuesday');[/PHP]
Even though it looks good, even if you replace it with the timestamp, its still a very obvious pattern.