views:

113

answers:

2

For security purposes, I would like to create a security token for every CRUD operation on a site running a LAMP stack. The security token would be attached to the a href URL and would be verified after the user clicks on the link before displaying or doing anything.

First of all, are using security tokens a good first step in securing the application, or are there better alternatives?

Second, what is the best way to do this with PHP? Any recommendations and especially code would be helpful. I am new to using security tokens, so any explanations would be great.

(By best, I mean most secure and usable at the same time)

+2  A: 

You might use uniqid:

$token = md5(uniqid(mt_rand(), true));
erenon
How would I verify this token on the next page?
chris
@chris: What do you mean exaclty? There are a lot of way to do this, depends on the requirments. You could store it in the $_SESSION superglobal or in your DB.
erenon
Just as the first comment on the uniqid manual page notes: you shouldn't `md5` a uniqid, just use it as it is.
deceze
A: 

Won't stop a XSS attack, since the hacker can grok your HTML and extract the href's with security tokens in them.

peufeu
if it is a security number that can only be used once, then the hacker can only use it once. I've heard that wordpress uses something like this. Do you know if there is there a better way?
chris