Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. <?php
  2. session_start();
  3. $_SESSION['message'] = '';
  4. $mysqli = new mysqli('localhost', 'root', '', 'accounts');
  5.  
  6. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  7. //checking that two passwords are equal to each other
  8. if ($_POST['password'] == $_POST['confirmpassword']) {
  9.  
  10. $username = $mysqli->real_escape_string($_POST['username']);
  11. $email = $mysqli->real_escape_string($_POST['email']);
  12. $password = md5($_POST['password']); //md5 hash password security
  13. $avatar_path = $mysqli->real_escape_string('image/'.$_FILES['avatar']['name']);
  14.  
  15. //make sure file type is act an image
  16. if (preg_match("!image!", $_FILES['avatar']['type'])){
  17.  
  18. //copy image to image/ folder
  19. if (copy($_FILES['avatar']['tmp_name'], $avatar_patch)){
  20.  
  21. $_SESSION['username'] = $username;
  22. $_SESSION['avatar'] = $avatar_patch;
  23.  
  24. $sql = "INSERT INTO users (username, email, password, avatar) "
  25. . "VALUES ('$username', '$email', '$password', '$avatar_patch')";
  26.  
  27. //if the query is successful, redirect to welcome.php page, done!
  28. if ($mysqli->query($sql) === true) {
  29. $_SESSION['message'] = 'Registration successful! Added $username to the database!';
  30. header("location: welcome.php");
  31. }
  32. else {
  33. $_SESSION['message'] = "User could not be added to the database!";
  34. }
  35. }
  36. else {
  37. $_SESSION['message'] = "File upload failed!";
  38. }
  39. }
  40. else {
  41. $_SESSION['message'] = "Please only upload GIF, JPG, PNG images!";
  42. }
  43. }
  44. else {
  45. $_SESSION['message'] = "The two passwords did not match!";
  46. }
  47. }
  48.  
  49. ?>
  50. <link href="//db.onlinewebfonts.com/c/a4e256ed67403c6ad5d43937ed48a77b? family=Core+Sans+N+W01+35+Light" rel="stylesheet" type="text/css"/>
  51. <link rel="stylesheet" href="form.css" type="text/css">
  52. <div class="body-content">
  53. <div class="module">
  54. <h1>Create an account</h1>
  55. <form class="form" action="form.php" method="post" enctype="multipart/form-data" autocomplete="off">
  56. <div class="alert alert-error"><?= $_SESSION['message'] ?></div>
  57. <input type="text" placeholder="User Name" name="username" required />
  58. <input type="email" placeholder="Email" name="email" required />
  59. <input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
  60. <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
  61. <div class="avatar"><label>Select your avatar: </label><input type="file" name="avatar" accept="image/*" required /></div>
  62. <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
  63. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement