User registration form in php with session and user private account in website

  User registration form in php

  • First step is to design a registration form.
  • Here i am design a registration form which contain fields like name,address,gender etc.
  • Open your text editor and type following code.
  • Code for designing a registration:-
     <form name="f1" method="post" action="ragiinsert.php"  enctype="multipart/form-data">
     <table align="center">
               <tr><td>Name</td>
              <td><input type="text" name="txtname" /></td></tr>
              <tr><td>Address</td>
              <td><textarea name="add" cols="" rows=""></textarea></td></tr>
              <tr>
             <td>Passportsize Photo</td>
                <td><input type="file" name="file" id="file" /></td>
             </tr>
             <tr>
             <td>Contact Number</td>
                <td><input type="text" name="txtnum" /></td>
             </tr>                                      <tr>
             <td>EmailID</td>
                <td><input type="text" name="txtmail" /></td>
             </tr>          
             <tr>
             <td>Gender</td>
             <td>Male<input name="gen" type="radio" value="male" />&nbsp;Fmale<input name="gen" type="radio" value="male" /> </td>
             </tr>
             <tr>
             <td>Cast</td>
                <td><select name="cast">
                <option>Select</option>              
                <option>Open</option>
                <option>SCST</option>
                <option>SCBC</option>
                <option>OBC</option>                              
                </select></td>
             </tr>                   
               <tr>
             <td>Username</td>
                <td><input type="text" name="txtuser" /></td>
             </tr>
               <tr>
             <td>Password</td>
                <td><input type="password" name="txtpass" /></td>
             </tr>
             <tr><td colspan="2" align="center">           <input type="submit" name="sbt" value="Next" /></td></tr>           
         </table>
         </form>

  •   Save this file as index.php
  •   When user click on Submit button his/her entered values will post to another page.
  •   Here we post the values of user to ragiinsert.php.
  •   Now next step is  create a new database called mydb.
  •   After that create a new table into mydb called registrationdetail.
  •  In registrationdetail table we have 10 fields like loginid,name,username,password etc.
  •  Now we are going to write a code for inserting a user's value to database.
  •  This is the simple code for inserting a value to database.
  •  write following code:
<?php


      //When user click on submit after fill up his/her all detail in registration form hi/her value would be post here.
    $name=$_POST['txtname'];   
    $add=$_POST['add'];
    $cont=$_POST['txtnum'];
    $email=$_POST['txtmail'];
    $gen=$_POST['gen'];   
    $cast=$_POST['cast'];
    $quli=$_POST['quli'];
    $pre=$_POST['pre'];
    $user=$_POST['txtuser'];
    $passw=$_POST['txtpass']; 
   
            $con=mysql_connect("localhost","root","");
            mysql_select_db("adp");
           
            $qu1="select max(loginid)+1 as maxno from ragistartiondetail";
            $res1=mysql_query($qu1) or(mysql_error());
            $row1=mysql_fetch_array($res1);
            $maxid=$row1['maxno'];       
$qu="insert into ragistartiondetail values($maxid,'$name','$add',$cont,'$sr','$email','$gen','$cast','$user','$passw')";   
    $res=mysql_query($qu);
?>
  • Save this file as ragiinsert.php. 
  • After that our next task is to create a login form.
  • Code for design of login form.
        <?php
       
         echo '<form name="f1" method="post" action="loginchk.php'">
         <table align="center">
         <tr>               
             <td>Username</td>
                <td><input type="text" name="txtuser" /></td>
             </tr>
         <tr>
         <td>Password</td>
         <td><input type="password" name="txtpass" /></td>
         </tr>
         <tr><td colspan="2" align="center"><input type="submit" name="sbt" value="Submit" /></td></tr>
         </table>      
     </form>';
     ?>    










  • Save as login.php
  • After entering usrname and passowrd user click on submit button the entered data will be posted on loginchk.php 
  • Where we validate the database for correct username and password .
  • Here we are going to check database for username and password if username and password is    correct then it should access his account if incorrect it will return false.
  • Code for validation for correct username and password:-
 <?php
                    session_start();
                    $con=mysql_connect("localhost","root","");
                    mysql_select_db("adp");
                        if(isset($_POST['txtuser']) && isset($_POST['txtpass']))
                        {
                            $qu="select * from ragistartion where username='".$_POST['txtuser']."' and pass='".$_POST['txtpass']."'" ;
                            $res=mysql_query($qu) or die(mysql_error());
                            if($row=mysql_fetch_array($res))
                                {
                                  
                                     $_SESSION['txtuser']=$_POST['txtuser'];    
                                      header('Location:Welcome.php'');
                                }
                             else 
                               {
                                      echo('incorrect username of password');
                                }
                      }
  • Create a one file called welcome.php
  • username and password is correct the user enter into the welcome.php
  • In Welcome.php it contain code for welcome message and logout button and link to user account.
  • Code for welcome.php
  •  

<?php
     session_start();
     if(!isset($_SESSION['txtuser']))
     {
         header("Location:index.php");
     }

?>
<?php
<a href="my_account.php">My account</a>   
<a href="process.php?do=signout">Signout</a>
?>
  •     Our welcome.php page look like.





  • After that we have to create a one more file called myaccount.php  
  • In myaccount.php we will fetch all the data of particular user.
  • Code for myaccount.php
<?php
     session_start();
     if(!isset($_SESSION['txtuser']))
     {
         header("Location:index.php");
     }
?>
  <?php                  
         if(isset($_SESSION['txtuser']))
         {
               $con=mysql_connect("localhost","root","");
                     mysql_select_db("adp");
             $id=$_SESSION['txtuser'];
             $qu="select * from ragistartiondetail where username='$id'";
             $res=mysql_query($qu);
                echo '<tr><td>Name</td><td>Address</td><td>Contact</td><td>Email</td><td>Gender</td><td>Cast</td>';
             while($row=mysql_fetch_array($res))
             {
                echo '<tr><td>'.$row['username'].'</td>';               
                echo '<td>'.$row['password'].'</td>';           
                echo '<td>'.$row['email'].'</td>';                               
                echo '<td>'.$row['contact'].'</td>';                               
                echo '<td>'.$row['city'].'</td>';                                               
                echo '<td>'.$row['contact'].'</td>';                               

             }            
         }
echo '</table><br />';
?>
  • When user click on Myaccount it will look like 


  •  Our Last file is Logout.php
  • Code for Logout.php
<?php          


                 session_start();
                 unset($_SESSION['txtuser']);
                 header("Location:index.php");
?>
  • Here our tutorial has been finished.



7 Comments

  1. Awesome! Blut please rewrite the code so that we can use it in PHP5.5: www.php.net/manual/en/function.mysql-query.php
    Remeber to enable the mysqli and pdo extensions inside your php.ini configuration file.

    ReplyDelete
  2. Asking questions are truly pleasant thing if you are not
    understanding something entirely, except this article provides
    nice understanding even.

    my web-site - Internet Money Path

    ReplyDelete
  3. I am extremely impressed with your writing skills and also with the layout on your blog.
    Is this a paid theme or did you modify it yourself?
    Anyway keep up the nice quality writing, it's rare
    to see a great blog like this one these days.

    Feel free to visit my page http://internetmoneypathsite.com

    ReplyDelete
  4. I blog often and I seriously appreciate your information. This great article has really peaked my interest.

    I am going to take a note of your site and keep checking for new details about
    once per week. I subscribed to your Feed as well.



    Here is my blog post ... Internet Money Path

    ReplyDelete
Previous Post Next Post