Full width home advertisement

Post Page Advertisement [Top]

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_id INT AUTO_INCREMENT,

    u_name VARCHAR(30),

    u_pass VARCHAR(30),

    PRIMARY KEY(u_id)); 



     

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

    <?php
    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.

    exit(""); 



    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");
    $u_name=@$_POST['u_name'];

    $u_pass=@$_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;

    $_SESSION['login']=1;

    $_SESSION['u_name']=$u_name;

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

    }else{

    \\Incorrect username or password.

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

    }
    }
    ?>
    <!DOCTYPE html>
    <html><title>Login Page</title>
    <body>
    <?php
    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 />
    </body></html>
     












  3. Thats it. To logout use:
    <?php
    session_start();
    session_destroy();
    header("Location: /index.php");
    ?>

     

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib