Advertisement
Guest User

Untitled

a guest
Dec 26th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  1. <?php
  2. $connection = mysqli_connect('localhost','root','');
  3. if(!$connection){
  4. echo "faild to connect to db" . die(mysqli_error($connection));
  5. }
  6. $dbselect = mysqli_select_db($connection,'new');
  7. if(!$dbselect){
  8. echo "faild to select db" . die(mysqli_error($connection));
  9. }
  10. ?>
  11.  
  12. <?php
  13. require_once('connect.php');
  14. if(isset($_POST) & !empty($_POST)){
  15. $username = $_POST['username'];
  16. $email = $_POST['email'];
  17. $password = $_POST['password'];
  18. $sql = "INSERT INTO `user` (username,email,password,uid) VALUES ('$username','$email','$password',1)";
  19. $result = mysqli_query($connection, $sql);
  20. if ($result){
  21. $smsg = "user registered successfully";
  22. }else{
  23. $fmsg = "user registration failed";
  24. }
  25. }
  26. ?>
  27. <!doctype html>
  28. <html lang="en">
  29. <head>
  30. <title>register user with php</title>
  31. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  32. <link rel="stylesheet" href="styles.css" >
  33. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  34. </head>
  35. <body>
  36. <div class="container">
  37. <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
  38. <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
  39. <form class="form-signin" method="POST">
  40. <h2 class="form-signin-heading">Please Register</h2>
  41. <div class="input-group">
  42. <span class="input-group-addon" id="basic-addon1">username:</span>
  43. <input type="text" name="username" class="form-control" placeholder="Username" required>
  44. </div>
  45. <label for="inputEmail" class="sr-only">Email address</label>
  46. <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
  47. <label for="inputPassword" class="sr-only">Password</label>
  48. <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
  49. <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
  50. <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
  51. </form>
  52. </div>
  53. </body>
  54. </html>
  55.  
  56. <html>
  57. <head>
  58. <title>User Login Using PHP & MySQL</title>
  59. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  60. <link rel="stylesheet" href="style.css" >
  61. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  62. </head>
  63. <body>
  64. <?php //Start the Session
  65. session_start();
  66. require('connect.php');
  67. //3. If the form is submitted or not.
  68. //3.1 If the form is submitted
  69. if (isset($_POST['username']) and isset($_POST['password'])){
  70. //3.1.1 Assigning posted values to variables.
  71. $username = $_POST['username'];
  72. $password = $_POST['password'];
  73. //3.1.2 Checking the values are existing in the database or not
  74. $query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
  75. $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
  76. $count = mysqli_num_rows($result);
  77. //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
  78. if ($count == 1){
  79. $_SESSION['username'] = $username;
  80. }else{
  81. //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
  82. $fmsg = "Invalid Login Credentials.";
  83. }
  84. }
  85. //3.1.4 if the user is logged in Greets the user with message
  86. if (isset($_SESSION['username'])){
  87. $username = $_SESSION['username'];?>
  88. <?php
  89. require_once('connect.php');
  90. if(isset($_POST) & !empty($_POST)){
  91. $username1 = $_POST['username1'];
  92. $email1 = $_POST['email1'];
  93. $password1 = $_POST['password1'];
  94. $sql3 = "SELECT id, username, uid FROM user where username='$username'";
  95. $result3 = $connection->query($sql3);
  96. if ($result3->num_rows > 0) {
  97. // output data of each row
  98. while($row = $result3->fetch_assoc()) {
  99. $uidreg = $row['id'];
  100. }
  101. } else {
  102. echo "0 results3";
  103. }
  104. $sql2 = "INSERT INTO `user` (username,email,password,uid) VALUES ('$username1','$email1','$password1','$uidreg')";
  105. $result2 = mysqli_query($connection, $sql2);
  106. if ($result2){
  107. echo "user registered successfully";
  108. if (!mkdir("user $username1", 0777, true)) {
  109. die('Failed to create folders...');}
  110. }else{
  111. echo "register failed";
  112. }
  113. }
  114. ?>
  115. <div id='box'>
  116. <form class="form-signin" method="POST">
  117. <?php echo "<h2 class='form-signin-heading'> Hello $username Please Register</h2>"?>
  118. <div class="input-group">
  119. <span class="input-group-addon" id="basic-addon1">username:</span>
  120. <input type="text" name="username1" class="form-control" placeholder="Username" required>
  121. </div>
  122. <label for="inputEmail" class="sr-only">Email address</label>
  123. <input type="email" name="email1" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
  124. <label for="inputPassword" class="sr-only">Password</label>
  125. <input type="password" name="password1" id="inputPassword" class="form-control" placeholder="Password" required>
  126. <button class="btn btn-lg btn-primary btn-block" type="submit" name="regSubmit">Register</button>
  127. <a class="btn btn-lg btn-primary btn-block" href="user-login.php">Login</a>
  128.  
  129. </form>
  130. <br><?php echo "<a href='logout.php'>Logout</a>"; ?>
  131. </div>
  132. <?php }else{
  133. //3.2 When the user visits the page first time, simple login form will be displayed.
  134. ?>
  135. <div class="container">
  136. <form class="form-signin" method="POST">
  137. <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
  138. <h2 class="form-signin-heading">Please Login</h2>
  139. <div class="input-group">
  140. <span class="input-group-addon" id="basic-addon1">username:</span>
  141. <input type="text" name="username" class="form-control" placeholder="Username" required>
  142. </div>
  143. <div class="input-group">
  144. <label for="inputPassword" class="sr-only">Password</label>
  145. <span class="input-group-addon" id="basic-addon1">Password:</span>
  146. <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
  147. </div>
  148. <button class="btn btn-lg btn-primary btn-block" type="submit" name="loginSubmit">Login</button>
  149. </form>
  150. </div>
  151.  
  152. </body>
  153.  
  154. </html>
  155. <?php } ?>
  156.  
  157. <?php
  158. session_start();
  159. session_destroy();
  160. header('Location: login.php');
  161. ?>
  162.  
  163. <html>
  164. <head>
  165. <title>User Login Using PHP & MySQL</title>
  166. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  167. <link rel="stylesheet" href="style.css" >
  168. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  169. </head>
  170. <body>
  171. <?php //Start the Session
  172. session_start();
  173. require('connect.php');
  174. //3. If the form is submitted or not.
  175. //3.1 If the form is submitted
  176. if (isset($_POST['username2']) and isset($_POST['password2'])){
  177. //3.1.1 Assigning posted values to variables.
  178. $username2 = $_POST['username2'];
  179. $password2 = $_POST['password2'];
  180. //3.1.2 Checking the values are existing in the database or not
  181. $query = "SELECT * FROM `user` WHERE username='$username2' and password='$password2'";
  182. $result4 = mysqli_query($connection, $query) or die(mysqli_error($connection));
  183. $count4 = mysqli_num_rows($result4);
  184. //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
  185. if ($count4 == 1){
  186. $_SESSION['username2'] = $username2;
  187. }else{
  188. //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
  189. $fmsg = "Invalid Login Credentials.";
  190. }
  191. }
  192. //3.1.4 if the user is logged in Greets the user with message
  193. if (isset($_SESSION['username2'])){
  194. $username2 = $_SESSION['username2'];?>
  195. <?php
  196. $target_dir = "user $username2/";
  197. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  198. $uploadOk = 1;
  199. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  200. if(isset($_POST["submit"])) {
  201. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  202. if($check !== false) {
  203. echo "File is an image - " . $check["mime"] . ".";
  204. $uploadOk = 1;} else {
  205. echo "File is not an image.";
  206. $uploadOk = 0;}}
  207. if (file_exists($target_file)) {
  208. echo "Sorry, file already exists.";
  209. $uploadOk = 0;}
  210. if ($_FILES["fileToUpload"]["size"] > 500000) {
  211. echo "Sorry, your file is too large.";
  212. $uploadOk = 0;}
  213. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  214. && $imageFileType != "gif" ) {
  215. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  216. $uploadOk = 0;}
  217. if ($uploadOk == 0) {
  218. echo "Sorry, your file was not uploaded.";} else {
  219. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  220. echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";} else {
  221. echo "Sorry, there was an error uploading your file.";}}
  222. ?>
  223. <div id='box'>
  224. <?php echo "<h2> hi, $username2</h2>"; ?><br>
  225. <form method="post" enctype="multipart/form-data">
  226. Select image to upload:
  227. <input type="file" name="fileToUpload" id="fileToUpload">
  228. <input type="submit" value="Upload Image" name="submit">
  229. </form>
  230. <br><a href='user-logout.php'>Logout</a>"
  231. </div>
  232. <?php }else{
  233. //3.2 When the user visits the page first time, simple login form will be displayed.
  234. ?>
  235. <div class="container">
  236. <form class="form-signin" method="POST">
  237. <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
  238. <h2 class="form-signin-heading">Please Login</h2>
  239. <div class="input-group">
  240. <span class="input-group-addon" id="basic-addon1">username:</span>
  241. <input type="text" name="username2" class="form-control" placeholder="Username" required>
  242. </div>
  243. <div class="input-group">
  244. <label for="inputPassword" class="sr-only">Password</label>
  245. <span class="input-group-addon" id="basic-addon1">Password:</span>
  246. <input type="password" name="password2" id="inputPassword" class="form-control" placeholder="Password" required>
  247. </div>
  248. <button class="btn btn-lg btn-primary btn-block" type="submit" name="loginSubmit">Login</button>
  249. </form>
  250. </div>
  251. </body>
  252. </html>
  253. <?php } ?>
  254.  
  255. <?php
  256. session_start();
  257. session_destroy();
  258. header('Location: user-login.php');
  259. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement