How To Find Your Student Register Code
In this tutorial, I walk you through the complete process of creating a user registration organization where users can create an business relationship past providing username, email and countersign, login and logout using PHP and MySQL. I will also show you how you can make some pages attainable simply to logged-in users. Whatever other user non logged in will non exist able to admission the page.
The first matter we'll need to exercise is set our database.
Create a database chosenregistration. In the registration database, add a table calledusers. The users table volition take the following four fields.
- id
- username - varchar(100)
- email - varchar(100)
- password - varchar(100)
You can create this using a MySQL client like PHPMyAdmin.
Or you can create it on the MySQL prompt using the following SQL script:
CREATE Tabular array `users` ( `id` int(xi) Non NULL AUTO_INCREMENT Master KEY, `username` varchar(100) Non Cipher, `e-mail` varchar(100) NOT Null, `password` varchar(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
And that's it with the database.
At present create a binder calledregistration in a directory accessible to our server. i.e create the binder inside htdocs (if you are using XAMPP server) or insidewww(if y'all are using wampp server).
Inside the folder registration,create the following files:
Open these files up in a text editor of your selection. Mine is Sublime Text 3.
Registering a user
Open the annals.php file and paste the following code in it:
regiser.php:
<?php include('server.php') ?> <!DOCTYPE html> <html> <head> <title>Registration system PHP and MySQL</championship> <link rel="stylesheet" blazon="text/css" href="style.css"> </caput> <trunk> <div form="header"> <h2>Annals</h2> </div> <form method="postal service" activity="register.php"> <?php include('errors.php'); ?> <div class="input-group"> <label>Username</label> <input type="text" proper name="username" value="<?php echo $username; ?>"> </div> <div class="input-grouping"> <label>Email</label> <input type="electronic mail" proper name="electronic mail" value="<?php echo $electronic mail; ?>"> </div> <div class="input-group"> <label>Password</label> <input type="countersign" name="password_1"> </div> <div class="input-group"> <label>Confirm password</label> <input blazon="password" name="password_2"> </div> <div class="input-group"> <push button type="submit" class="btn" name="reg_user">Register</push> </div> <p> Already a member? <a href="login.php">Sign in</a> </p> </class> </body> </html>
Nothing complicated so far right?
A few things to note here:
Showtime is that our form'saction attribute is set to register.php. This means that when the form submit push button is clicked, all the data in the form volition exist submitted to the aforementioned folio (register.php). The part of the code that receives this grade data is written in the server.php file and that'due south why we are including it at the very acme of the register.php file.
Notice also that we are including the errors.php file to brandish class errors. We will come to that soon.
Every bit you can see in the caput department, we are linking to a style.css file. Open up upwards the style.css file and paste the following CSS in it:
* { margin: 0px; padding: 0px; } body { font-size: 120%; groundwork: #F8F8FF; } .header { width: 30%; margin: 50px machine 0px; color: white; groundwork: #5F9EA0; text-align: eye; edge: 1px solid #B0C4DE; border-lesser: none; border-radius: 10px 10px 0px 0px; padding: 20px; } form, .content { width: 30%; margin: 0px auto; padding: 20px; edge: 1px solid #B0C4DE; background: white; edge-radius: 0px 0px 10px 10px; } .input-group { margin: 10px 0px 10px 0px; } .input-group label { display: block; text-align: left; margin: 3px; } .input-group input { meridian: 30px; width: 93%; padding: 5px 10px; font-size: 16px; border-radius: 5px; border: 1px solid gray; } .btn { padding: 10px; font-size: 15px; color: white; background: #5F9EA0; border: none; border-radius: 5px; } .error { width: 92%; margin: 0px auto; padding: 10px; edge: 1px solid #a94442; color: #a94442; groundwork: #f2dede; edge-radius: 5px; text-align: left; } .success { colour: #3c763d; groundwork: #dff0d8; border: 1px solid #3c763d; margin-bottom: 20px; }
Now the form looks beautiful.
Let's now write the lawmaking that volition receive information submitted from the class and shop (register) the information in the database. Equally promised earlier, nosotros do this in the server.php file.
Open server.php and paste this code in it:
server.php
<?php session_start(); // initializing variables $username = ""; $electronic mail = ""; $errors = array(); // connect to the database $db = mysqli_connect('localhost', 'root', '', 'registration'); // Annals USER if (isset($_POST['reg_user'])) { // receive all input values from the form $username = mysqli_real_escape_string($db, $_POST['username']); $email = mysqli_real_escape_string($db, $_POST['email']); $password_1 = mysqli_real_escape_string($db, $_POST['password_1']); $password_2 = mysqli_real_escape_string($db, $_POST['password_2']); // form validation: ensure that the form is correctly filled ... // by adding (array_push()) corresponding mistake unto $errors array if (empty($username)) { array_push($errors, "Username is required"); } if (empty($email)) { array_push($errors, "E-mail is required"); } if (empty($password_1)) { array_push($errors, "Password is required"); } if ($password_1 != $password_2) { array_push($errors, "The two passwords do non match"); } // first check the database to make certain // a user does non already exist with the aforementioned username and/or e-mail $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$e-mail' LIMIT 1"; $result = mysqli_query($db, $user_check_query); $user = mysqli_fetch_assoc($effect); if ($user) { // if user exists if ($user['username'] === $username) { array_push($errors, "Username already exists"); } if ($user['email'] === $email) { array_push($errors, "email already exists"); } } // Finally, register user if there are no errors in the form if (count($errors) == 0) { $countersign = md5($password_1);//encrypt the password before saving in the database $query = "INSERT INTO users (username, electronic mail, password) VALUES('$username', '$email', '$password')"; mysqli_query($db, $query); $_SESSION['username'] = $username; $_SESSION['success'] = "You lot are now logged in"; header('location: alphabetize.php'); } } // ...
Sessions are used to track logged in users and so nosotros include a session_start() at the top of the file.
The comments in the lawmaking pretty much explain everything, only I'll highlight a few things here.
The if statement determines if the reg_user button on the registration form is clicked. Remember, in our form, the submit button has a proper noun attribute set up to reg_user and that is what we are referencing in the if statement.
All the data is received from the course and checked to make sure that the user correctly filled the form. Passwords are as well compared to make sure they lucifer.
If no errors were encountered, the user is registered in theusers table in the database with a hashed password. The hashed password is for security reasons. It ensures that even if a hacker manages to gain admission to your database, they would non be able to read your password.
But error messages are not displaying now because our errors.php file is still empty. To brandish the errors, paste this code in the errors.php file.
<?php if (count($errors) > 0) : ?> <div class="mistake"> <?php foreach ($errors as $error) : ?> <p><?php repeat $fault ?></p> <?php endforeach ?> </div> <?php endif ?>
When a user is registered in the database, they are immediately logged in and redirected to the alphabetize.php page.
And that's it for registration. Permit'south look at user login.
Login user
Logging a user in is an even easier matter to practise. Merely open the login page and put this code inside it:
<?php include('server.php') ?> <!DOCTYPE html> <html> <head> <championship>Registration organisation PHP and MySQL</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div course="header"> <h2>Login</h2> </div> <grade method="post" activeness="login.php"> <?php include('errors.php'); ?> <div form="input-group"> <label>Username</label> <input blazon="text" name="username" > </div> <div class="input-group"> <label>Password</label> <input type="password" name="password"> </div> <div class="input-group"> <button blazon="submit" class="btn" name="login_user">Login</button> </div> <p> Non yet a member? <a href="register.php">Sign upward</a> </p> </form> </body> </html>
Everything on this page is quite similar to the register.php page.
Now the code that logs the user in is to be written in the aforementioned server.php file. So open the server.php file and add this code at the cease of the file:
// ... // LOGIN USER if (isset($_POST['login_user'])) { $username = mysqli_real_escape_string($db, $_POST['username']); $password = mysqli_real_escape_string($db, $_POST['password']); if (empty($username)) { array_push($errors, "Username is required"); } if (empty($password)) { array_push($errors, "Password is required"); } if (count($errors) == 0) { $password = md5($countersign); $query = "SELECT * FROM users WHERE username='$username' AND countersign='$password'"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) { $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); }else { array_push($errors, "Incorrect username/password combination"); } } } ?>
Once more all this does is check if the user has filled the course correctly, verifies that their credentials lucifer a tape from the database and logs them in if information technology does. Afterwards logging in, the user is redirected them to the index.php file with a success bulletin.
Now allow's come across what happens in the alphabetize.php file. Open it up and paste the post-obit code in it:
<?php session_start(); if (!isset($_SESSION['username'])) { $_SESSION['msg'] = "You must log in kickoff"; header('location: login.php'); } if (isset($_GET['logout'])) { session_destroy(); unset($_SESSION['username']); header("location: login.php"); } ?> <!DOCTYPE html> <html> <head> <title>Abode</championship> <link rel="stylesheet" type="text/css" href="style.css"> </caput> <body> <div course="header"> <h2>Home Page</h2> </div> <div class="content"> <!-- notification message --> <?php if (isset($_SESSION['success'])) : ?> <div class="fault success" > <h3> <?php echo $_SESSION['success']; unset($_SESSION['success']); ?> </h3> </div> <?php endif ?> <!-- logged in user information --> <?php if (isset($_SESSION['username'])) : ?> <p>Welcome <strong><?php repeat $_SESSION['username']; ?></strong></p> <p> <a href="index.php?logout='i'" style="color: red;">logout</a> </p> <?php endif ?> </div> </trunk> </html>
The first if statement checks if the user is already logged in. If they are not logged in, they will be redirected to the login folio. Hence this page is attainable to but logged in users. If y'all'd similar to brand whatsoever page accessible only to logged in users, all y'all have to do is place this if statement at the meridian of the file.
The second if statement checks if the user has clicked the logout button. If yes, the system logs them out and redirects them back to the login folio.
And that's information technology!
Now go on, customize it to suit your needs and build an crawly site. If yous have any worries or anything you need to clarify, leave it in the comments below and help will come.
You lot can always support by sharing on social media or recommending my blog to your friends and colleagues.
Best regards :D
Awa Melvine
Yous might also like:
- How to create a blog in PHP and MySQL database
Source: https://codewithawa.com/posts/complete-user-registration-system-using-php-and-mysql-database
Posted by: congeroppers.blogspot.com
0 Response to "How To Find Your Student Register Code"
Post a Comment