Skip to content
Advertisement

Token for prevention of CSRF doesn’t work on live server

I wrote a script which generates an authentication token in order to prevent CSRF attacks. It works well on local server but returns the 403 error on live server

Here is the code that checks if a token already exists or not

JavaScript

And here is the code that validates the token upon submission

JavaScript

UPDATE

I checked in on my error_log.php and this was the error displayed

session_start(): Cannot start session when headers already sent in /home/refermec/public_html/user/login.php on line 15

I have the same code as stated earlier in all pages with a form that requires authentication

Advertisement

Answer

According to your error, you have content ABOVE session_start(); Once any content, no matter a HTML comment, an echo, a header() happens before session_start();, php will throw that error.

All these things need to come AFTER the session_start();

Make sure session_start(); is at the top of the file, or at the top of an included file.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement