Guest User

Untitled

a guest
Feb 18th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <?php
  2.  
  3. # Establishing the connection to the Database
  4. function connect()
  5. {
  6. $servername = "localhost";
  7. $username = "root";
  8. $password = "root";
  9. $dbname = "GeekChat";
  10.  
  11. $connection = new mysqli($servername, $username, $password, $dbname);
  12.  
  13. // Check connection
  14. if ($connection->connect_error)
  15. {
  16. return null;
  17. }
  18. else
  19. {
  20. return $connection;
  21. }
  22. }
  23.  
  24. # Callback error messages
  25. function errors($type)
  26. {
  27. $header = "HTTP/1.1 ";
  28.  
  29. switch($type)
  30. {
  31. case 306: $header .= "306 Wrong Credentials";
  32. break;
  33. case 400: $header .= "400 User Not Found";
  34. break;
  35. case 404: $header .= "404 Request Not Found";
  36. break;
  37. case 409: $header .= "409 Your action was not completed correctly, please try again later";
  38. break;
  39. case 412: $header .= "412 Username already in use";
  40. break;
  41. case 417: $header .= "417 No content set in the cookie/session";
  42. break;
  43. case 500: $header .= "500 Bad connection to Database";
  44. break;
  45. default: $header .= "404 Request Not Found";
  46. }
  47.  
  48. header($header);
  49. return array('status' => 'ERROR', 'code' => $type);
  50. }
  51.  
  52. # Query to retrieve a user data
  53. function validateUserCredentials($userName)
  54. {
  55. # Open and validate the Database connection
  56. $conn = connect();
  57.  
  58. if ($conn != null)
  59. {
  60. $sql = "SELECT * FROM User WHERE userName = '$userName'";
  61. $result = $conn->query($sql);
  62.  
  63. # The current user exists
  64. if ($result->num_rows > 0)
  65. {
  66. while($row = $result->fetch_assoc())
  67. {
  68. $conn->close();
  69. return array("status" => "COMPLETE", "fName" => $row['fName'], "lName" => $row['lName'], "password" => $row['password']);
  70. }
  71. }
  72. else
  73. {
  74. # User Not Found
  75. $conn->close();
  76. return errors(400);
  77. }
  78. }
  79. else
  80. {
  81. # Bad connection to Database
  82. $conn->close();
  83. return errors(500);
  84. }
  85. }
  86.  
  87. # Query to find out if the user already exist in the Database
  88. function verifyUserExistence($userName)
  89. {
  90. # Open and validate the Database connection
  91. $conn = connect();
  92.  
  93. if ($conn != null)
  94. {
  95. $sql = "SELECT * FROM User WHERE userName = '$userName'";
  96. $result = $conn->query($sql);
  97.  
  98. if ($result->num_rows > 0)
  99. {
  100. # The current user already exists
  101. $conn->close();
  102. return errors(412);
  103. }
  104. else
  105. {
  106. $conn->close();
  107. return array("status" => "COMPLETE");
  108. }
  109. }
  110. else
  111. {
  112. # Bad connection to Database
  113. $conn->close();
  114. return errors(500);
  115. }
  116. }
  117.  
  118. # Function to insert a new user to the Database
  119. function registerNewUser($userFirstName, $userLastName, $userName, $email, $userPassword, $userGender)
  120. {
  121. # Open and validate the Database connection
  122. $conn = connect();
  123.  
  124. if ($conn != null)
  125. {
  126. $sql = "INSERT INTO User(fName, lName, gender, email, userName, password) VALUES ('$userFirstName', '$userLastName', '$userGender', '$email', '$userName', '$userPassword')";
  127. if (mysqli_query($conn, $sql))
  128. {
  129. $conn->close();
  130. return array("status" => "COMPLETE");
  131. }
  132. else
  133. {
  134. $conn->close();
  135. return errors(409);
  136. }
  137. }
  138. else
  139. {
  140. # Connection to Database was not successful
  141. $conn->close();
  142. return errors(500);
  143. }
  144. }
  145.  
  146. ?>
Add Comment
Please, Sign In to add comment