Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 27, 2023 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/2a05d7cab43bfe525c7509417e375caa to your computer and use it in GitHub Desktop.
Save code-boxx/2a05d7cab43bfe525c7509417e375caa to your computer and use it in GitHub Desktop.
PHP MYSQL User Registration

PHP MYSQL USER REGISTRATION

https://code-boxx.com/user-registration-php-mysql/

NOTES

  1. Create a database and import 1-users.sql.
  2. Edit 2-user-lib.php, change the database settings to your own.
  3. Launch 3-register.php in the browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `users` (
`user_id` bigint(20) NOT NULL,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `user_email` (`user_email`);
ALTER TABLE `users`
MODIFY `user_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
<?php
class Users {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $error = null;
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct () {
if ($this->stmt !== null) { $this->stmt = null; }
if ($this->pdo !== null) { $this->pdo = null; }
}
// (C) RUN SQL QUERY
// $sql : sql to run
// $data : data to bind
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) GET USER BY ID OR EMAIL
// $id : email or user id
function get ($id) {
$this->query(sprintf("SELECT * FROM `users` WHERE `user_%s`=?",
is_numeric($id) ? "id" : "email"
), [$id]);
return $this->stmt->fetch();
}
// (E) REGISTER USER
// $name : user name
// $email : user email
// $pass : password
function register ($name, $email, $pass) {
// (E1) CHECK
if (is_array($this->get($email))) {
$this->error = "$email is already registered";
return false;
}
// (E2) ADD NEW USER
$this->query(
"INSERT INTO `users` (`user_name`, `user_email`, `user_password`) VALUES (?,?,?)",
[$name, $email, password_hash($pass, PASSWORD_DEFAULT)]
);
return true;
}
}
// (F) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (G) CREATE USER OBJECT
$USR = new Users();
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
div.error {
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ff9797;
background: #ffeaea;
}
#register {
max-width: 400px;
padding: 20px;
border: 1px solid #e9e9e9;
background: #f2f2f2;
}
#register h2 {
padding: 0;
margin: 0 0 10px 0;
}
#register input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
}
#register input[type=text], #register input[type=email], #register input[type=password] {
border: 1px solid #d9d9d9;
}
#register input[type=submit] {
margin-top: 20px;
color: #fff;
background: #3c65c3;
border: 0;
cursor: pointer;
}
function check () {
let pass = document.getElementsByName("pass")[0].value,
cpass = document.getElementsByName("cpass")[0].value;
if (pass != cpass) {
alert("Passwords do not match");
return false;
} else { return true; }
}
<?php
/* (A) ALREADY SIGNED IN - TO HOME PAGE
if (isset($_SESSION["user"])) {
header("Location: HOME.PAGE");
exit();
}
*/
// (B) PROCESS REGISTRATION
if (count($_POST)>0) {
require "2-user-lib.php";
$pass = $USR->register($_POST["name"], $_POST["email"], $_POST["pass"]);
if ($pass) {
// header("Location: HOME.PAGE");
exit("SUCCESSFUL!");
}
}
// (C) REGISTRATION FORM ?>
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
<meta charset="utf-8">
<link rel="stylesheet" href="3-register.css">
<script src="3-register.js"></script>
</head>
<body>
<!-- (C1) ERROR MESSAGE (IF ANY) -->
<?php
if (isset($pass) && $pass==false) {
echo "<div class='error'>$USR->error</div>";
} ?>
<!-- (C2) REGISTRATION FORM -->
<form method="post" id="register" onsubmit="return check()">
<h2>REGISTER</h2>
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="pass" placeholder="Password" required>
<input type="password" name="cpass" placeholder="Confirm Password" required>
<input type="submit" value="Register">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment