Advertisement
Guest User

Untitled

a guest
Feb 6th, 2011
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /**
  2. * Verify Cross Site Request Forgery Protection
  3. *
  4. * @access public
  5. * @return null
  6. */
  7. public function csrf_verify()
  8. {
  9. // If no POST data exists we will set the CSRF cookie
  10. if (count($_POST) == 0)
  11. {
  12. return $this->csrf_set_cookie();
  13. }
  14.  
  15. // Do the tokens exist in both the _POST and _COOKIE arrays?
  16. if ( ! isset($_POST[$this->csrf_token_name]) OR ! isset($_COOKIE[$this->csrf_cookie_name]))
  17. {
  18. $this->csrf_remove_cookie();
  19. $this->csrf_show_error();
  20. }
  21.  
  22. // Do the tokens match?
  23. if ($_POST[$this->csrf_token_name] != $_COOKIE[$this->csrf_cookie_name])
  24. {
  25. $this->csrf_remove_cookie();
  26. $this->csrf_show_error();
  27. }
  28. else
  29. {
  30. // Set hash as empty so new one is generated if form validation fails, or when token is verified.
  31. $this->csrf_hash = '';
  32. }
  33.  
  34. // We kill this since we're done and we don't want to polute the _POST array
  35. unset($_POST[$this->csrf_token_name]);
  36.  
  37. // Nothing should last forever
  38. unset($_COOKIE[$this->csrf_cookie_name]);
  39.  
  40. $this->_csrf_set_hash();
  41. $this->csrf_set_cookie();
  42.  
  43. log_message('debug', "CSRF token verified ");
  44. }
  45.  
  46. // --------------------------------------------------------------------
  47.  
  48. /**
  49. * Remove Cross Site Request Forgery Protection Cookie
  50. *
  51. * @access public
  52. * @return null
  53. */
  54. public function csrf_remove_cookie()
  55. {
  56. $expire = time() - 60;
  57.  
  58. setcookie($this->csrf_cookie_name, '', $expire, config_item('cookie_path'), config_item('cookie_domain'), 0);
  59.  
  60. log_message('debug', "CRSF cookie Removed");
  61. }
  62.  
  63. // --------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement