Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. <?php
  2. // DEFINES
  3. define('DB_SERVER', 'localhost');
  4. define('DB_USERNAME', '');
  5. define('DB_PASSWORD', '');
  6. define('DB_DATABASE', '');
  7.  
  8. // Core of Platform
  9. class Core{
  10.  
  11. public $db;
  12.  
  13. /*** for construction process ***/
  14. public function __construct(){
  15. $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  16.  
  17. if(mysqli_connect_errno()) {
  18. echo "Error: Could not connect to database.";
  19. exit;
  20. }
  21. }
  22.  
  23. /*** for registration process ***/
  24. public function register($username,$password,$email){
  25.  
  26. $password = md5($password);
  27. $sql="SELECT * FROM users WHERE username='$username' OR email='$email'";
  28.  
  29. //checking if the username or email is available in db
  30. $check = $this->db->query($sql) ;
  31. $count_row = $check->num_rows;
  32.  
  33. //if the username is not in db then insert to the table
  34. if ($count_row == 0){
  35. $sql1="INSERT INTO users SET username='$username', password='$password', email='$email'";
  36. $result = mysqli_query($this->db,$sql1) or die(mysqli_connect_errno()."Data cannot inserted");
  37. return $result;
  38. }
  39. else {
  40. return false;
  41. }
  42. }
  43.  
  44. /*** for login process ***/
  45. public function login($username, $password){
  46.  
  47. $password = md5($password);
  48. $sql2="SELECT user_id from users WHERE username='$username' and password='$password'";
  49.  
  50. //checking if the username is available in the table
  51. $result = mysqli_query($this->db,$sql2);
  52. $user_data = mysqli_fetch_array($result);
  53. $count_row = $result->num_rows;
  54.  
  55. if ($count_row == 1) {
  56. // this login var will use for the session thing
  57. $_SESSION['login'] = true;
  58. $_SESSION['uid'] = $user_data['user_id'];
  59. return true;
  60. }
  61. else{
  62. return false;
  63. }
  64. }
  65.  
  66. /*** return user's data ***/
  67. public function get_user_data($user_id, $query){
  68. $result = mysqli_query($this->db,"SELECT * FROM users WHERE user_id = $user_id");
  69. $user_data = mysqli_fetch_array($result);
  70. echo $user_data[$query];
  71. }
  72.  
  73. /*** starting the session ***/
  74. public function get_session(){
  75. return $_SESSION['login'];
  76. echo $_SESSION['login'];
  77. }
  78.  
  79. /*** loging out ***/
  80. public function user_logout() {
  81. $_SESSION['login'] = FALSE;
  82. session_destroy();
  83. }
  84. }
  85.  
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement