Guest User

Untitled

a guest
Oct 26th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // validate login
  5. if(isset($_POST['submit'])){
  6. ob_end_clean(); // buffer clean
  7.  
  8. validate($_POST['username'],$_POST['password']);
  9. }
  10.  
  11. //validate cookies and update status
  12. if(isset($_POST['addstatussubmit'])){
  13. ob_end_clean(); // buffer clean
  14.  
  15. $cookieNameCsrf = "csrfTokenCookie";
  16. validateStatus($_POST['token_csrf'],$_COOKIE[$cookieNameCsrf]);
  17. }
  18.  
  19. //generate csrf token
  20. function generateToken($sessionCookie){
  21. if(empty($_SESSION['random_key'])){
  22. $_SESSION['random_key'] = bin2hex(random_bytes(32));
  23. }
  24.  
  25. $token = hash_hmac('sha256',$sessionCookie,$_SESSION['random_key']);
  26.  
  27. $sessionID = session_id();
  28.  
  29. $expireTime = time() + 60*60; // expire time 1 hour
  30. $cookieNameCsrf = "csrfTokenCookie";
  31.  
  32. setcookie ($cookieNameCsrf, $token, $expireTime, "/","localhost", FALSE, TRUE);
  33.  
  34. ob_start(); // store in buffer
  35. echo $token;
  36. }
  37.  
  38. //validate cookie
  39. function validate($username,$password){
  40. /**
  41. * For demo ,
  42. * Username : user
  43. * Password : user
  44. */
  45.  
  46. if($username == "user" && $password == "user"){
  47. $cookieName = "sessionCookie";
  48.  
  49. generateToken($_COOKIE[$cookieName]);
  50.  
  51. echo "<script> alert('Successfully Logged In') </script>";
  52. echo "<script type=\"text/javascript\"> window.location.href = 'client.php';</script>";
  53.  
  54. }else{
  55. echo "<script> alert('Login failed! Check username and password again !!!') </script>";
  56. echo "<script type=\"text/javascript\"> window.location.href = 'index.php';</script>";
  57. }
  58. }
  59.  
  60. //validate status
  61. function validateStatus($token,$csrfCookie){
  62. if($token == $csrfCookie){
  63. echo "<script> alert('Status successfully added') </script>";
  64. echo "<script type=\"text/javascript\"> window.location.href = 'client.php';</script>";
  65. }else{
  66. echo "<script> alert('Status posting failed! CSRF token not matched !!!') </script>";
  67. echo "<script type=\"text/javascript\"> window.location.href = 'client.php';</script>";
  68. }
  69. }
  70.  
  71. ?>
Add Comment
Please, Sign In to add comment