You should use the built in crypt
function:
You have two options:
Let PHP Crypt generate the salt
$user = mysql_real_escape_string($_POST['user']);
$pswd = mysql_real_escape_string($_POST['pass']);
//Salt is generated automatically
$encrypt = crypt( $pswd );
Generate the Salt yourself
$user = mysql_real_escape_string($_POST['user']);
$pswd = mysql_real_escape_string($_POST['pass']);
//These are the settings for the salt (Separated so you can understand it)
$algorithm = "2a";
$length = "12";
//Start the salt by specifying the algorithm and length
$salt = "$" . $algorithm . "$" . $length . "$";
//Add on random salt and make base64 adjusted for bcrypt's version
$salt .= substr( str_replace( "+", ".", base64_encode( mcrypt_create_iv( 128, MCRYPT_DEV_URANDOM ) ) ), 0, 22 );
//Encrypt with your generated salt
$encrypt = crypt( $pswd, $salt );
Verifying it is easy:
if ( $encrypted_pass_fromDB_with_salt === crypt( $passwrd, $encrypted_pass_fromDB_with_salt ) ) echo "ok";