I keep trying to get it to work, when I click register with under 3 characters on either or both of the fields it won't pop up with a box saying what it's supposed to say, and when I click register with above 3 on both it won't give a confirmation or an error let alone insert the information into the database.
<?php
$sqlHost = 'localhost';
$sqlUser = 'root';
$sqlPass = 'hidthepassword';
$sqlDatabase = 'RPG';
$connection = new PDO('mysql:host='.$sqlHost.';dbname='.$sqlDatabase.';charset=utf8', $sqlUser, $sqlPass);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$username = null;
$password = null;
if(isset($_GET['user'])) {
$username = $_GET['user'];
}
if(isset($_GET['pass'])) {
$pass = $_GET['pass'];
}
if((isset($username)) && (isset($password))) {
// TODO SQL
$salt = genSalt(40);
$passHash = md5(md5($salt) . md5($password));
$statement = $connection->prepare("INSERT INTO `rpg`.`accounts` (`id`, `username`, `password`, `salt`) VALUES (:user, :pass, :salt);");
$statement->bindParam(":user", $username);
$statement->bindParam(":pass", $passHash);
$statement->bindParam(":salt", $salt);
if($statement->execute()) {
echo "Thank-you for your registration, " . $username;
} else {
echo "Sorry, your registration failed.";
}
} else {
// DISPLAY
echo '<input type="text" id="user" placeholder="username"/>
<input type="text" id="pass" placeholder="password"/>
<button id="button">Register</button>
<script>
var r = document.getElementById("button");
button.addEventListener("click", function() {
var user = document.getElementById("user");
var pass = document.getElementById("pass");
if(user.value.length < 3 || pass.value.length < 3) {
alert("Please enter a valid username or password");
} else {
window.location = "index.php?user="user.value+"&pass="+pass.value;
}
));
</script>
';
}
function genSalt($length) {
$variables = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789";
$charLength = strlen($variables);
$returned = "";
for($i = 0; $i < $length; $i++) {
$returned .= $variables[rand(0, ($charLength - 1))];
}
return $returned;
}
?>