Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. common.php:
  2. <?php
  3.  
  4. session_start();
  5.  
  6. function registerUser($user,$pass1,$pass2){
  7. $errorText = '';
  8.  
  9. // Check passwords
  10. if ($pass1 != $pass2) $errorText = "Passwords are not identical!";
  11. elseif (strlen($pass1) < 6) $errorText = "Password is to short!";
  12.  
  13. // Check user existance
  14. $pfile = fopen("userpwd.txt","a+");
  15. rewind($pfile);
  16.  
  17. while (!feof($pfile)) {
  18. $line = fgets($pfile);
  19. $tmp = explode(':', $line);
  20. if ($tmp[0] == $user) {
  21. $errorText = "The selected user name is taken!";
  22. break;
  23. }
  24. }
  25.  
  26. // If everything is OK -> store user data
  27. if ($errorText == ''){
  28. // Secure password string
  29. $userpass = md5($pass1);
  30.  
  31. fwrite($pfile, "\r\n$user:$userpass");
  32. }
  33.  
  34. fclose($pfile);
  35.  
  36.  
  37. return $errorText;
  38. }
  39.  
  40. function loginUser($user,$pass){
  41. $errorText = '';
  42. $validUser = false;
  43.  
  44. // Check user existance
  45. $pfile = fopen("userpwd.txt","r");
  46. rewind($pfile);
  47.  
  48. while (!feof($pfile)) {
  49. $line = fgets($pfile);
  50. $tmp = explode(':', $line);
  51. if ($tmp[0] == $user) {
  52. // User exists, check password
  53. if (trim($tmp[1]) == trim(md5($pass))){
  54. $validUser= true;
  55. $_SESSION['userName'] = $user;
  56. }
  57. break;
  58. }
  59. }
  60. fclose($pfile);
  61.  
  62. if ($validUser != true) $errorText = "Invalid username or password!";
  63.  
  64. if ($validUser == true) $_SESSION['validUser'] = true;
  65. else $_SESSION['validUser'] = false;
  66.  
  67. return $errorText;
  68. }
  69.  
  70. function logoutUser(){
  71. unset($_SESSION['validUser']);
  72. unset($_SESSION['userName']);
  73. }
  74.  
  75. function checkUser(){
  76. if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
  77. header('Location: login.php');
  78. }
  79. }
  80.  
  81. ?>
  82.  
  83. index.php:
  84. <?php
  85. require_once('common.php');
  86. checkUser();
  87. ?>
  88.  
  89. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
  90. <html>
  91. <head>
  92. <title>Micro Login System</title>
  93. <link href="style/style.css" rel="stylesheet" type="text/css" />
  94. </head>
  95. <body>
  96. <div id="main">
  97. <div class="caption">Login System Demo Page</div>
  98. <div id="icon">&nbsp;</div>
  99. <div id="result">
  100. Hello <?php echo $_SESSION['userName']; ?> ! <br/>
  101. <p>This site demonstartes how to use Micro Login System.</p>
  102. <p><a href="logout.php"> To log out click here!</a></p>
  103. </div>
  104. <div id="source">Micro Login System v 1.0</div>
  105. </div>
  106. </body>
  107.  
  108. login.php:
  109. <?php
  110. require_once('common.php');
  111.  
  112. $error = '0';
  113.  
  114. if (isset($_POST['submitBtn'])){
  115. // Get user input
  116. $username = isset($_POST['username']) ? $_POST['username'] : '';
  117. $password = isset($_POST['password']) ? $_POST['password'] : '';
  118.  
  119. // Try to login the user
  120. $error = loginUser($username,$password);
  121. }
  122.  
  123. ?>
  124.  
  125. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
  126. <html>
  127. <head>
  128. <title>Micro Login System</title>
  129. <link href="style/style.css" rel="stylesheet" type="text/css" />
  130. </head>
  131. <body>
  132. <div id="main">
  133. <?php if ($error != '') {?>
  134. <div class="caption">Site login</div>
  135. <div id="icon">&nbsp;</div>
  136. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform">
  137. <table width="100%">
  138. <tr><td>Username:</td><td> <input class="text" name="username" type="text" /></td></tr>
  139. <tr><td>Password:</td><td> <input class="text" name="password" type="password" /></td></tr>
  140. <tr><td colspan="2" align="center"><input class="text" type="submit" name="submitBtn" value="Login" /></td></tr>
  141. </table>
  142. </form>
  143.  
  144. &nbsp;<a href="register.php">Register</a>
  145.  
  146. <?php
  147. }
  148. if (isset($_POST['submitBtn'])){
  149.  
  150. ?>
  151. <div class="caption">Login result:</div>
  152. <div id="icon2">&nbsp;</div>
  153. <div id="result">
  154. <table width="100%"><tr><td><br/>
  155. <?php
  156. if ($error == '') {
  157. echo "Welcome $username! <br/>You are logged in!<br/><br/>";
  158. echo '<a href="index.php">Now you can visit the index page!</a>';
  159. }
  160. else echo $error;
  161.  
  162. ?>
  163. <br/><br/><br/></td></tr></table>
  164. </div>
  165. <?php
  166. }
  167. ?>
  168. <div id="source">Micro Login System v 1.0</div>
  169. </div>
  170. </body>
  171.  
  172. logout.php:
  173. <?php
  174. require_once('common.php');
  175. logoutUser();
  176. header('Location: index.php');
  177. ?>
  178.  
  179. register.php:
  180. <?php
  181. require_once('common.php');
  182.  
  183. if (isset($_POST['submitBtn'])){
  184. // Get user input
  185. $username = isset($_POST['username']) ? $_POST['username'] : '';
  186. $password1 = isset($_POST['password1']) ? $_POST['password1'] : '';
  187. $password2 = isset($_POST['password2']) ? $_POST['password2'] : '';
  188.  
  189. // Try to register the user
  190. $error = registerUser($username,$password1,$password2);
  191. }
  192. ?>
  193.  
  194. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
  195. <html>
  196. <head>
  197. <title>Micro Login System</title>
  198. <link href="style/style.css" rel="stylesheet" type="text/css" />
  199. </head>
  200. <body>
  201. <div id="main">
  202. <?php if ((!isset($_POST['submitBtn'])) || ($error != '')) {?>
  203. <div class="caption">Register user</div>
  204. <div id="icon">&nbsp;</div>
  205. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="registerform">
  206. <table width="100%">
  207. <tr><td>Username:</td><td> <input class="text" name="username" type="text" /></td></tr>
  208. <tr><td>Password:</td><td> <input class="text" name="password1" type="password" /></td></tr>
  209. <tr><td>Confirm password:</td><td> <input class="text" name="password2" type="password" /></td></tr>
  210. <tr><td colspan="2" align="center"><input class="text" type="submit" name="submitBtn" value="Register" /></td></tr>
  211. </table>
  212. </form>
  213.  
  214. <?php
  215. }
  216. if (isset($_POST['submitBtn'])){
  217.  
  218. ?>
  219. <div class="caption">Registration result:</div>
  220. <div id="icon2">&nbsp;</div>
  221. <div id="result">
  222. <table width="100%"><tr><td><br/>
  223. <?php
  224. if ($error == '') {
  225. echo " User: $username was registered successfully!<br/><br/>";
  226. echo ' <a href="login.php">You can login here</a>';
  227.  
  228. }
  229. else echo $error;
  230.  
  231. ?>
  232. <br/><br/><br/></td></tr></table>
  233. </div>
  234. <?php
  235. }
  236. ?>
  237. <div id="source">Micro Login System v 1.0</div>
  238. </div>
  239. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement