Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  2.    
  3.     //two passwords are equal to each other
  4.     if ($_POST['password'] == $_POST['confirmpassword']) {
  5.        
  6.         //define other variables with submitted values from $_POST
  7.         $username = $mysqli->real_escape_string($_POST['username']);
  8.         $email = $mysqli->real_escape_string($_POST['email']);
  9.  
  10.         //md5 hash password for security
  11.         $password = md5($_POST['password']);
  12.  
  13.         //path were our avatar image will be stored
  14.         $avatar_path = $mysqli->real_escape_string('images/'.$_FILES['avatar']['name']);
  15.        
  16.         //make sure the file type is image
  17.         if (preg_match("!image!",$_FILES['avatar']['type'])) {
  18.            
  19.             //copy image to images/ folder
  20.             if (copy($_FILES['avatar']['tmp_name'], $avatar_path)){
  21.                
  22.                 //set session variables to display on welcome page
  23.                 $_SESSION['username'] = $username;
  24.                 $_SESSION['avatar'] = $avatar_path;
  25.  
  26.                 //insert user data into database
  27.                 $sql =
  28.                 "INSERT INTO users (username, email, password, avatar) "
  29.                 . "VALUES ('$username', '$email', '$password', '$avatar_path')";
  30.                
  31.                 //check if mysql query is successful
  32.                 if ($mysqli->query($sql) === true){
  33.                     $_SESSION['message'] = "Registration successful!"
  34.                     . "Added $username to the database!";
  35.                     //redirect the user to welcome.php
  36.                     header("location: welcome.php");
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement