daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest May 27th, 2018 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. if ($_SERVER["REQUEST_METHOD"] == "POST"){
  3.   include 'dbcon.php';
  4.   $username = mysqli_real_escape_string($connection, $_POST['name']);
  5.   $useremail = mysqli_real_escape_string($connection,$_POST['email']);
  6.   $password = mysqli_real_escape_string($connection, $_POST['psw']);
  7.  
  8.   //Error-hanterare
  9.   //Kolla efter tomma fält
  10.   if (empty($username) || empty($useremail) || empty($password)){
  11.     header("Location: ../index.php?registration=empty");
  12.     exit();
  13.   }
  14.   else{
  15.     //Kollar om input-chars är valid
  16.     if (!preg_match("/^[a-zA-Z]*$/", $username)){
  17.       header("Location: ../index.php?registration=invalid");
  18.       exit();
  19.     }
  20.     else{
  21.       //Kollar om email är valid
  22.       if(!preg_match("/.+@.+\..+/", $_POST['email'])){
  23.         header("Location: ../index.php?registration=invalidemail");
  24.         exit();
  25.       }
  26.       else{
  27.         $email = $_POST['email'];
  28.         $sql = "SELECT * FROM users WHERE email='$email'";
  29.         $result = mysqli_query($connection, $sql);
  30.         $resultCheck = mysqli_num_rows($result);
  31.         if ($resultCheck > 0) {
  32.           header("Location: ../index.php?registration=usertaken");
  33.           exit();
  34.         }
  35.         else{
  36.           //Hashar pw
  37.           //genRandomString skapar en slumpad sträng. Längden på strängen blir vad du sätter $length till. Varje gång loopen körs slumpas en av charsen och läggs till i randomString.
  38.           function genRandomString($length = 10)
  39.            {
  40.             $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@£$%&+';
  41.             $charsLength = strlen($chars);
  42.             $randomString = '';
  43.             for ($i = 0; $i < $length; $i++)
  44.             {
  45.                 $randomString .= $chars[rand(0, $charsLength - 1)];
  46.             }
  47.             return $randomString;
  48.           }
  49.           $salt = sha1(genRandomString($length = 10), FALSE);
  50.           $cryptedpw = md5($password.$salt);
  51.           //Insertar användaren i databasen
  52.           $query3 = "INSERT INTO users (email, password, username, salt) VALUES('$useremail', '$cryptedpw', '$username', '$salt')";
  53.           mysqli_query($connection, $query3);
  54.           header("Location: ../accountCreated.php?registration=success");
  55.           exit();
  56.         }
  57.       }
  58.     }
  59.   }
  60. }
  61. else{
  62.   exit("error");
  63. }
  64.   echo "error";
  65. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top