www.webdeveloper.com
Results 1 to 2 of 2

Thread: Basic Login Script

  1. #1
    Join Date
    Oct 2013
    Posts
    6

    Basic Login Script

    I'm new here and trying to build some reputation up.

    Here is a basic login script I programmed with lots of comments to help the people who are new to programming to understand it better.

    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
        }
    
    
    
    
    }
    
    
    ?>
    *untested but should be no errrors

  2. #2
    Join Date
    Feb 2013
    Posts
    78
    Please don't take this as an offense, but if you want to show them a simple login php script at least use mysqli or PDO.

    Here's a login & registration script that I have tested and I know contains no errors. The link : http://www.pepster.com/general.php?category=PHP

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles