Register
Hello There, Guest!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PHP] - HOW TO CREATE A SIMPLE REGISTRATION , LOGIN SCRIPT
#1
Star 
In this tutorial, i will be explaining how to create a simple Registration , Login Scripts 

What I will Cover: 

- How to create a Registration Script 
- How to create a login / logout Script 
- PHP / MySQL programming languages 

Things You will Need: 

- Basic Knowledge of PHP, MySQL, and HTML 

- A Host for PHP (Can be localhost) 

- A MySQL Server (Commonly, PHPmyADMIN is used) 

- A Will to Learn 
- 5 files => config.php , members.php , register.php , login.php, logout.php 

Notes: 

- These are simple scripts. Which mean there aren't made for publishing. I will create an advanced tutorial in due time. 

- I will not use MD5 in this tutorial 

- There are some section of this code that i will not discuss (MySQL Injections) 

- Do not FLAME. This tutorial took a while to create, thus only point out the errors or any confusion. I will make the corrections. 

- Code might be sloppy. However, it's simple and new developers can learn from this. 

- I will write down the method, then provide the code. 
- No i didn't copy and paste. 

Lets Begin. 

1) Create the DB and Table. 
- In this tutorial, we will keeps things pretty simple. 

Step 1: Creating the Database. 

- Open up your MySQL Server (e.g. phpmyadmin) 
- Create a new DB. We will be naming it our website's name. 

Step 2: Creating the Table 
- Locate the DB we just created, then create the Table. (Located under structure). 
- We will be naming it "members" and we will having 3 columns. 
- The 3 columns that we will be adding are: id, username, and password. 
- Check Picture for More Info 
https://imageshack.us/scaled/medium/211/addstuff.png

- Save. 




2) Connecting the MySQL Server AND DB to our webpage. 

Step 1: Open config.php 

Step 2: Set Variables of the MySQL Server. 


Code:
//Information to connect to your MySQL Server AND DB
$host     = "localhost";
$username = "root";
$password = "";
$db       = "mysite";
 

Step 3: Connect to the Server. If it can't connect then we will output that. 


Code:
$con = mysqli_connect($host,$username,$password,$db) or die("Can not connect to Server.");

Final Code 


Code:
<?php
/*
NOTES:
1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE.
2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS!
3) I WILL NOT USE MD5 IN THIS TUTORIAL
4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS.
5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS.
6) I DIDN'T MAKE THE VARIABLES FINAL ON PURPOSE! DON'T FLAME.
*/

//Information to connect to your MySQL Server AND DB
$host     = "localhost";
$username = "root";
$password = "";
$db       = "mysite";

//Connect to MySQL Server
$con = mysqli_connect($host,$username,$password,$db) or die("Can not connect to Server.");

?>
 




3) Register the member!. 

- Display a form 

- Once the user click submit, we will redirect to the same file and run a series a checks. 

- Check => See if the user enter all the fields (Username, Password, Repeat Password), if the passwords matches, and if the username exist 

- If the user pass all the test then we will add the user into the DB. 

-- In order to do this, we will need to query the DB. (Select * From TABLE NAME Where Username = '$user') 

-- Then we will need to check the rows. If a row is detected (row will return 1) then a username is already registered. 
--- We will need to insert the username and password into the DB table. If the username doesn't exist. 

- Register.php File 


Code:
<?php

/*
NOTES:
1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE.
2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS!
3) I WILL NOT USE MD5 IN THIS TUTORIAL
4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS.
5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS.
 
*/


session_start();  //Must Start a session.

require "config.php"; //Connection Script, include in every file!

//Check to see if the user is logged in.
//'isset' check to see if a variables has been 'set'
if(isset($_SESSION['username'])){
   header("location: members.php");
}

//Check to see if the user click the button
if(isset($_POST['submit']))
{
   //Variables from the table
   $user  = $_POST['user'];
   $pass  = $_POST['pass'];
   $rpass = $_POST['rpass'];
   
   //Prevent MySQL Injections
   $user  = stripslashes($user);
   $pass  = stripslashes($pass);
   $rpass = stripslashes($rpass);
   
   $user  = mysqli_real_escape_string($con, $user);
   $pass  = mysqli_real_escape_string($con, $pass);
   $rpass = mysqli_real_escape_string($con, $rpass);
   
   //Check to see if the user left any space empty!
   if($user == "" || $pass == "" || $rpass == "")
   {
      echo "Please fill in all the information!";
   }
   
   else
   {
      //Check too see if the user's Passwords Matches!
      if($pass != $rpass)
      {
         echo "Passwords do not match! Try Again";
      }
      
      //CHECK TO SEE IF THE USERNAME IS TAKEN, IF NOT THEN ADD USERNAME AND PASSWORD INTO THE DB
      else
      {
         //Query the DB
         $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user'") or die("Can not query the TABLE!");
         
         //Count the number of rows. If a row exist, then the username exist!
         $row = mysqli_num_rows($query);
         if($row == 1)
         {
            echo "Sorry, but the username is already taken! Try again.";
         }
         
         //ADD THE USERNAME TO THE DB
         else
         {
            $add = mysqli_query($con,"INSERT INTO members (id, username, password) VALUES (null, '$user' , '$pass') ") or die("Can't                Insert! ");
            echo "Successful! <a href='members.php'> Click Here </a> to log In.";
         }
         
         
      }      

   }
   
}
?>

<html>
<table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black">

<tr>
<form name="register" method="post" action="register.php">
<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>
<td colspan="3"><strong><center>Registration</center></strong></t
d>
</tr>

<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="user" type="text" id="user"></td>
</tr>

<tr>
<td>Password</td>
<td>:</td>
<td><input name="pass" type="password" id="pass"></td>
</tr>

<tr>
<td>Repeat Password</td>
<td>:</td>
<td><input name="rpass" type="password" id="rpass"></td>
</tr>

<tr>
<td></td>
<td></td>
<td><input type="submit" name="submit" value="Register"></td>
</tr>

</table>
</td>
</form>
</tr>
</table>

</html>
 



4) Logging in a member 

- Display a Form for the member (Username and Password) 

- Once clicked, we will process the form and do some checks. 

- Checks => See if the user left any blank spaces, if the username / pass matches the DB username and password 

- If the user pass the Checks, then we will create a session for that user. 


-login.php file 


Code:
<?php

/*
NOTES:
1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE.
2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS!
3) I WILL NOT USE MD5 IN THIS TUTORIAL
4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS.
5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS.
*/

session_start();
require "config.php"; //Connection Script, include in every file!

//Check to see if the user is logged in.
if(isset($_SESSION['username'])){
   header("location: members.php"); //isset check to see if a variables has been 'set'
}



if(isset($_POST['submit']))
{
   //Variables from the table
   $user  = $_POST['user'];
   $pass  = $_POST['pass'];
      
   //Prevent MySQL Injections
   $user  = stripslashes($user);
   $pass  = stripslashes($pass);
   
   $user  = mysqli_real_escape_string($con, $user);
   $pass  = mysqli_real_escape_string($con, $pass);
   
   //Check to see if the user left any space empty!
   if($user == "" || $pass == "")
   {
      echo "Please fill in all the information!";
   }
   
   //Check to see if the username AND password MATCHES the username AND password in the DB
   else
   {
      $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user' and password = '$pass'") or die("Can not query DB.");
      $count = mysqli_num_rows($query);
      
      if($count == 1){
         //YES WE FOUND A MATCH!
         $_SESSION['username'] = $user; //Create a session for the user!
         header ("location: members.php");
      }
      
      else{
         echo "Username and Password DO NOT MATCH! TRY AGAIN!";
      }
   }
   
}

?>

<html>
<table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black">

<tr>
<form name="register" method="post" action="login.php">
<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>
<td colspan="3"><strong><center>Login </center></strong></td>
</tr>

<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="user" type="text" id="user"></td>
</tr>

<tr>
<td>Password</td>
<td>:</td>
<td><input name="pass" type="password" id="pass"></td>
</tr>

<tr>
<td></td>
<td></td>
<td><input type="submit" name="submit" value="Login"></td>
</tr>

</table>
</td>
</form>
</tr>
</table>

</html>
 




5) Logging out a member 

- We will want to destroy the session. 
- We will not check to see if the user is logged in or not (Next Tutorial) 

Code:
<?php
/*
NOTES:
1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE.
2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS!
3) I WILL NOT USE MD5 IN THIS TUTORIAL
4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS.
5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS.
*/

session_start();
require "config.php";
session_destroy();

echo "You have successfully logged out. <a href='login.php'> Click here </a> to login!";

?>
 




6) Member's Page 

- The member's page is normally referred as an Account page (Next Tutorial) or any pages that requires a member to be logged in. 

- We will check to determine if the user is logged in or not (Check for an active Session). 
- Tell the member to either log in or log out. 


Code:
<?php
/*
NOTES:
1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE.
2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS!
3) I WILL NOT USE MD5 IN THIS TUTORIAL
4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS.
5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS.
6) NOT NEEDED BUT IF YOU WANT THE ONLY USERS TO VIEW AN APPLICATION THEN THIS MIGHT BE USEFUL.
*/

//IF THE USER IS LOGGED IN THEN TELL THE USER, ELSE TELL THE USER TO LOG IN!
session_start();
require "config.php";

//Check to see if the user is logged in.
if(isset($_SESSION['username'])){
   echo "Hello ".$_SESSION['username'].", you are logged in. <br /> This the member's page! Nothing here :(. <a href='logout.php'>Click Here </a>to log out.";
}

else{
   echo "Please <a href='login.php'>Log In </a> to view the content on this page!";
}

?>
 


Hope This Helps Rolleyes
[-] The following 1 user Likes Owl 's post:
  • Squirrel
Reply
#2
This brings back memories from when I was studied PHP and MySQL.

How about a tutorial on JavaScript? It could relate since Agar.io and Agma.io are primarily coded using it.



[Image: giphy.gif][Image: giphy.gif][Image: giphy.gif][Image: giphy.gif]
[-] The following 1 user Likes Shadow Fox's post:
  • Sora
Reply
#3
(12-21-2017, 02:10 AM)Shadow Fox Wrote: This brings back memories from when I was studied PHP and MySQL.

How about a tutorial on JavaScript? It could relate since Agar.io and Agma.io are primarily coded using it.



[Image: giphy.gif][Image: giphy.gif][Image: giphy.gif][Image: giphy.gif]

On It
Reply
#4
Heyy!
Nice tutorial!
Great work on putting the code within coding bracket tags.
These kind of tutorials are where most of us, if not all started Smile Plus, a great way to learn is by teaching.
Have any questions? Send me a message here: http://agarioforums.net/private.php?action=send&uid=1
Reply
#5
(12-21-2017, 09:01 AM)Sora Wrote: Heyy!
Nice tutorial!
Great work on putting the code within coding bracket tags.
These kind of tutorials are where most of us, if not all started Smile Plus, a great way to learn is by teaching.

I Agree
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)