Friday, December 24, 2010

PHP Registration and Login script


Register Login ScriptThe following tutorial will teach you how to add a login/register script to your site. This tutorial requires 6 files, and PHP with MySQL installed.

Simply upload all files to any directory, open connect.php and set the connection variables. After that simply run install.php once, and when it tells you it is installed, delete install.php

You can download all the files here. Install [install.php]

<?php

    require_once("connect.php");
    
    $query = mysql_query("
CREATE TABLE 'members' (
'id' MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT ,
'username' VARCHAR( 50 ) NOT NULL ,
'firstname' VARCHAR( 50 ) NOT NULL ,
'lastname' VARCHAR( 50 ) NOT NULL ,
'password' VARCHAR( 50 ) NOT NULL ,
'date' VARCHAR( 50 ) NOT NULL ,
'ip' VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( 'id' ) ,
UNIQUE (
'username'
)
)");

echo "Installed! Please delete this file.";
    

?>

Connect [connect.php]

<?php

    // MySQL connect information.
    $c_username = "database_username";
    $c_password = "database_password";
    $c_host = "localhost";
    $c_database = "database_name";

    // Connect.
    $connection = mysql_connect($c_host, $c_username, $c_password)
    or die ("It seems this site's database isn't responding.");

    mysql_select_db($c_database)
    or die ("It seems this site's database isn't responding.");

?>

Register [register.php]

<?php

// Check if he wants to register:
if (!empty($_POST[username]))
undefined

?>

<html>
    <head>
        <title>Register</title>
    </head>
    <body>
        <form action="register.php" method="post">
            <table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
                <tr>
                    <td width="100%">Registration</td>
                </tr>
                <tr>
                    <td width="100%"><label>Desired Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>First Name: <input type="text" name="firstname" size="25" value="<? echo $_POST[firstname]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Last Name: <input type="text" name="lastname" size="25" value="<? echo $_POST[lastname]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Password: <input type="password" name="password" size="25" value="<? echo $_POST[password]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Verify Password: <input type="password" name="password2" size="25" value=""></label></td>
                </tr>
                <tr>
                    <td width="100%"><input type="submit" value="Register!"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

Login [login.php]

<?php
session_start();
// Check if he wants to login:
if (!empty($_POST[username]))
undefined

?>

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <form action="login.php" method="post">
            <table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
                <tr>
                    <td width="100%">Login</td>
                </tr>
                <tr>
                    <td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
                </tr>
                <tr>
                    <td width="100%"><input type="submit" value="Login!"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

Logout [logout.php]

<?php
    $_SESSION[username] = "";
?>

Want to limit certain files only to members, use the below script. Members [members.php]

<?php
session_start();

// Check his status.
if (!empty($_SESSION[username])) // he got it.
undefined
else // bad info.
undefined

?>

No comments: