1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Login

Discussion in 'PHP & Perl' started by backlinkpros, Oct 29, 2013.

  1. backlinkpros

    backlinkpros Regular Member

    Joined:
    Mar 29, 2013
    Messages:
    250
    Likes Received:
    32
    Bored out ma mind so just made this quickly, hope it has a use for someone. I added lots of comments to help to understand it and might make a register page if there is some demand...

    Code:
    <?php
    session_start(); //start a session
    
    
    //mysql connect
    mysql_connect("", "", "") or die (mysql_error());
    
    
    //select database
    mysql_select_db("") or die (mysql_error());
    
    
    //display login form
    echo"
    <form action='login.php' method='POST' />
    Username: <input type='text' name='username' /> <br />
    Password: <input type='password' name='password' /> <br />
    <input type='submit' name='login' value='Login' />
    </form> 
    ";
    
    
    if (isset($_POST['login'])) { //check if the user has clicked the "Login" button
    
    
        //store user input in variables and secure them against sql injections
        $username = mysql_real_escape_string($_POST['username']);
        $password = md5($_POST['password']);
    
    
        //check if user exists in the table
        $query = mysql_query("SELECT * FROM tablename WHERE username = '$username' AND password = '$password' ") or die (mysql_error());
    
    
        if (!(mysql_num_rows($query)) {
            echo"You don't exist."; //if they dont display a error
        }else{
            //if they exist
            $_SESSION['user'] = $_POST['username']; //create a session
            header('location:home.php'); //redirect to another page
        }
    
    
    
    
    }
    
    
    ?>
    
     
    Last edited: Oct 29, 2013