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


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

  1. backlinkpros

    backlinkpros Regular Member

    Mar 29, 2013
    Likes Received:
    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...

    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
    <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' />
    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
            //if they exist
            $_SESSION['user'] = $_POST['username']; //create a session
            header('location:home.php'); //redirect to another page
    Last edited: Oct 29, 2013