Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //retrieve our data from POST
- $username = $_POST['username'];
- $pass1 = $_POST['pass1'];
- $pass2 = $_POST['pass2'];
- if($pass1 != $pass2)
- header('Location: register_form.php');
- if(strlen($username) > 30)
- header('Location: register_form.php');
- $hash = hash('sha256', $pass1);
- //creates a 3 character sequence
- function createSalt()
- {
- $string = md5(uniqid(rand(), true));
- return substr($string, 0, 3);
- }
- $salt = createSalt();
- $hash = hash('sha256', $salt . $hash);
- $dbhost = 'localhost';
- $dbname = 'db_10053670';
- $dbuser = 'user_10053670';
- $dbpass = 'Ch33s3cob'; //not really
- $conn = mysql_connect($dbhost, $dbuser, $dbpass);
- mysql_select_db($dbname, $conn);
- //sanitize username
- $username = mysql_real_escape_string($username);
- $query = "INSERT INTO users ( username, password, salt )
- VALUES ( '$username' , '$hash' , '$salt' );";
- mysql_query($query);
- mysql_close();
- header('Location: register.html');
- $username = $_POST['username'];
- $password = $_POST['password'];
- //connect to the database here
- $username = mysql_real_escape_string($username);
- $query = "SELECT password, salt
- FROM users
- WHERE username = '$username';";
- $result = mysql_query($query);
- if(mysql_num_rows($result) < 1) //no such user exists
- {
- header('Location: login_form.php');
- }
- $userData = mysql_fetch_array($result, MYSQL_ASSOC);
- $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
- if($hash != $userData['password']) //incorrect password
- {
- header('Location: register.html');
- }
- //login successful
- ?>
Advertisement
Add Comment
Please, Sign In to add comment