• Sunday, July 20, 2014

    PHP, MYSQL Login Page

    Making login page with PHP and MYSQL

    1. Optional (if u dont create any table for login purpose) Create a table "users" with fields "u_id","u_name","u_pass", MYSQL query:

      create table `users`(


      u_name VARCHAR(30),

      u_pass VARCHAR(30),

      PRIMARY KEY(u_id)); 


    2. Now make a login page and write the following code:

      session_start(); //Starting the session
      if(@$_SESSION['login'] == 1) //Checking if user is already login or not.
      header("Location: /index.php"); //Already login,, send the user to homepage.


      if(isset($_POST['submit']=='Login')) //Checking if the form is submit or not.
      $con = mysql_connect("mysql_host","mysql_username","mysql_password") or die("Connect Error");

      mysql_select_db("mysql_db_name")  or die("DB Select error");
      [email protected]$_POST['u_name'];

      [email protected]$_POST['u_pass']; 

      $query=mysql_query("SELECT * FROM users WHERE u_name='$u_name' AND u_pass='$u_pass' LIMIT 1") or die("Query Error");

      if(mysql_num_rows($query) > 0)


      \\Login Successful;



      header("Location: /index.php"); //Redirect to home page.


      \\Incorrect username or password.

      $msg="<b>Incorrect username or password<b><br />" ;

      <!DOCTYPE html>
      <html><title>Login Page</title>
      if(isset($msg))echo $msg;
      <form name="f1" action="#" method="post">
      <input type="text" name="u_name" placeholder="Enter username here" /><br />
      <input type="password" name="u_pass" placeholder="Enter password here" /><br />
      <input type="submit" name="submit" value="Login" /><br />

    3. Thats it. To logout use:
      header("Location: /index.php");


    Syed Masoom Rizvi

    Author & Editor

    Hi, This is Syed Masoom Rizvi, who is continue exploring the world of programming ,programming and programming... :)


    Post a Comment