Skip to content

Instantly share code, notes, and snippets.

@dennisdegryse
Last active August 29, 2015 14:13
Show Gist options
  • Save dennisdegryse/01ff51f4d4ca8e871616 to your computer and use it in GitHub Desktop.
Save dennisdegryse/01ff51f4d4ca8e871616 to your computer and use it in GitHub Desktop.
<?php
$dbhost = 'localhost';
$dbname = 'mydb';
$dbuser = 'root';
$dbpass = '';
try {
$db = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection error: ".$e->getMessage();
}
$email="";
$mobile="";
$username="";
$msg="";
if(isset($_POST['submit']))
{
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$username=$_POST['username'];
$password=$_POST['password'];
$password1=$_POST['password1'];
$query = $db->prepare( "SELECT `email` FROM `users` WHERE `email` = ?" );
$query->bindValue( 1, $email );
$query->execute();
if( $query->rowCount() > 0 ) { # If rows are found for query
$msg= '<div><img src="no.png"/>email already taken</div>';
} else {
$query = $db->prepare( "SELECT `mobile` FROM `users` WHERE `mobile` = ?" );
$query->bindValue( 1, $mobile );
$query->execute();
if( $query->rowCount() > 0 ) { # If rows are found for query
$msg= '<div><img src="no.png"/>This mobile number already taken</div>';
} else {
$query = $db->prepare( "SELECT `username` FROM `users` WHERE `username` = ?" );
$query->bindValue( 1, $username );
$query->execute();
if( $query->rowCount() > 0 ) { # If rows are found for query
$msg= '<div><img src="no.png"/>username already taken</div>';
} else {
if ($password==$password1) {
$options = array('cost' => 11);
$password=password_hash($password, PASSWORD_BCRYPT, $options);
$statement = $db->prepare("insert into users (email,mobile,username,password) values(?,?,?,?)");
$statement->execute(array($email,$mobile,$username,$password));
$msg="Registered Successfully";
header ("location: login-panel.php?feedback=Registered Successfully. Now you can log in");
} else {
$msg= '<div><img src="no.png"/>Password doesnt match </div>';
}
}
}
}
} else
?>
/*----------------------------------------------------------------
I want to use Bcrypt instead of md5. But I cant understand how to implement it.
Please can somebody add Bcrypt code to my coding. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment