Social Icons

Friday, November 30, 2012

Make Login form using PHP MySql

Create Login Page Using PHP MySql

For most of the websites, it is a common requirement to have registration for users and accordingly login for them. Only users who have login account for the website like username and password, they can access most of the sections of the website. While unauthorized users (who doesn't have account for the website) can't. Herein this tutorial, we will read how to make a login page for any website. To understand this tutorial completely, I assume that you have basic knowledge of PHP, MySql. Anyway, I will try to explain each and every line of code in the tutorial. So, here we go.

Let us make a rough idea what we have to do. There will be a username column and a password column and a Login Button in the webpage. User will enter his/her username and password there and will click on Login button. It will check from database either these details match or not. If matches it will give success message else failure. So, firstly we need a database in which there will be a table and two columns as 'username' and 'password'. We will store email in username and any string in password.

Open your mysql with localhost/phpmyadmin. You can create a database there directly using GUI or by commands also. I provide you the query to write there.

Step 1 – Create a Database

Run this query in mysql:

CREATE DATABASE tutorials

This query will create a database named 'tutorials' in your mysql database. Here, 'tutorials' is the database name. You can replace it with your own name whatever you want.

Step 2 – Create a table (having columns username and password) in the above database


Now, we have to create a table in the database 'tutorials'. Now, select the database 'tutorials' in your  localhost/phpmyadmin. There run this query:


create table user (username varchar(50), password varchar(50))

This query will create a table named 'user' in the database 'tutorials'. It will have two columns as 'username' and 'password'. Varchar above in the query is the data type for username and password both. Both of them have size of 50 characters max. We could take char as data type also but then it would allocate 50 bytes for each. While varchar is flexible.

Now as no data is there in our database so we have to insert some data into it. Lets insert some username and password values into table 'user'. Run this query:

INSERT INTO user (username,password) VALUES ('prem@prembaranwal.com', '123')

It will insert a row having username value as prem@prembaranwal.com and password as 123.

Lets insert one more value similarly. Run this query:

INSERT INTO user (username,password) VALUES ('talkmeonprem@gmail.com', 'prem')

Now enough for the database part. Lets move to the front-end PHP and HTML part.

Lets create a login page. There will be a two textboxes for username and password where user will enter its username and password values. Apart from this there will be a submit login button. On clicking this button it will validate username and password as it is correct or not and display a message 'success' if correct and 'failure' if wrong.

Create a file named login.php

Code for login.php
            
            <html>
            <head>
            <title>Login Page in PHP</title>
            </head>
            <body>
            <form name=”myForm” action=”loginconnect.php” method=”POST”>
            <table>
            <tr>
                   <td>Username: </td>
                   <td><input type=”text” name=”username” id=”username” value=””/></td>
            </tr>
            <tr>
                   <td>Password: </td>
                   <td><input type=”password” name=”pwd” id=”pwd” value=””/></td>
            </tr>
            <tr></tr>
            <tr>
                   <td><input type=”submit” name=”login” value=”Login”/></td>
            </tr>
            </table>
            </form>
            </body>
            </html>


It will create a login page having username, password fields and submit button.

Now, user will enter its details in the textboxes and click on Login button. After clicking we have to check either the username and password is correct for the user or not. After clicking on Login button it will go to loginconnect.php page as it is written in form action in the above code ( see in login.php)

Lets write the code for loginconnect.php

Code for loginconnect.php

            <?php

            $user = $_POST['username'];  // store username entered by user

            $pwd = $_POST['pwd'];         // store password entered by user

            $con = mysql_connect("localhost","root","");  //establish connection to mysql database

            mysql_select_db("tutorials", $con);  //selects database tutorials
            $result = mysql_query("SELECT * FROM user WHERE username = '$user' and password='$pwd'”); // Query to check username and password is correct or not.

            $count = mysql_num_rows($result);  // Return number of rows from the above query

            if($count > 0)  // If it equals to 1 means username and password is correct
            {
                        echo “Successfully logged in”;
            }
            else
            {
                        echo “Either username or password is incorrect”;
            }

The description for the above code is given there in the comment itself for each line of code. I think these descriptions are enough to understand. Now run the login page.

Ø  Open your browser and type localhost/login.php in the url.

Ø  It will give you the login page containing username and password fields and login button.

Ø  Enter the details which we saved in our database.

Ø  In username, enter prem@prembaranwal.com and in password enter 123 and click on login button. It will give you successfully logged in message. Try with some wrong data and it will give you error message.

So, with this making basic login page finishes. Now, lets make it more better. We haven't put any validation here in the login page. Suppose if user doesn't enter anything in username and password and directly click on login button. In that case we can validate the form and ask user to enter those fields. Lets put validation in the form. Some modification is required in login.php file only.

Code for login.php with validation

            <html>
            <head>
            <title>Login Page in PHP</title>
            <script language=”javascript” type=”text/javascript”>
            function validateForm()
            {
                        var a = document.forms[“myForm”][“username”].value;
                        var b = document.forms[“myForm”][“pwd”].value;
                        if(a == null || a == “”)
                        {
                                    alert(“Enter Username”);
                                    return false;
                        }
                        if(b == null || b == “”)
                        {
                                    alert(“Enter Password”);
                                    return false;
                        }
            }
            </script>
            </head>
            <body>
            <form name=”myForm” action=”loginconnect.php” method=”POST”>
            <table>
            <tr>
                   <td>Username: </td>
                   <td><input type=”text” name=”username” id=”username” value=””/></td>
            </tr>
            <tr>
                   <td>Password: </td>
                   <td><input type=”password” name=”pwd” id=”pwd” value=””/></td>
            </tr>
            <tr></tr>
            <tr>
                   <td><input type=”submit” name=”login” value=”Login” onclick=”return validateForm()”/></td>
            </tr>
            </table>
            </form>
            </body>
            </html>


The above code will validate if the user has not entered username or password field and clicks on Login button directly. This results in a complete working login form in PHP.

Vedic Maths

Vedic Mathematics

 In simple words, we can say that vedic mathematics is just a unique method to make all of mathematical calculations faster. This helps in solving mathematical problems easily and in effective time. For example : Suppose you go to market to purchase something like books, clothes etc. The shopkeeper gives you some discount (say 33.5%). In this case, you find difficulty in calculating the percentage discount amount and whatever the shopkeeper ask, you pay it to him. But if you know vedic maths, then it will be easier to calculate them. It is also beneficial in exams for students. Today, time matters the most in competitive exams. Most person know the method to solve the question. But, there comes a matter of time who can solve faster. With vedic mathematics, you will be able to make calculations much faster without the need of calculator and the result will be solving problems in  lesser time as compared to others. Here, some of its important parts are covered. 

Vedic Maths Tutorials

1. Multiplication
             Multiplication is the part of algebraic maths where users find most difficulty to calculate it. But, it becomes very easier if you do it from vedic mathematics. Lets do it with vedic maths. You will surely enjoy it.

  i) Multiplication between 1 digit numbers
       Lets do multiplication between two 1-digit numbers. Suppose we have to multiply 9x6.
         Now 9 is 1 below to 10 and 6 is 4 below to 10. Write it like this,
          9        1
               X
          6        4 
          5        4   is the answer

Lets see how it came. For the left hand side digit(5), subtract the numbers diagonally from any side i.e 9 - 4 = 6 - 1 = 5. For the right hand sided digit, multiply the right hand sided numbers i.e 1x4 = 4. Now combine these, 54 will be the answer.

  ii) Multiplication of numbers near 100
           Now, lets do multiplication of two digit numbers close to 100. Suppose you have to multiply the numbers as 86x97. Again 86 is 14 below 100 and 97 is 3 below 100. Write it like this,
          97      3
               X
          86      14
          83      42  is the answer.

          Lets see how it came. For getting the left hand side number(83), subtract the numbers diagonally i.e 97 - 14 = 86 - 3 = 83. For the left hand sided number(14), multiply the last right hand side numbers i.e 3x14 = 42. Combine these, 8342 will be the answer.

  iii) Multiply numbers just above 100.
          Now see for multiplication between 3-digit numbers just over 100. Suppose you have to multiply 106x104. Lets do it with trick of vedic maths.
          106x104 = 11024.

          Lets see how it happened easily. Actually the answer is in two parts 110 and 24. First part 110 comes from (106+4) or (104+6). Second part 24 comes from multiplication between unit's place dit numbers i.e 6x4 = 24. So, 11024 is the answer.

You have seen how it is easy to do the multiplication with vedic mathematics. No need to use calculator or pick copy pen for it. Now, you can do it in your mind itself. So, just do some more practices. More you will practice, more you will remember these tricks.

2. Square of a number
    In this tutorial we will learn how to calculate square of a number very easily with vedic maths. Just in mind, you can do it.   
   i) Square of a two digit number ending with 5
         Suppose we have a number as 75. We have to find out the square of this number. For this, we have to use copy pen for it and multiply 75x75. It will take too longer. But from now, you will be able to do it in your mind. No need to use copy pen. Lets see :        75x75 = 5625 is the answer.

             Now lets see how it came very easily. Actually the answer is in two parts(56 & 25). If you have to square any number ending with 5 then write 25 in last. Its the right part of the answer. For the left part(56), just increment the ten's place digit by 1 and multiply with it i.e 7x(7+1) = 7x8 = 56. Combine these results 5625 is the answer. Similarly,85x85 = 7225  and rest you can try yourself.

   ii) Square of any two digit number
       Herein this method, we will learn to calculate square of any two digit number. At the first time, it is possible that you feel it something typical. But after practicing it, you will find it too easier and also it will save your much time. So, lets start : Suppose we have to square the number of 32.
     Square of 32  = 32x32 = 1024. is the answer.

   Lets see how the result came. Here start with the unit's place digit i.e 2. Square it. It will give you 4 (2x2). This the     unit's place digit of the answer. Now proceed, multiply the unit's place digit(2) and ten's place digit(3) and multiply   the result by 2(here, 2 is not the unit's place digit... It is always fix to multiply by 2) i.e(3x2x2) and the result will give 12. Write its unit's place digit before 4(calculate above as 2x2) and keep in mind the carry of 12 i.e 1. Now, we get the last two digit of the answer i.e, 24. Again proceed for the complete answer, Now, square the ten's place digit i.e 3x3 = 9. And we had a carry of 1 earlier. So, add this to 9. It will give 10. Write 10 before 24. Combining all these, 1024 will be the answer.

Note:- More vedic mathematics tutorials will be provided here very soon...Hope, you enjoyed it...

Total Pageviews