I had been working on a project for some time, and then it went on the way back burner after my daughter was born. I'm back to it, and now I discover that I'm best off using PDO over MySQLi. So, I'm working on the conversion, and I'd like to get some confirmation that I'm handling things well. Essentially: Does this code look secure and sound? [It's just my account activation php.]
<?php
require_once ('common.php');
$x = $y = FALSE;
if (isset($_GET['x']) && filter_var($_GET['x'], FILTER_VALIDATE_EMAIL)) {
$x = $_GET['x'];
}
if (isset($_GET['y']) && (strlen($_GET['y']) == 32 )) {
$y = $_GET['y'];
}
if ($x && $y) {
$query = "SELECT 1 FROM users WHERE (email=:email AND confirm IS NULL)";
$query_params = array( ':email' => $x );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex) {
die("Failed to run query: " . $ex->getMessage());
}
$count = $stmt->rowCount();
if($count == 1) {
echo "<h3>Your account is already activated. No need to activate again. You may now log in.</h3>";
exit();
} else { // Not an active user
$query = "UPDATE users SET confirm=NULL WHERE (email=:email AND confirm=:confirm) LIMIT 1";
$query_params = array(
':email' => $x,
':confirm' => $y
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex) {
die("Failed to run query: " . $ex->getMessage());
}
$count = $stmt->rowCount();
if($count == 1) {
echo "<h3>Your account is now active. You may now log in.</h3>";
exit();
} else {
echo '<p class="error">Your account could not be activated. Please re-check the link or contact the system administrator.</p>';
exit();
}
}
} else {
$url = BASE_URL . 'index.html';
ob_end_clean();
header("Location: $url");
exit();
}
?>
I am aware that the $ex->getMessage()
needs to go before production.