• php forms
  • conkurent
  • real estate

Simple php login script

Simple php login script realizes mechanism of authentication users when they try to access a website. Very often user login and password have to be saved during the whole session. For this purpose Simple php login script must have access to protected area of website during the whole session. Data during session is saved in cookies. To launch session you can use session_start() function.

Simple php login script, shown below, uses HTML form for data input, and extraction of existing logins and passwords is made from MySQL database. Access to page.php is closed, but with the help of authorization we can receive access to it:

<?
session_start(); //initialize session mechanism

if(!isset($_POST['ok'])) {
// if the form is not completed, display it
    echo"
    <html>
    <head>
    <title>Authorization page</title>
    </head>
    <body>
    <table width='100%' height='100%'>
    <form method='POST' action='login.php'>
    <tr><td align=center>
    <table>
    <tr><td>
    <table>
    <tr><td>Login:</td><td><input type='text'
        name='login' size='15'></td></tr>
    <tr><td>Password:</td><td><input
        type='password' name='pass' size='15'></td></tr>
    </table>
    </td></tr>
    <tr><td align=center><input type='submit' name='ok'
        value='Enter'></td></tr>
    </table>
    </td></tr>
    </form>
    </table>
    </body>
    </html>
    ";
}
else{    
    //supposed that user data
    //is saved in database, in users table, that includes id, login, pass fields

    $db=mysql_connect('host', 'login', 'password');
    mysql_select_db('db_name', $db);
    
    //check if there is a user with such login and password
    $res=mysql_query("SELECT * FROM users WHERE login='".$_POST['login']."'
        AND pass='".$_POST['pass']."'", $db);
    if(mysql_num_rows($res)!=1){    //such user doesn’t exist
        echo "Incorrect login and password";
    }
    else{    //user is found
        $_SESSION['login']=$_POST['login'];    //set login & pass
        $_SESSION['pass']=$_POST['pass'];
        Header("Location: protected.php");    // redirect him to protected.php
    }
    
    mysql_close();
}
?>

This sample of simple php login script describes easy way of access control with convenient method of saving user data in database. Here is another simple php login script that saves all inputted login and password in arrays $_POST and $_GET (subject to type of request to server):

<html>
    <head><title>Welcome to PHP!</title></head>
    <body>
<?php
    if ($login = $_POST["login"]) {
        echo "<b>Привет, login!</b>";
    }
?>
<form method="post">
  your login:&nbsp;&nbsp;<input type="text" name="login"/><br/>
  <input type="submit" value="Enter"/>
</form>
</body>
</html>

Password protection system lite

Building site security

User management software

Secure PHP login script