You are here:Home » PHP » User Registration And Login System Using PHP and Mysql

User Registration And Login System Using PHP and Mysql

In this Blog I am going to explain you a complete PHP Registration, validation, Login and User Interface Script. This script is Complete in the sense that this will let you register and save those inputs to database table “user_signup”. Empty field validations, compare field validation, and username uniqueness script is implemented. Other validations can also be implemented easily.
Below are the features you can see in this script.
           Javascript validations are also done.
     Email confirmation script to confirm the email after successful user registration.
    User activation through email confirmation ( For this I have used : user_activation_code, user_account_status  columns in database)
   User Login script.
    User Interface with session implementation.
)        User Logout script to let destroy User Session and take him back to the login page.

The PHP pages I have created to implement above functionality are:
)      Index.php   --   links to signup.php and login.php pages

)      signup.php -- User Registration Form and complete registration code with javascript validations and PHP code validations. It also sends an email to the user emal id upon successful user registration


)      signup_success.php – redirects the user to this page after successful user registration.

)      email_conf.php – user opens there email ID account and click the confirmation link which they have received from us. Email_conf.php performs all the logic to implement the email confirmation and activates the user account. Now the user can login to their user account
)      login.php – This is the user’s login section to enter their login area. Here we create the SESSION variable and use it in user area to maintain the users identity in their respective user section.
)      user_home.php – after successful login user comes to user_home.php page. Here we maintain the user session.
)      logout_user.php – logout_user.php script let user to logout there user area and destroy the current user section.

Mysql Database table script: 
Table Nameuser_signup
CREATE TABLE IF NOT EXISTS `user_signup` (
  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_name` varchar(50) NOT NULL,
  `user_full_name` varchar(50) NOT NULL,
  `user_email` varchar(50) NOT NULL,
  `user_password` varchar(50) NOT NULL,
  `user_address` varchar(50) NOT NULL,
  `user_phone` varchar(50) NOT NULL,
  `user_city` varchar(50) NOT NULL,
  `user_state` varchar(50) NOT NULL,
  `user_account_status` tinyint(4) DEFAULT '0',
  `user_activation_code` varchar(50) NOT NULL,
  `user_news_letter_status` tinyint(4) DEFAULT '0',
  `signup_date` datetime NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Index.php Script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<a href="signup.php">Sign Up</a><br />

<a href="login.php">LogIn Section</a>

</body>

</html>



signup.php Script:

<?php
$error_list = "";
    if(isset($_POST['submitted']))
    {
        $errors = array();
       
         if (empty($_POST['user_name']))
         {
         $errors[] = 'You forgot to enter user name.';
         }
         else
         {
       
                 $user  = $_POST['user_name'];
       
                 require_once('includes/mysqli_connect.php');
               
                 mysql_select_db(DB_NAME, $dbc1);
               
                 $query = 'select * from user_signup where user_name="'. mysql_real_escape_string($user,$dbc1) .'"';
               
                 $result = mysql_query($query, $dbc1) or die(mysql_error($dbc1));
       
                     $available = false;
                   
                     if(mysql_num_rows($result)>0)
                     {
                   
                     $available = false;
                   
                   
                     }
                     else
                     {
                   
                        $available = true;
                     }
       
                     mysql_free_result($result);
                      
                      
                       if($available == true)
                       {
                        $user_name = trim($_POST['user_name']);
                       }else
                       {
                       $errors[] = 'Your username is already taken choose a different username';
                       }
               
               
               
         }
       
                
         if (empty($_POST['full_name']))
         {
         $errors[] = 'You forgot to enter your complete name.';
         }
         else
         {
         $full_name = trim($_POST['full_name']);
         }
       
                
         if (empty($_POST['user_email']))
         {
         $errors[] = 'You forgot to enter your email.';
         }
         else
         {
         $user_email = trim($_POST['user_email']);
         }
       
        // Check for a password and match against the confirmed password:
         if (!empty($_POST['user_pwd1']))
          {
             if ($_POST['user_pwd1'] != $_POST['user_pwd'])
             {
           
             $errors[] = 'Your password did not match the confirmed password.';
           
             }
             else
             {
             $user_pwd = trim($_POST['user_pwd1']);
             }
        }else
        {
             $errors[] = 'You forgot to enter your password.';
        }
 
        if (empty($_POST['address']))
         {
          $errors[] = 'You Forget to enter your Address';
         }
         else
         {
         $address = trim($_POST['address']);
         }
         if (empty($_POST['phone_no']))
         {
          $errors[] = 'Please enter your Phone no';
         }
         else
         {
         $phone_no = trim($_POST['phone_no']);
         }
       
         if (empty($_POST['city']))
         {
          $errors[] = 'Please mention your city';
         }
         else
         {
         $city = trim($_POST['city']);
         }
       
         if (empty($_POST['state']))
         {
         $errors[] = 'Please select your state';
         }
         else
         {
         $state = trim($_POST['state']);
         }
       
         if (empty($_POST['ck_newsletter']))
         {
         $ck_newsletter = 0;
         }
         else
         {
         $ck_newsletter = 1;
         }
       
         if(empty($errors))
         {
            
           
           
            require_once('includes/mysqli_connect.php');
           
            $act_code = mt_rand(). mt_rand() . mt_rand();
           
                $query = "INSERT into user_signup(user_name, user_full_name, user_email, user_password, user_address, user_phone, user_city, user_state, user_account_status, user_activation_code, user_news_letter_status,signup_date)values('$user_name', '$full_name', '$user_email',     PASSWORD('$user_pwd'),'$address','$phone_no', '$city','$state',0, '$act_code', $ck_newsletter, NOW() )";
               
                $result = @mysqli_query($dbc, $query); //or die(@mysqli_error('problem in query'));
               
                if($result)
                {
               
                        $to  = $user_email;
                        $subject = 'Account activation mail';
                                               
                        $headers = "From: " . strip_tags('donotreply@mohitsharma.com') . "\r\n";
                        $headers .= "Reply-To: ". strip_tags('donotreply@mohitsharma.com') . "\r\n";
                        //$headers .= "CC: susan@example.com\r\n";
                        $headers .= "MIME-Version: 1.0\r\n";
                        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                       
                        $message ='
                            <html>
                                <head>
                                <title>Account Activation Mail</title>
                                </head>
                                <body>
                                <p>Hi '.$full_name.', <br>
                                Thank you for Registration<br>

                                <p>Here is your Account Detail<br/>
                               
                                <table>
                                <tr>
                                <td>LoginId </td> <td>'.$user_name.'</td>
                                </tr>
                               
                                <tr>
                                <td>Password </td> <td>'.$user_pwd.'</td>
                                </tr>
                               
                                <tr>
                                <td>Email </td> <td>'.$user_email.'</td>
                                </tr>
                               
                                <tr>
                                <td>Phone </td> <td>'.$phone_no.'</td>
                                </tr>
                               
                               
                                </table>
                               
                                </p>
                                <P>Please Click the below link to confirm your email ID and to complete the fist account activation step <br/>
                                <a href="http://786info.net/signup1/email_conf.php?act_code='.$act_code.'">Confirm Email</a>
                                </p>
                                </body>
                            </html>';
                           
                            if (mail($to, $subject, $message, $headers))
                            {
                                  header('refresh: 3; URL=signup_success.php?');
                                  exit();
                            }
                            else
                            {
                              echo 'There was a problem sending the email.';
                            }
                           
                           
                                  exit();
                           
                } else
               
                {
                   
                      // If it did not run OK.
                   
                     // Public message:
                     echo '<h1>Error!</h1>
                     <p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
                   
                     // Debugging message:
                     echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $query .'</p>';

                }
               
                 mysqli_close($dbc); // Close the database connection.

                 // Include the footer and quit the script:
               
                 exit();                 
         }
         else
         {
         $error_list ='<span><h2>Please Fill Below mentioned Fields</h2><hr>
             <p class="error"><b>The following error(s) occurred:</b><br />';
       
             foreach ($errors as $msg)
             { // Print each error.
             $error_list.= " - $msg<br />\n";
             }
              $error_list .= "</span></p><hr>";
           
         } // error condition ends here*/
    } // submitted statement ends here
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="Stylesheet" href="style/style.css" />
    <title>Home Page</title>
<script LANGUAGE="JavaScript">
<!--

function ValidateForm(form){
    ErrorText= "";
    if (form.user_name.value == "") {ErrorText= "\nPlease enter your user name"}
    if (form.full_name.value == "") {ErrorText += "\nPlease enter your complete name"}

    if ((form.user_email.value == "" || form.user_email.value.indexOf('@', 0) == -1) || form.user_email.value.indexOf('.') == -1)
        {

        ErrorText+=     "\nPlease enter a proper value for your email"
       
        }
    if(form.user_pwd1.value == "" || (form.user_pwd1.value != form.user_pwd.value))
    {
    ErrorText +="\nYour Password does not matches the confirmed Password";
   
    }
   
    if(form.user_pwd1.value.length < 5){ ErrorText +="\nYour Password must be atleast 5 character long.";}
   
    if (form.phone_no.value == "") {ErrorText += "\nPlease enter your phone no"}
    if (form.city.value == "") {ErrorText += "\nPlease enter your city name"}
    if (form.state.value == "--Select--") {ErrorText += "\nPlease select your state"}
   
if (ErrorText!= "")
        {
        alert("Please check your Form submission mistakes :\n" + ErrorText);
        return false;
        }

       
        if (ErrorText= "") { form.submit() }
       
       
}

 -->
</script>



</head>
<body>
<div id="container">
  <!--Top menu starts here-->
  <!--Top menu ends here-->
  <!--image slider starts here-->
  <div id="middle">
      <div id="middleMain">
<div id="reg">
<div style="width:80%; color:#042976;"><h2>Joining Form</h2></div>
<div style="width:80%; color:#f55b07;"><sup>*</sup> Signed fields are mandatery, Please fill each and every below field with correct information</div>

<!--onsubmit="return ValidateForm(this)" -->

<?php echo $error_list; ?>

<center>
 <form name="form1" id="form1" method="post" action="signup.php" >
    <table width="auto;" cellpadding="2" cellspacing="3" class="style1">
        <tr>
            <td width="180px;">User Name<sup style="color:#f55b07;">*</sup></td>
          <td>
             <input id="user_name" name="user_name" type="text" value="" /><br /><br />
              <!-- <input type="button" onclick="validate();" value="Check Availability" />
              <div id="msg"></div> -->
              </td>
        </tr>
        <tr>
            <td>Your Complete Name <sup style="color:#f55b07;">*</sup><br />
          <!--<span style="font-size:x-small; color:#000000;"> (This must be your name, applicable in your bank account) </span> -->
                </td>
            <td>
                <input id="full_name" name="full_name" type="text" />
                </td>
        </tr>
        <tr>
            <td> Email <sup style="color:#f55b07;">*</sup>
                </td>
            <td>
                <input id="user_email" name="user_email" type="text" />
                </td>
        </tr>
        <tr>
            <td> Password <sup style="color:#f55b07;">*</sup>
                </td>
            <td>
                <input id="user_pwd" name="user_pwd" type="password" />
            </td>
        </tr>
        <tr>
            <td> Confirm Password <sup style="color:#f55b07;">*</sup>
                </td>
            <td>
               <input id="user_pwd1" name="user_pwd1" type="password" /><br />
          <span style="font-size:x-small; color:#000000;"> (Your Password must be 5 Characters long) </span>
                </td>
        </tr>
         <tr>
            <td> Address <br />
                </td>
            <td>
                <input id="address" name="address" type="text" maxlength="100" />
                </td>
        </tr>
        <tr>
            <td> Phone No. <br /><span style="font-size:x-small; color:#000000;"> (You may mention it later) </span>
                </td>
            <td>
                <input id="phone_no" name="phone_no" type="text" />
                </td>
        </tr>
        <tr>    <td> City </td> <td> <input id="city" name="city" type="text" /> </td>
        </tr>
        <tr>
            <td> State </td>
            <td> <select id="state" name="state">
            <option selected="selected" value="--Select--">--State--</option>
            <option value="Andhra Pradesh">Andhra Pradesh</option>
            <option value="Arunachal Pradesh">Arunachal Pradesh</option>
            <option value="Assam">Assam</option>
            <option value="Bihar">Bihar</option>
            <option value="Chhattisgarh">Chhattisgarh</option>
            <option value="Goa">Goa</option>
            <option value="Gujarat">Gujarat</option>
            <option value="Haryana">Haryana</option>
            <option value="Himachal Pradesh">Himachal Pradesh</option>
            <option value="Jammu and Kashmir">    Jammu and Kashmir</option>
            <option value="Jharkhand">Jharkhand</option>
            <option value="Karnataka">Karnataka</option>
            <option value="Kerala">Kerala</option>
            <option value="Madhya Pradesh">Madhya Pradesh</option>
            <option value="Maharashtra">Maharashtra</option>
            <option value="Manipur">Manipur</option>
            <option value="Meghalaya">Meghalaya</option>
            <option value="Mizoram">Mizoram</option>
            <option value="Nagaland">Nagaland</option>
            <option value="Orissa">Orissa</option>
            <option value="Punjab">Punjab</option>
            <option value="Rajasthan">Rajasthan</option>
            <option value="Sikkim">Sikkim</option>
            <option value="Tamil Nadu">Tamil Nadu</option>
            <option value="Tripura">Tripura</option>
            <option value="Uttaranchal">Uttaranchal</option>
            <option value="Uttar Pradesh">Uttar Pradesh</option>
            <option value="West Bengal">West Bengal</option>
            </select> </td>
        </tr>
        <tr>
            <td>&nbsp;
                </td>
            <td>
            <input name="ck_newsletter" type="checkbox" value="yes" checked align="absmiddle" />I want to receive newsletter. <br />
            <input name="ck_terms_cond" type="checkbox" value="Accept" checked align="absmiddle" />I Accept Terms and Conditions. <br />
                </td>
        </tr>
        <tr>
            <td>
                <input id="submitted" name="submitted" type="hidden" />
                </td>
            <td>
              <input name="submit" type="submit" value="submit"/>
                </td>
        </tr>
    </table>
    </form>
</center>
</div>
    </div>
  </div>
</div>
</body>
</html>

1 comment:

  1. https://gajabwap.blogspot.com/2018/04/how-to-create-php-forgate-password.html?_e_pi_=7%2CPAGE_ID10%2C9071402477

    ReplyDelete