Recommend this page to a friend! |
Classes of Nicola Covolo | PHP CSRF Protection class | README.md | Download |
|
DownloadCSRFProtectorProtect against CSRF attack. PHP >= 5.4 IntroductionCross-site request forgery, also known as a one-click attack or session riding and abbreviated as CSRF (sometimes pronounced sea-surf) or XSRF, is a type of malicious exploit of a website whereby unauthorized commands are transmitted from a user that the website trusts. Unlike cross-site scripting (XSS), which exploits the trust a user has for a particular site, CSRF exploits the trust that a site has in a user's browser. This class can be usefull to also avoid some sort of javascript scripts that attemps a human simulation or a DOS attack. Why I should use this class?Most of others PHP scripts require that you manually edit link and form one by one. In medium and big size application, this is not only stressful but also dangerous because as human you can do mistakes. CSRFProtector, instead, do the job automatically! Just before the end of the scripts, it search in the output buffered each links and forms. Then, they are modified adding a speacial randomic token: tokens are then saved in sessions to create a white list. When a web request come to your server, CSFRProtector check if the associated token is in the permitted list: if yes then the script can continue, otherwise a error is shown. Not only: it also add a flag in session with the end time of script execution and you can choose when the next request is accepted. So sum up:
To do:
InstallationFirst off all, download and unzip all the contents in a folder in your server. Let's suppose is libs. At the begin of your main script, add this code
That is all! Anyway it's more powerfull than what might seem. Advanced configurationsThe construct can take three optional arguments:
It's also possible to manually protect GET and POST data using fews function:
|