PHP

Simple Registration,login,logout in PHP

Register and Log In and Log Out Module

Sign Up -> Steps to follow:

1) Get User details from HTML form

2) Store User data in Data Base using PHP

Log In -> Steps to follow:

1) Get Login details from HTML form

2)   (i) Retrive User data from Data Base using PHP

(ii) Matching User Details

(iii) Match found then redirect to next page ( Also set some session variables )

(iv) Match not found show error message and redirect to login page

Check logged in or not -> Steps to follow:      ( it’s a PHP code)

1) Get session variables ( they created during login )

2) If session are empty show error and redirect to login page

Log Out -> Steps to follow:     ( it’s a PHP code)

1) Start session

2) Destroy session variables when logout clicked (this will prevent user enter after logout)

 

 

A) Simple Registration code:

1) (filename -> form.html )

HTML =>  <html>

<head>

<title> Registration </title>

</head>

<body>

<form method=”post” action=”register.php” >

User name: <input type=”text” name=”user” />

Email id: <input type=”email” name=”email” />

Password: <input type=”password” name=”password” />

<input type=”submit” name=”submit”  value=”Register” />

</form>

</body>

</html>

 

2) ( filename ->  register.php )

PHP =>

<?php                      //PHP open syntax

if($_POST[‘submit’]==’Register’)

{

$user = $_POST[‘user’];

$email = $_POST[’email’];

$password = password_hash($_POST[‘password’]);  //password_hash() is a hashing function

 

$host=”localhost”;  $user=”root”;  $password=”12345″;  $db=”data_collect”;

$con = mysqli_connect($host,$user,$password,$db);                      // Connecting MySQL data base

$sql = ” insert into user_table (user_name,email_id,password) values ( ‘$user’ ,                ‘$email’ , ‘$password’ ) “;                                    // SQL code for insert data in table

$result = mysqli_query ( $con , $sql );                                     // Executes sql code

if ( $result )

{ echo ” Registeration Success “;

}

else

{ echo ” Registeration Failed \n Try again “;                          // Error message

}

}

?>                  //PHP close syntax

B) Simple login code:

1) ( filename -> login.html )

HTML =>  <html>

<head>

<title> Registeration </title>

</head>

<body>

<form method=”post” action=”login.php” >

Email id: <input type=”email” name=”email” />

Password: <input type=”password” name=”password” />

<input type=”submit” name=”submit”  value=”Login” />

</form>

</body>

</html>

 

2) ( filename -> login.php )

PHP =>

<?php                      //PHP open syntax

if($_POST[‘submit’]==’Login’)

{

$email = $_POST[’email’];

$password = $_POST[‘password’];

 

$host=”localhost”;  $user=”root”;  $password=”12345″;  $db=”data_collect”;

$con = mysqli_connect($host,$user,$password,$db);      // Connecting MySQL data base

$sql=”select password from user_table where email_id=’$email'”;    // SQL code for get                                                                                                                                                                    data in table

$result = mysqli_query ( $con , $sql );   // Executes sql code

if ( mysqli_num_rows( $result ) > 0 )     // Code to check your email registered or not

{

$data=mysqli_fetch_assoc($result);     // Getting password from db

if(password_verify ( $password , $data [‘password’] ) ) {     // password_verify()                                                                                      function verify given password with hashed password

{

$_SESSION[‘loggedin’] = “true”;  // session memory for checking logged in or not

$_SESSION[“email”] = $email;   // session memory for checking which user logged in

header(‘Location: home.html’);           // header() function can redirect web pages..                                        ( home.html file must be in same folder as login.php for this redirect to work…. )

}

else

{ echo ” Incorrect Password “;

}

}

else

{ echo ” Your email not registered “;

}

}

?>                  //PHP close syntax

C) Simple login check code: ( Just Put below code as top of every page )

PHP =>

<?php

session_start();

$status = $_SESSION[‘loggedin’];    ( Accessing session variable that created when login )

$email = $_SESSION[’email’];

if( $email == “” or $status == “” ) {

echo ” Please login “;

header(‘Location: login.html’);     //Redirect to login page

}

?>

D) Simple log out code:

PHP =>  ( file name -> logout.php )

<?php

session_start();

session_unset();   // remove all session variables

session_destroy();  // destroy the session

echo ” Log out success ” ;

header(‘Location: login.html’);   //Redirect to login page

?>

 

 

 

 

 

 

 

 

 

Leave a Reply