Guest User

Untitled

a guest
Sep 30th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. Drew_o_Carroll.php
  2.  
  3. login.php
  4. registration.php
  5. home.php
  6. include
  7. -- functions.php
  8. -- config.php
  9.  
  10. Database
  11. Sample database users table columns uid, username, passcode, name and email.
  12. CREATE TABLE users
  13. (
  14. uid INT PRIMARY KEY AUTO_INCREMENT,
  15. username VARCHAR(30) UNIQUE,
  16. password VARCHAR(50),
  17. name VARCHAR(100),
  18. email VARCHAR(70) UNIQUE
  19. );
  20.  
  21. functions.php
  22. Contains PHP code class User{} contains functions/methods.
  23. <?php
  24. include_once 'config.php';
  25. class User
  26. {
  27. //Database connect
  28. public function __construct()
  29. {
  30. $db = new DB_Class();
  31. }
  32. //Registration process
  33. public function register_user($name, $username, $password, $email)
  34. {
  35. $password = md5($password);
  36. $sql = mysql_query("SELECT uid from users WHERE username = '$username' or email = '$email'");
  37. $no_rows = mysql_num_rows($sql);
  38. if ($no_rows == 0)
  39. {
  40. $result = mysql_query("INSERT INTO users(username, password, name, email) values ('$username', '$password','$name','$email')") or die(mysql_error());
  41. return $result;
  42. }
  43. else
  44. {
  45. return FALSE;
  46. }
  47. }
  48. // Login process
  49. public function check_login($emailusername, $password)
  50. {
  51. $password = md5($password);
  52. $result = mysql_query("SELECT uid from users WHERE email = '$emailusername' or username='$emailusername' and password = '$password'");
  53. $user_data = mysql_fetch_array($result);
  54. $no_rows = mysql_num_rows($result);
  55. if ($no_rows == 1)
  56. {
  57. $_SESSION['login'] = true;
  58. $_SESSION['uid'] = $user_data['uid'];
  59. return TRUE;
  60. }
  61. else
  62. {
  63. return FALSE;
  64. }
  65. }
  66. // Getting name
  67. public function get_fullname($uid)
  68. {
  69. $result = mysql_query("SELECT name FROM users WHERE uid = $uid");
  70. $user_data = mysql_fetch_array($result);
  71. echo $user_data['name'];
  72. }
  73. // Getting session
  74. public function get_session()
  75. {
  76. return $_SESSION['login'];
  77. }
  78. // Logout
  79. public function user_logout()
  80. {
  81. $_SESSION['login'] = FALSE;
  82. session_destroy();
  83. }
  84.  
  85. }
  86. ?>
  87.  
  88. registration.php
  89. Here $user = new User(); is the class User{} object using this calling method $user->register_user{} and inserting values.
  90. <?php
  91. include_once 'include/functions.php';
  92. $user = new User();
  93. // Checking for user logged in or not
  94. if ($user->get_session())
  95. {
  96. header("location:home.php");
  97. }
  98.  
  99. if ($_SERVER["REQUEST_METHOD"] == "POST")
  100. {
  101. $register = $user->register_user($_POST['name'], $_POST['username'], $_POST['password'], $_POST['email']);
  102. if ($register)
  103. {
  104. // Registration Success
  105. echo 'Registration successful <a href="login.php">Click here</a> to login';
  106. } else
  107. {
  108. // Registration Failed
  109. echo 'Registration failed. Email or Username already exits please try again';
  110. }
  111. }
  112. ?>
  113. //HTML Code
  114. <form method="POST" action="register.php" name='reg' >
  115. Full Name
  116. <input type="text" name="name"/>
  117. Username
  118. <input type="text" name="username"/>
  119. Password
  120. <input type="password" name="password"/>
  121. Email
  122. <input type="text" name="email"/>
  123. <input type="submit" value="Register"/>
  124. </form>
  125.  
  126. login.php
  127. Calling method $user->check_login{} for login verification.
  128. <?php
  129. session_start();
  130. include_once 'include/functions.php';
  131. $user = new User();
  132. if ($user->get_session())
  133. {
  134. header("location:home.php");
  135. }
  136.  
  137. if ($_SERVER["REQUEST_METHOD"] == "POST")
  138. {
  139. $login = $user->check_login($_POST['emailusername'], $_POST['password']);
  140. if ($login)
  141. {
  142. // Login Success
  143. header("location:login.php");
  144. }
  145. else
  146. {
  147. // Login Failed
  148. $msg= 'Username / password wrong';
  149. }
  150. }
  151. ?>
  152. //HTML Code
  153. <form method="POST" action="" name="login">
  154. Email or Username
  155. <input type="text" name="emailusername"/>
  156. Password
  157. <input type="password" name="password"/>
  158. <input type="submit" value="Login"/>
  159. </form>
  160.  
  161. home.php
  162. <?php
  163. session_start();
  164. include_once 'include/functions.php';
  165. $user = new User();
  166. $uid = $_SESSION['uid'];
  167. if (!$user->get_session())
  168. {
  169. header("location:login.php");
  170. }
  171. if ($_GET['q'] == 'logout')
  172. {
  173. $user->user_logout();
  174. header("location:login.php");
  175. }
  176. ?>
  177. //HTML Code
  178. <a href="?q=logout">LOGOUT</a>
  179. <h1> Hello <?php $user->get_fullname($uid); ?></h1>
  180.  
  181. config.php
  182. Database configuration class DB_class() function __construct() is the method name for the constructor.
  183. <?php
  184. define('DB_SERVER', 'localhost');
  185. define('DB_USERNAME', 'username');
  186. define('DB_PASSWORD', 'password');
  187. define('DB_DATABASE', 'database');
  188. class DB_Class
  189. {
  190. function __construct()
  191. {
  192. $connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or
  193. die('Oops connection error -> ' . mysql_error());
  194. mysql_select_db(DB_DATABASE, $connection)
  195. or die('Database error -> ' . mysql_error());
  196. }
  197. }
  198. ?>
Add Comment
Please, Sign In to add comment