Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. <?php
  2. namespace login;
  3. error_reporting(E_ALL);
  4. date_default_timezone_set('America/New_York');
  5. session_start();
  6.  
  7. class Login
  8. {
  9. //database variables
  10. protected $host;
  11. protected $database;
  12. protected $username;
  13. protected $password;
  14.  
  15. //database connection
  16. protected $db_connect;
  17.  
  18. //game variables
  19. protected $game_id;
  20. protected $game_name;
  21.  
  22. //player variables
  23. protected $user_id;
  24. protected $player_id;
  25. protected $player_name;
  26. protected $token;
  27. protected $ip;
  28.  
  29. public function __construct($game,$user_id)
  30. {
  31. $this->game_name = $game;
  32. $this->user_id = $user_id;
  33.  
  34. switch($game)
  35. {
  36. case 'game_001' :
  37. //set up variables
  38. $this->game_id = 001;
  39. $this->host = 'host';
  40. $this->database = 'game_001';
  41. $this->username = 'dbUser';
  42. $this->password = 'dbPassword';
  43. break;
  44. default :
  45. die('No database exists.');
  46. }
  47.  
  48. //delete this
  49. echo 'Login started...</br>';
  50.  
  51. //login to game
  52. $this->login();
  53. }
  54.  
  55. public function __destruct()
  56. {
  57. //close database connection
  58. $this->db_connect->close();
  59.  
  60. //delete this
  61. echo 'Login has been closed.';
  62. }
  63.  
  64. private function login()
  65. {
  66. //connect to database
  67. $this->db_connect();
  68.  
  69. //get player id and name with user_id
  70. $this->get_player();
  71.  
  72. //create token
  73. $this->generate_token();
  74.  
  75. //get ip
  76. $this->get_ip();
  77.  
  78. //record login
  79. $this->record_login();
  80.  
  81. //log token in database
  82. $this->set_token();
  83.  
  84. //log session variables
  85. $this->set_session();
  86. }
  87.  
  88. private function set_session()
  89. {
  90. //delete this
  91. echo 'Setting session variables...</br>';
  92.  
  93. $_SESSION[$this->game_name.'-token'] = $this->token;
  94. $_SESSION[$this->game_name.'-logged_in'] = true;
  95. $_SESSION[$this->game_name.'-player_id'] = $this->player_id;
  96. $_SESSION[$this->game_name.'-player_name'] = $this->player_name;
  97.  
  98. //delete this
  99. echo 'Session has been set.</br>';
  100. }
  101.  
  102. private function set_token()
  103. {
  104. //delete this
  105. echo 'Setting token in database...</br>';
  106.  
  107. $db = $this->db_connect;
  108. $sql = "UPDATE `players` SET `token` = ? WHERE `players`.`player_id` = ?";
  109.  
  110. //prepare the statement
  111. if(!$stmt = $db->prepare($sql))
  112. {
  113. die("Prepare failed: (".$db->errno.")".$db->error);
  114. }
  115.  
  116. //bind variables
  117. if(!$stmt->bind_param("si",$this->token, $this->player_id))
  118. {
  119. die("Binding parameters failed: (".$db->errno.")".$db->error);
  120. }
  121.  
  122. //execute the prepared statement
  123. if(!$stmt->execute())
  124. {
  125. die("Execution failed: (".$db->errno.")".$db->error);
  126. }
  127.  
  128. //delete this
  129. echo 'Token set successfully.</br>';
  130. }
  131.  
  132. private function get_player()
  133. {
  134. //delete this
  135. echo 'Searching for player information...</br>';
  136.  
  137. $db = $this->db_connect;
  138. $user_id = $this->user_id;
  139.  
  140. $sql = "SELECT player_id, player_name FROM players WHERE user_id = ?";
  141.  
  142. //prepare the statement
  143. if(!$stmt = $db->prepare($sql)){
  144. die("Prepare failed: (".$db->errno.")".$db->error);
  145. }
  146.  
  147. //bind variables
  148. if(!$stmt->bind_param("i",$user_id)){
  149. die("Binding parameters failed: (".$db->errno.")".$db->error);
  150. }
  151.  
  152. //execute the prepared statement
  153. if(!$stmt->execute()){
  154. die("Execution failed: (".$db->errno.")".$db->error);
  155. }
  156.  
  157. //bind result
  158. $stmt->bind_result($this->player_id,$this->player_name);
  159.  
  160. if(!$stmt->fetch()) {
  161. die("Fetch result failed.");
  162. }
  163.  
  164. //delete this
  165. echo 'Player information found: (#' . $this->player_id . ') ' . $this->player_name . '</br>';
  166. }
  167.  
  168. private function record_login()
  169. {
  170. //delete this
  171. echo 'Recording log into database...</br>';
  172.  
  173. $type = 'login';
  174. $note = '';
  175. $time= date('Y-m-d H:i:s');
  176. $player_id = $this->player_id;
  177. $player_name = $this->player_name;
  178. $db = $this->db_connect;
  179.  
  180. $sql = "INSERT INTO login_history (game_num, player_id, user_id, log_type, timestamp, ip) VALUES (?,?,?,?,?,?)";
  181.  
  182. //prepare the statement
  183. if(!$stmt = $db->prepare($sql))
  184. {
  185. die("Prepare failed: (".$db->errno.")".$db->error);
  186. }
  187.  
  188. //bind variables
  189. if(!$stmt->bind_param("iiisss",$this->game_id, $this->player_id, $this->user_id, $type, $time, $this->ip))
  190. {
  191. die("Binding parameters failed: (".$db->errno.")".$db->error);
  192. }
  193.  
  194. //execute the prepared statement
  195. if(!$stmt->execute())
  196. {
  197. die("Execution failed: (".$db->errno.")".$db->error);
  198. }
  199.  
  200. //delete this
  201. echo 'Login recorded successfully.</br>';
  202. }
  203.  
  204. private function generate_token()
  205. {
  206. //delete this
  207. echo 'Generating token...</br>';
  208.  
  209. $this->token = md5(uniqid(rand(), TRUE));
  210.  
  211. //delete this
  212. echo 'Token generated: ' . $this->token . '</br>';
  213. }
  214.  
  215. private function get_ip()
  216. {
  217. //delete this
  218. echo 'Retreiving player ip address...</br>';
  219.  
  220. $client = @$_SERVER['HTTP_CLIENT_IP'];
  221. $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  222. $remote = $_SERVER['REMOTE_ADDR'];
  223.  
  224. if(filter_var($client, FILTER_VALIDATE_IP))
  225. {
  226. $ip = $client;
  227. }
  228. elseif(filter_var($forward, FILTER_VALIDATE_IP))
  229. {
  230. $ip = $forward;
  231. }
  232. else
  233. {
  234. $ip = $remote;
  235. }
  236.  
  237. $this->ip = $ip;
  238.  
  239. //delete this
  240. echo 'IP address found: ' . $this->ip . '</br>';
  241. }
  242.  
  243. private function db_connect()
  244. {
  245. echo 'Establishing database connection...</br>';
  246.  
  247. //the actual db connection ( is used because mysqli is a global class)
  248. $db = new mysqli($this->host,$this->username,$this->password,$this->database);
  249.  
  250. //error check and return is everything is good.
  251. if(mysqli_connect_error())
  252. {
  253. exit();
  254. }
  255. else
  256. {
  257. //delete this
  258. echo 'Database connection established.</br>';
  259.  
  260. $this->db_connect = $db;
  261. }
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement