I've made BCrypt and PDO registration script I'm staging to redo my signup page and get rid 90% of the current file size.
So I forgot to include a form last time, i've included one this time. It works as it is. Next will be a stand-alone login form supporting this script. PDO Edition does the same thing, only different code.
This is about as simple as it gets. This bypasses TT entirely this time.. as do all my mods, just for kicks ?
In this release, I transferred over my avatar upload feature with thumbnail resizing (image_thumb.ext) and original (original filename). It's saved me many GB's worth of bandwidth alone since February. Its a must-have for high traffic and a lot of users.
Add to backend/functions.php
Now create register.php
Now create images/avatars and CHMOD 777.
Create as account-register.php
I added the filter module check, and fallback to unsafe vars if not installed.
Just a working basic skeleton that needs a dress. I don't know bootstrap, and it breaks everything I use so I'll leave that up to you.
NOTE: The salt attribute for BCrypt are depreciated as of PHP 7.0 and is encouraged to let password_hash() do it instead. I've put it in anyway for compatibility.
[ATTACH type="full" alt="Register.png"]5574[/ATTACH]
So I forgot to include a form last time, i've included one this time. It works as it is. Next will be a stand-alone login form supporting this script. PDO Edition does the same thing, only different code.
This is about as simple as it gets. This bypasses TT entirely this time.. as do all my mods, just for kicks ?
In this release, I transferred over my avatar upload feature with thumbnail resizing (image_thumb.ext) and original (original filename). It's saved me many GB's worth of bandwidth alone since February. Its a must-have for high traffic and a lot of users.
Add to backend/functions.php
function imageResize($imageSrc, $imageWidth, $imageHeight)
{
$newImageWidth = 200;
$newImageHeight = 200;
$newImageLayer = imagecreatetruecolor($newImageWidth, $newImageHeight);
imagecopyresampled($newImageLayer, $imageSrc, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $imageWidth, $imageHeight);
return $newImageLayer;
}
Now create register.php
<?
require_once 'backend/functions.php';
if (isset($_POST['submit'], $_POST['username'], $_POST['password'])) {
if (is_array($_FILES)) {
$uploadedFile = $_FILES['avatar']['tmp_name'];
$sourceProperties = getimagesize($uploadedFile);
$newFileName = sha1_file($uploadedFile);
$dirPath = "images/avatars/";
$ext = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
$imageType = $sourceProperties[2];
switch ($imageType) {
case IMAGETYPE_PNG:
$imageSrc = imagecreatefrompng($uploadedFile);
$tmp = imageResize($imageSrc, $sourceProperties[0], $sourceProperties[1]);
imagepng($tmp, $dirPath . $newFileName . "_thumb." . $ext);
break;
case IMAGETYPE_JPEG:
$imageSrc = imagecreatefromjpeg($uploadedFile);
$tmp = imageResize($imageSrc, $sourceProperties[0], $sourceProperties[1]);
imagejpeg($tmp, $dirPath . $newFileName . "_thumb." . $ext);
break;
case IMAGETYPE_GIF:
$imageSrc = imagecreatefromgif($uploadedFile);
$tmp = imageResize($imageSrc, $sourceProperties[0], $sourceProperties[1]);
imagegif($tmp, $dirPath . $newFileName . "_thumb." . $ext);
break;
default:
echo "Invalid avatar image. Please <a href='" . $site_config['SITEURL'] . "/signup/'>go back</a> and try again.";
exit;
break;
}
move_uploaded_file($uploadedFile, $dirPath . $newFileName . "." . $ext);
}
try {
$dsn = 'mysql:dbname=database;host=localhost';
$user = 'username';
$password = 'password';
$conn = new PDO($dsn, $user, $password);
if (function_exists("filter_var")) {
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
} else {
echo "Filter extension for PHP " . phpversion() . " not found. Using unsafe instead.";
$username = $_POST["username"];
$email = $_POST["email"];
}
$avatar = $dirPath . $newFileName . "." . $ext;
$avatar_thumb = $dirPath . $newFileName . "_thumb." . $ext;
$password = $_POST["password"];
$saltstr = '_salt56789012345678901';
$salt = password_hash($password, PASSWORD_BCRYPT, array(
'salt' => $saltstr
));
$cryptedPassword = password_hash($password, PASSWORD_BCRYPT, array(
'cost' => 11
));
$sql = "INSERT INTO users (username, password, secret, email, status, added, last_access, age, country, gender, client, stylesheet, language, class, avatar, avatar_thumb) VALUES (:username, :cryptedPassword, :secret, :email, 'confirmed', NOW(), NOW(), 29, 1, 'Male', 'uTorrent', 1, 1, 1, :avatar, :avatar_thumb)";
$stmt = $conn->prepare($sql);
if ($stmt) {
$stmt->bindParam(":username", $username);
$stmt->bindParam(":cryptedPassword", $cryptedPassword);
$stmt->bindParam(":email", $email);
$stmt->bindParam(":secret", $salt);
$stmt->bindParam(":avatar", $avatar);
$stmt->bindParam(":avatar_thumb", $avatar_thumb);
$result = $stmt->execute();
echo $result ? 'ok' : 'Query successful';
} else {
throw new Exception('Preparing Query Failed');
}
}
catch (Exception $e) {
echo $e->getMessage();
}
}
?>
Now create images/avatars and CHMOD 777.
Create as account-register.php
<?
require_once("backend/functions.php");
dbconn(true); stdhead("Sign Up"); begin_frame("Register");
?>
<form method="POST" enctype="multipart/form-data" action="/register.php" autocomplete="off">
<input type="text" name="username" placeholder="Your Username" /><br><br>
<input type="email" name="email" placeholder="Your E-mail" /><br><br>
<input type="password" name="password" placeholder="Your Password" /><br><br>
<input type="file" name="avatar" accept="image/*"><br><br>
<button type="submit" name="submit">Sign Up</button>
</form>
<? end_frame(); stdfoot(); ?>
I added the filter module check, and fallback to unsafe vars if not installed.
Just a working basic skeleton that needs a dress. I don't know bootstrap, and it breaks everything I use so I'll leave that up to you.
NOTE: The salt attribute for BCrypt are depreciated as of PHP 7.0 and is encouraged to let password_hash() do it instead. I've put it in anyway for compatibility.
[ATTACH type="full" alt="Register.png"]5574[/ATTACH]