I can't use openid because my auth is not based on email id
OpenID does not depend on e-mail—an OpenID identity could be something like someone.example.com
.
Then insert ( useragent, ip, unique key which i create ). into a database.
It is unwise to tie your sessions to IP address. Some users' IP addresses will legitimately vary (for example because they are behind a proxy cluster, or are switching between mobile networks). Kicking them out on IP change would make your site very difficult to use for a subset of your audience.
Matching IP is in any case of very limited usefulness. The usual attack it is attempting to address is that of session token leakage and reuse from an attacker client. But possible causes of token leakage are generally those that give the attacker access to make requests from the client anyway (XSS, client-side malware, bad transport security) or those which are already catastrophic (database compromise, server code execution).
Similarly User-Agent matching is of little value as the UA string isn't any kind of secret and is easily spoofed. Matching IP and UA can be of some worth as just one input to a complex risk rating system with behavioural history, but primitive matching is ineffective as a security measure and likely to cause you more damage from false negatives than benefit.
Time stamp + ip + useragent + username and encrypt them many rounds = unique key !
Encrypt type = MCRYPT_RIJNDAEL_256
Then insert ( useragent, ip, unique key which i create ). into a database.
set the unique key in cookie and transmit it to user.
As the client-submitted ‘unique key’ is verified simply by comparing it against the known value in the database, its actual content is of no relevance and there is no point in generating it in such an elaborate way.
A string of random data would do just as well for this, and then what you'd have would be functionally equivalent to the normal way people do logins in PHP: by putting a logged-in-user-ID on the session. Re-using PHP's standard random-PHPSESSID-based sessions would have advantages in terms of performance and the well-understood nature of their configuration.
There is another somewhat-common model of authentication, which may be what you were thinking of with the ‘unique key’ idea. This is where you create a token based on items you want to authenticate (typically user ID, password/reset generation numbers and token expiry time) together with a cryptographic signature over those items (typically HMAC) using a server-side secret key. The server can recognise and authenticate the incoming token using that signature, giving it the advantage that it doesn't need any persistent database/session storage. But unless you need that particular property I would stick with plain old sessions.
i think using TLS is must here
Yes. And remember to give your cookies the secure
flag so they don't leak through a non-TLS request.