Create Account
Sign In

Members Area System in php mysql - members users system area log sign


Members Area System in php mysql - members users system area log sign Members Area System in php mysql - members users system area log sign The 22/11/2010 at 22:08:49
Rating: 4.5/5
Keywords: members area system php mysql users members system area log in log out sign up free members area system script php mysql sessions tutorial

Hi,
This script will lets you create a Member Area easily.

Our Member Area have 6 pages.
  • Sign up
  • Log in/Log out
  • Home
  • Editing a profile
  • List of all users
  • Profile of an user


This is a demonstration of the Members Area:
Demonstration
You can also download the Members Area as a .zip or .rar archive:
Image
Download the .ZIP archive

Image
Download the .RAR archive


Let start by the data base, we are going to create the table "users".
--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` bigint(20) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `avatar` text NOT NULL,
  `signup_date` int(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Download
This is how table "users" looks:
Image

Sign up

This page lets the user to sign up, he have to fill a form by entering his username, password, email and his avatar.
sign_up.php
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
        <title>Sign up</title>
    </head>
    <body>
        <div class="header">
                <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
            </div>
<?php
//We check if the form has been sent
if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'], $_POST['avatar']) and $_POST['username']!='')
{
        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
                $_POST['username'] = stripslashes($_POST['username']);
                $_POST['password'] = stripslashes($_POST['password']);
                $_POST['passverif'] = stripslashes($_POST['passverif']);
                $_POST['email'] = stripslashes($_POST['email']);
                $_POST['avatar'] = stripslashes($_POST['avatar']);
        }
        //We check if the two passwords are identical
        if($_POST['password']==$_POST['passverif'])
        {
                //We check if the password has 6 or more characters
                if(strlen($_POST['password'])>=6)
                {
                        //We check if the email form is valid
                        if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',$_POST['email']))
                        {
                                //We protect the variables
                                $username = mysql_real_escape_string($_POST['username']);
                                $password = mysql_real_escape_string($_POST['password']);
                                $email = mysql_real_escape_string($_POST['email']);
                                $avatar = mysql_real_escape_string($_POST['avatar']);
                                //We check if there is no other user using the same username
                                $dn = mysql_num_rows(mysql_query('select id from users where username="'.$username.'"'));
                                if($dn==0)
                                {
                                        //We count the number of users to give an ID to this one
                                        $dn2 = mysql_num_rows(mysql_query('select id from users'));
                                        $id = $dn2+1;
                                        //We save the informations to the databse
                                        if(mysql_query('insert into users(id, username, password, email, avatar, signup_date) values ('.$id.', "'.$username.'", "'.$password.'", "'.$email.'", "'.$avatar.'", "'.time().'")'))
                                        {
                                                //We dont display the form
                                                $form = false;
?>
<div class="message">You have successfuly been signed up. You can log in.<br />
<a href="connexion.php">Log in</a></div>
<?php
                                        }
                                        else
                                        {
                                                //Otherwise, we say that an error occured
                                                $form = true;
                                                $message = 'An error occurred while signing up.';
                                        }
                                }
                                else
                                {
                                        //Otherwise, we say the username is not available
                                        $form = true;
                                        $message = 'The username you want to use is not available, please choose another one.';
                                }
                        }
                        else
                        {
                                //Otherwise, we say the email is not valid
                                $form = true;
                                $message = 'The email you entered is not valid.';
                        }
                }
                else
                {
                        //Otherwise, we say the password is too short
                        $form = true;
                        $message = 'Your password must contain at least 6 characters.';
                }
        }
        else
        {
                //Otherwise, we say the passwords are not identical
                $form = true;
                $message = 'The passwords you entered are not identical.';
        }
}
else
{
        $form = true;
}
if($form)
{
        //We display a message if necessary
        if(isset($message))
        {
                echo '<div class="message">'.$message.'</div>';
        }
        //We display the form
?>
<div class="content">
    <form action="sign_up.php" method="post">
        Please fill the following form to sign up:<br />
        <div class="center">
            <label for="username">Username</label><input type="text" name="username" value="<?php if(isset($_POST['username'])){echo htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
            <label for="password">Password<span class="small">(6 characters min.)</span></label><input type="password" name="password" /><br />
            <label for="passverif">Password<span class="small">(verification)</span></label><input type="password" name="passverif" /><br />
            <label for="email">Email</label><input type="text" name="email" value="<?php if(isset($_POST['email'])){echo htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
            <label for="avatar">Avatar<span class="small">(optional)</span></label><input type="text" name="avatar" value="<?php if(isset($_POST['avatar'])){echo htmlentities($_POST['avatar'], ENT_QUOTES, 'UTF-8');} ?>" /><br />
            <input type="submit" value="Sign up" />
                </div>
    </form>
</div>
<?php
}
?>
                <div class="foot"><a href="<?php echo $url_home; ?>">Go Home</a> - <a href="http://www.webestools.com/">Webestools</a></div>
        </body>
</html>

Log in/Log out

If the user is logged, we log him out, otherwise, we display a form, if the combination is right we log him.
read_pm.php
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
        <title>Connexion</title>
    </head>
    <body>
        <div class="header">
                <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
            </div>
<?php
//If the user is logged, we log him out
if(isset($_SESSION['username']))
{
        //We log him out by deleting the username and userid sessions
        unset($_SESSION['username'], $_SESSION['userid']);
?>
<div class="message">You have successfuly been loged out.<br />
<a href="<?php echo $url_home; ?>">Home</a></div>
<?php
}
else
{
        $ousername = '';
        //We check if the form has been sent
        if(isset($_POST['username'], $_POST['password']))
        {
                //We remove slashes depending on the configuration
                if(get_magic_quotes_gpc())
                {
                        $ousername = stripslashes($_POST['username']);
                        $username = mysql_real_escape_string(stripslashes($_POST['username']));
                        $password = stripslashes($_POST['password']);
                }
                else
                {
                        $username = mysql_real_escape_string($_POST['username']);
                        $password = $_POST['password'];
                }
                //We get the password of the user
                $req = mysql_query('select password,id from users where username="'.$username.'"');
                $dn = mysql_fetch_array($req);
                //We compare the submited password and the real one, and we check if the user exists
                if($dn['password']==$password and mysql_num_rows($req)>0)
                {
                        //If the password is good, we dont show the form
                        $form = false;
                        //We save the user name in the session username and the user Id in the session userid
                        $_SESSION['username'] = $_POST['username'];
                        $_SESSION['userid'] = $dn['id'];
?>
<div class="message">You have successfuly been logged. You can access to your member area.<br />
<a href="<?php echo $url_home; ?>">Home</a></div>
<?php
                }
                else
                {
                        //Otherwise, we say the password is incorrect.
                        $form = true;
                        $message = 'The username or password is incorrect.';
                }
        }
        else
        {
                $form = true;
        }
        if($form)
        {
                //We display a message if necessary
        if(isset($message))
        {
                echo '<div class="message">'.$message.'</div>';
        }
        //We display the form
?>
<div class="content">
    <form action="connexion.php" method="post">
        Please type your IDs to log in:<br />
        <div class="center">
            <label for="username">Username</label><input type="text" name="username" id="username" value="<?php echo htmlentities($ousername, ENT_QUOTES, 'UTF-8'); ?>" /><br />
            <label for="password">Password</label><input type="password" name="password" id="password" /><br />
            <input type="submit" value="Log in" />
                </div>
    </form>
</div>
<?php
        }
}
?>
                <div class="foot"><a href="<?php echo $url_home; ?>">Go Home</a> - <a href="http://www.webestools.com/">Webestools</a></div>
        </body>
</html>

Home

The Home page contain links to log in/log out, to sign up, to see the list of the users...
index.php
<?php
include('config.php')
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
        <title>Members Area</title>
    </head>
    <body>
        <div class="header">
                <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
            </div>
        <div class="content">
<?php
//We display a welcome message, if the user is logged, we display it username
?>
Hello<?php if(isset($_SESSION['username'])){echo ' '.htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8');} ?>,<br />
Welcome on our website.<br />
You can <a href="users.php">see the list of users</a>.<br /><br />
<?php
//If the user is logged, we display links to edit his infos, to see his pms and to log out
if(isset($_SESSION['username']))
{
?>
<a href="edit_infos.php">Edit my personnal informations</a><br />
<a href="connexion.php">Logout</a>
<?php
}
else
{
//Otherwise, we display a link to log in and to Sign up
?>
<a href="sign_up.php">Sing up</a><br />
<a href="connexion.php">Log in</a>
<?php
}
?>
                </div>
                <div class="foot"><a href="http://www.webestools.com/">Webestools</a></div>
        </body>
</html>

Editing a profile

We display a form that let the user change his informations like his username, password, email, avatar...
edit_infos.php
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
        <title>Edit my personnal informations</title>
    </head>
    <body>
        <div class="header">
                <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
            </div>
<?php
//We check if the user is logged
if(isset($_SESSION['username']))
{
        //We check if the form has been sent
        if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'], $_POST['avatar']))
        {
                //We remove slashes depending on the configuration
                if(get_magic_quotes_gpc())
                {
                        $_POST['username'] = stripslashes($_POST['username']);
                        $_POST['password'] = stripslashes($_POST['password']);
                        $_POST['passverif'] = stripslashes($_POST['passverif']);
                        $_POST['email'] = stripslashes($_POST['email']);
                        $_POST['avatar'] = stripslashes($_POST['avatar']);
                }
                //We check if the two passwords are identical
                if($_POST['password']==$_POST['passverif'])
                {
                        //We check if the password has 6 or more characters
                        if(strlen($_POST['password'])>=6)
                        {
                                //We check if the email form is valid
                                if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',$_POST['email']))
                                {
                                        //We protect the variables
                                        $username = mysql_real_escape_string($_POST['username']);
                                        $password = mysql_real_escape_string($_POST['password']);
                                        $email = mysql_real_escape_string($_POST['email']);
                                        $avatar = mysql_real_escape_string($_POST['avatar']);
                                        //We check if there is no other user using the same username
                                        $dn = mysql_fetch_array(mysql_query('select count(*) as nb from users where username="'.$pseudo.'"'));
                                        //We check if the username changed and if it is available
                                        if($dn['nb']==0 or $_POST['username']==$_SESSION['username'])
                                        {
                                                //We edit the user informations
                                                if(mysql_query('update users set username="'.$pseudo.'", password="'.$password.'", email="'.$email.'", avatar="'.$avatar.'" where username="'.$_SESSION['username'].'"'))
                                                {
                                                        //We dont display the form
                                                        $form = false;
                                                        //We delete the old sessions so the user need to log again
                                                        unset($_SESSION['username'], $_SESSION['userid']);
?>
<div class="message">Your informations have successfuly been updated. You need to log again.<br />
<a href="connexion.php">Log in</a></div>
<?php
                                                }
                                                else
                                                {
                                                        //Otherwise, we say that an error occured
                                                        $form = true;
                                                        $message = 'An error occurred while updating your informations.';
                                                }
                                        }
                                        else
                                        {
                                                //Otherwise, we say the username is not available
                                                $form = true;
                                                $message = 'The username you want to use is not available, please choose another one.';
                                        }
                                }
                                else
                                {
                                        //Otherwise, we say the email is not valid
                                        $form = true;
                                        $message = 'The email you entered is not valid.';
                                }
                        }
                        else
                        {
                                //Otherwise, we say the password is too short
                                $form = true;
                                $message = 'Your password must contain at least 6 characters.';
                        }
                }
                else
                {
                        //Otherwise, we say the passwords are not identical
                        $form = true;
                        $message = 'The passwords you entered are not identical.';
                }
        }
        else
        {
                $form = true;
        }
        if($form)
        {
                //We display a message if necessary
                if(isset($message))
                {
                        echo '<strong>'.$message.'</strong>';
                }
                //If the form has already been sent, we display the same values
                if(isset($_POST['username'],$_POST['password'],$_POST['email']))
                {
                        $pseudo = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');
                        if($_POST['password']==$_POST['passverif'])
                        {
                                $password = htmlentities($_POST['password'], ENT_QUOTES, 'UTF-8');
                        }
                        else
                        {
                                $password = '';
                        }
                        $email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
                        $avatar = htmlentities($_POST['avatar'], ENT_QUOTES, 'UTF-8');
                }
                else
                {
                        //otherwise, we display the values of the database
                        $dnn = mysql_fetch_array(mysql_query('select username,password,email,avatar from users where username="'.$_SESSION['username'].'"'));
                        $username = htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8');
                        $password = htmlentities($dnn['password'], ENT_QUOTES, 'UTF-8');
                        $email = htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8');
                        $avatar = htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8');
                }
                //We display the form
?>
<div class="content">
    <form action="edit_infos.php" method="post">
        You can edit your informations:<br />
        <div class="center">
            <label for="username">Username</label><input type="text" name="username" id="username" value="<?php echo $username; ?>" /><br />
            <label for="password">Password<span class="small">(6 characters min.)</span></label><input type="password" name="password" id="password" value="<?php echo $password; ?>" /><br />
            <label for="passverif">Password<span class="small">(verification)</span></label><input type="password" name="passverif" id="passverif" value="<?php echo $password; ?>" /><br />
            <label for="email">Email</label><input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br />
            <label for="avatar">Avatar<span class="small">(optional)</span></label><input type="text" name="avatar" id="avatar" value="<?php echo $avatar; ?>" /><br />
            <input type="submit" value="Send" />
        </div>
    </form>
</div>
<?php
        }
}
else
{
?>
<div class="message">To access this page, you must be logged.<br />
<a href="connexion.php">Log in</a></div>
<?php
}
?>
                <div class="foot"><a href="<?php echo $url_home; ?>">Go Home</a> - <a href="http://www.webestools.com/">Webestools</a></div>
        </body>
</html>

List of all users

We display the list of the users in a table.
users.php
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
        <title>List of users</title>
    </head>
    <body>
        <div class="header">
                <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
            </div>
        <div class="content">
This is the list of members:
<table>
    <tr>
        <th>Id</th>
        <th>Username</th>
        <th>Email</th>
    </tr>
<?php
//We get the IDs, usernames and emails of users
$req = mysql_query('select id, username, email from users');
while($dnn = mysql_fetch_array($req))
{
?>
        <tr>
        <td class="left"><?php echo $dnn['id']; ?></td>
        <td class="left"><a href="profile.php?id=<?php echo $dnn['id']; ?>"><?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?></a></td>
        <td class="left"><?php echo htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8'); ?></td>
    </tr>
<?php
}
?>
</table>
                </div>
                <div class="foot"><a href="<?php echo $url_home; ?>">Go Home</a> - <a href="http://www.webestools.com/">Webestools</a></div>
        </body>
</html>

Profile of an user

We display the informations of an user.
profile.php
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />
        <title>Profile of an user</title>
    </head>
    <body>
        <div class="header">
                <a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
            </div>
        <div class="content">
<?php
//We check if the users ID is defined
if(isset($_GET['id']))
{
        $id = intval($_GET['id']);
        //We check if the user exists
        $dn = mysql_query('select username, email, avatar, signup_date from users where id="'.$id.'"');
        if(mysql_num_rows($dn)>0)
        {
                $dnn = mysql_fetch_array($dn);
                //We display the user datas
?>
This is the profile of "<?php echo htmlentities($dnn['username']); ?>" :
<table style="width:500px;">
        <tr>
        <td><?php
if($dnn['avatar']!='')
{
        echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar" style="max-width:100px;max-height:100px;" />';
}
else
{
        echo 'This user dont have an avatar.';
}
?></td>
        <td class="left"><h1><?php echo htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8'); ?></h1>
        Email: <?php echo htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8'); ?><br />
        This user joined the website on <?php echo date('Y/m/d',$dnn['signup_date']); ?></td>
    </tr>
</table>
<?php
        }
        else
        {
                echo 'This user dont exists.';
        }
}
else
{
        echo 'The user ID is not defined.';
}
?>
                </div>
                <div class="foot"><a href="users.php">Go to the users list</a> - <a href="http://www.webestools.com/">Webestools</a></div>
        </body>
</html>

You also have to edit the MYSQL IDs in the config.php file.
config.php
<?php
//We start sessions
session_start();

/******************************************************
------------------Required Configuration---------------
Please edit the following variables so the members area
can work correctly.
******************************************************/

//We log to the DataBase
mysql_connect('hote', 'username', 'password');
mysql_select_db('database');

//Webmaster Email
$mail_webmaster = 'example@example.com';

//Top site root URL
$url_root = 'http://www.example.com';

/******************************************************
-----------------Optional Configuration----------------
******************************************************/

//Home page file name
$url_home = 'index.php';

//Design Name
$design = 'default';
?>

The users IDs and usernames are now in the table "users". You can now use this Members Area to identify the users in another system.

This is a demonstration of the Members Area:
Demonstration
You can also download the Members Area as a .zip or .rar archive:
Image
Download the .ZIP archive

Image
Download the .RAR archive

Thank you and I hope this php top site will be useful.

Similar Scripts and Tutorials

Personal Message System in php mysql - pm system private message discussion Personal Message System in php mysql - pm system private message discussion Simple PHP Forum Script - php forum easy simple script code download free php forum mysql Simple PHP Forum Script - php forum easy simple script code download free php forum mysql Number of mysql queries and execution time - query counter mysql timing php Number of mysql queries and execution time - query counter mysql timing php [Tutorial]Create a web 2.0 Logo with photoshop (Web 2.0 Title) [Tutorial]Create a web 2.0 Logo with photoshop (Web 2.0 Title) Top site PHP script - install a php top site on his web site Top site PHP script - install a php top site on his web site