1+ <?php
2+ // Change the below variables to reflect your MySQL database details
3+ $ DATABASE_HOST = 'localhost ' ;
4+ $ DATABASE_USER = 'root ' ;
5+ $ DATABASE_PASS = '' ;
6+ $ DATABASE_NAME = 'phplogin ' ;
7+ // Try and connect using the info above
8+ $ con = mysqli_connect ($ DATABASE_HOST , $ DATABASE_USER , $ DATABASE_PASS , $ DATABASE_NAME );
9+ // Check for connection errors
10+ if (mysqli_connect_errno ()) {
11+ // If there is an error with the connection, stop the script and display the error
12+ exit ('Failed to connect to MySQL: ' . mysqli_connect_error ());
13+ }
14+ // We can utilize the isset() function to check if the form has been submitted
15+ if (!isset ($ _POST ['username ' ], $ _POST ['password ' ], $ _POST ['email ' ])) {
16+ // Could not get the data that should have been sent
17+ exit ('Please complete the registration form! ' );
18+ }
19+ // Make sure the submitted registration values are not empty
20+ if (empty ($ _POST ['username ' ]) || empty ($ _POST ['password ' ]) || empty ($ _POST ['email ' ])) {
21+ // One or more values are empty.
22+ exit ('Please complete the registration form ' );
23+ }
24+ // Validate email address
25+ if (!filter_var ($ _POST ['email ' ], FILTER_VALIDATE_EMAIL )) {
26+ exit ('Email is not valid! ' );
27+ }
28+ // Validate username (must be alphanumeric)
29+ if (preg_match ('/^[a-zA-Z0-9]+$/ ' , $ _POST ['username ' ]) == 0 ) {
30+ exit ('Username is not valid! ' );
31+ }
32+ // Validate password (between 5 and 20 characters long)
33+ if (strlen ($ _POST ['password ' ]) > 20 || strlen ($ _POST ['password ' ]) < 5 ) {
34+ exit ('Password must be between 5 and 20 characters long! ' );
35+ }
36+ // Check if the username already exists
37+ if ($ stmt = $ con ->prepare ('SELECT id, password FROM accounts WHERE username = ? ' )) {
38+ // Bind parameters (s = string, i = int, b = blob, etc)
39+ $ stmt ->bind_param ('s ' , $ _POST ['username ' ]);
40+ $ stmt ->execute ();
41+ // Store the result so we can check if the account exists in the database
42+ $ stmt ->store_result ();
43+ // Check if the account exists
44+ if ($ stmt ->num_rows > 0 ) {
45+ // Username already exists
46+ echo 'Username already exists! Please choose another! ' ;
47+ } else {
48+ // Declare variables
49+ $ registered = date ('Y-m-d H:i:s ' );
50+ // We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in
51+ $ password = password_hash ($ _POST ['password ' ], PASSWORD_DEFAULT );
52+ // Username does not exist, insert new account
53+ if ($ stmt = $ con ->prepare ('INSERT INTO accounts (username, password, email, registered) VALUES (?, ?, ?, ?) ' )) {
54+ // Bind POST data to the prepared statement
55+ $ stmt ->bind_param ('ssss ' , $ _POST ['username ' ], $ password , $ _POST ['email ' ], $ registered );
56+ $ stmt ->execute ();
57+ // Output success message
58+ echo 'You have successfully registered! You can now login! ' ;
59+ } else {
60+ // Something is wrong with the SQL statement, check to make sure the accounts table exists with all 3 fields
61+ echo 'Could not prepare statement! ' ;
62+ }
63+ }
64+ // Close the statement
65+ $ stmt ->close ();
66+ } else {
67+ // Something is wrong with the SQL statement, check to make sure the accounts table exists with all 3 fields.
68+ echo 'Could not prepare statement! ' ;
69+ }
70+ // Close the connection
71+ $ con ->close ();
72+ ?>
0 commit comments