Advertisement
Guest User

php voorbeeld

a guest
Nov 8th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.30 KB | None | 0 0
  1. <?php
  2. include "database.php";
  3. $content = false;
  4. $game;
  5. $page = isset($_GET['page']) ? $_GET['page'] : null;
  6. $action = isset($_GET['action']) ? $_GET['action'] : null;
  7. $username = isset($_POST['name']) ? sanatize($_POST['name']) : null;
  8. $password = isset($_POST['wachtwoord']) ? sanatize($_POST['wachtwoord']) : null;
  9. $gameActionResult;
  10. $user;
  11.  
  12. switch($page)
  13. {
  14.     case "register":
  15.         $content = "create_account.php";
  16.         break;
  17.     case "register_check":
  18.         $hash = password_hash($password, PASSWORD_BCRYPT);
  19.         $sql = "INSERT INTO user_accounts (username, hash)
  20.        VALUES ('{$username}', '{$hash}')";
  21.         if ($conn->query($sql) === FALSE)
  22.         {
  23.             echo "Error: " . $sql . "<br>" . $conn->error;
  24.         }
  25.         else {
  26.             header("Location: " . "index.php?page=login");
  27.         }
  28.     break;
  29.     case "login":
  30.         $content = "login.php";
  31.         break;
  32.     case "login_check":
  33.         $sql = "SELECT hash FROM user_accounts WHERE username='{$username}'";
  34.         $result = $conn->query($sql);
  35.         if ($result->num_rows > 0)
  36.         {
  37.             $row = $result->fetch_assoc();
  38.             if(password_verify($password, $row['hash']))
  39.             {
  40.                 $length = 50;
  41.                 $token = bin2hex(random_bytes($length));
  42.                
  43.                 $sql = "UPDATE user_accounts SET token='{$token}' WHERE username='{$username}'";
  44.                 if ($conn->query($sql) === TRUE) {
  45.                     //echo "Token Set";
  46.                 }
  47.                 setcookie("token", $token, time() + (86400 * 30), "/");
  48.                 header('Location: index.php?page=game');
  49.             }
  50.             else
  51.             {
  52.                 echo "Verkeerd";
  53.             }
  54.         } else {
  55.             echo "Geen account met die naam gevonden";
  56.         }
  57.     break;
  58.     case "logout":
  59.         $content = "logout.php";
  60.         break;
  61.     case "check_login":
  62.             require "logcheck.php";
  63.             break;
  64.     case "register_user":
  65.  
  66.             break;
  67.     case "game":
  68.    
  69.        
  70.         $game = "game.php";
  71.         $token = isset($_COOKIE['token']) ? $_COOKIE['token'] : null;
  72.         if ($token != null) {
  73.             $sql = "SELECT * FROM user_accounts WHERE token='{$token}'";
  74.             $result = $conn->query($sql);
  75.             if ($result->num_rows > 0) {
  76.                 $user = $result->fetch_assoc();
  77.             } else {
  78.                 header("Location: index.php?page=login");
  79.             }
  80.         } else {
  81.             header("Location: index.php?page=login");
  82.         }
  83.         switch ($action)
  84.         {
  85.             case "Gather":
  86.                 if ($user['actions'] > 0) {
  87.                     $user['gold'] += 2;
  88.                     $user['actions'] -= 1;
  89.                    
  90.                 } else {
  91.                     echo "niet genoeg acties";
  92.                 }
  93.                 $content = "Gather.php";
  94.                 break;
  95.             case "Adventure":
  96.                     require "adventure.php";
  97.                 break;
  98.             case "Rest":
  99.                    
  100.                     if ($user['gold'] > 0) {
  101.                         $user['actions'] += 2;
  102.                         $user['gold'] -= 1;
  103.                         $user['hitpoints'] += 5;
  104.                        
  105.                     } else {
  106.                         echo "niet genoeg goud";
  107.                     }
  108.                 break;
  109.             case "Resurrect":
  110.             if ($user['hitpoints'] < 0) {
  111.                 $user['gold'] -= 50;
  112.                 $user['hitpoints'] = 100;
  113.                
  114.             }
  115.              
  116.                 break;
  117.             case "Reset":
  118.                 $user['hitpoints'] = 100;
  119.                 $user['gold'] = 0;
  120.                 $user['actions'] = 25;
  121.              
  122.                 break;
  123.             default:
  124.            
  125.         }
  126.         $sql2 = "UPDATE user_accounts SET actions={$user['actions']}, gold={$user['gold']} , hitpoints={$user['hitpoints']} WHERE id={$user['id']}";
  127.         $conn->query($sql2);
  128.         break;
  129.     default:
  130.         $content = "welcome.php";
  131. }
  132. require "Template.php";
  133.  
  134. function sanatize($value) {
  135.     $value = trim($value);
  136.     $value = stripcslashes($value);
  137.     $value = htmlspecialchars($value);
  138.     return $value;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement