Hi
i've been working on a signup forum for my site using pdo, but have came across a problem i been trying to fix for weeks with no luck at all. My problem is when i sign up and submit my info, im getting The given E-mail address is not valid. and Passwords do not match. but the emails are valid and the passwords do match any help would be great thanks.
Sign up code:
require("config.php");
require("globalfunction.php");
if(!empty($_POST))
{
$query = "SELECT id FROM signup WHERE user = :user";
$query_params = array(
':user' => $_POST['user']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$errors = array();
if (trim($_POST['user']) == '') { $errors[] = "Please Enter A Username."; }
if (trim($_POST['pass']) == '') { $errors[] = "Please Enter A Password."; }
if (trim($_POST['confirmpass']) == '') { $errors[] = "Please Enter A confirmation password."; }
if (trim($_POST['mailaddress']) == '') { $errors[] = "Please Enter Your E-mail Address."; }
if (trim($_POST['mailaddressconfirm']) == '') { $errors[] = "Please Enter Your Conformation E-mail Address."; }
if (strlen($pass) < $passLengthMIN ) { $errors[] = "The password contains to little characters."; }
if (strlen($pass) > $passLengthMAX ) { $errors[] = "The password contains to many characters."; }
if (strlen($user) < $userLengthMIN ) { $errors[] = "The username contains to little characters."; }
if (strlen($user) > $userLengthMAX ) { $errors[] = "The username contains to many characters."; }
if (validaddress($mailaddress) == false ) { $errors[] = "The given E-mail address is not valid."; }
if ($pass <> $confirmpass ) { $errors[] = "Passwords do not match."; }
if ($mailaddress <> $mailaddressconfirm ) { $errors[] = "Email Address do not match."; }
$query = "SELECT * FROM signup WHERE user = :user Or mailaddress = :mailaddress";
$query_params = array(
':user' => $user,
':mailaddress' => $mailaddress
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
header("Location: error");
exit;
}
if ($row = $stmt->fetch($result)){
if ($row['user'] == $user) { $errors[] = "Username, ".htmlentities($row['username']).", is already in use."; }
if ($row['mailaddress'] == $mailaddress) { $errors[] = "E-mail address, ".htmlentities($row['mailadres']).", is already in use."; }
}
if ($errors) {
$errorstr = "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>";
echo $errorstr;
} else {
$errorstr = "";
$datetime = date("d-m-Y G:i ");
$sth = $db->prepare("INSERT INTO signup (user,pass,salt,mailaddress,signupdate,lastlogin
) VALUES (:user,:pass,:salt,:mailaddress,:signupdate,:lastlogin)");
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$pass = hash('sha256', $_POST['pass'] . $salt);
$query_params = array(
':user' => $_POST['user'],
':pass' => $pass,
':salt' => $salt,
':mailaddress' => $_POST['mailaddress'],
':signupdate' => $datetime,
':lastlogin' => $lastlogin
);
$sth->execute($query_params);
header("Location: signup");
exit;
}
}
Global Function for email address:
function validaddress($mailaddress){
$prereturn = true;
if (strlen($mailaddress) < 5){$prereturn = false;}
$partsNumber = split("@",$mailaddress);
if (count($partsNumber) <> 2) {$prereturn = false;}
else{
list($user,$domain) = split("@",$mailaddress);
if (strlen($user) < 1) {$prereturn = false;}
}
return $prereturn;
}