Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Unofficial > Website > PHP & MySQL > Reading a tutorial

Make your member zone !

Avatar
Author : vcarter
Creation : : on 03/08/2007 09:53:20 PM
Last modification: : on 03/26/2007 01:34:40 AM
Rate and comment this tutorial
Print this tutorial
You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
Hello world !
If you are here, you want to make a members' zone but you are a beginning PHP user...
Indeed, this tutorial uses PHP (and xHTML).
If you don't know PHP, you must learn it before reading this tutorial.
In this page, I guide you for creating a members zone but I won't teach you PHP.

So, good reading ! :p
Chapter Contents :

The connection

The questions which you must you ask yourself



When beginning a code project, you must you ask yourself these questions :

Why am I creating this project ?
What is my project's goal ?


The answers for this project should be :

This project has to allow members to connect on my website to do personal actions.


It's alright. :p
(If you have been surprised by my answer, there is a problem ^^ )
So, the project has to allow members to connect for some personal actions.
To start off, we must allow members to connect.

The Database


To allow a member to connect, we have to store his personal information (login & password)
We have to create a database for storing members' personal information.

You have to create a new database named "members_zone".
In this database, you have to create a new table named "members".

The table has those columns :

"id" / "pseudo" / "password" / other informations like "webmail"...


The connection



In order to connect, members must enter their login and password.

So : (the file is index.php)

Code: PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!--Im making the form-->
<form action="index.php" method="post">
<input type="text" name="login"> 
<input type="password" name="password">
<input type="submit" value="Send Now !">
</form>

<!--The PHP code is in the same page (the form send the posts in this page)-->
<?php
//I check the login isn't null
if(isset($_POST['login']) && !empty($_POST['login']))
{
//For the security
$login = htmlentities($_POST['login']);
$password = htmlentities($_POST['password']); 

mysql_connect("localhost", "root", ""); // DB connection
mysql_select_db("members_zone"); // Base choice

$sql = mysql_query('SELECT count(*) FROM members WHERE pseudo = "'.$login.'" AND  password = "'.$password.'"');
$data = mysql_fetch_array($sql);

//I check the login and the password are correct (if the result > 0)
if ($data[0] != 0) 
{
//If they are correct, I create a session and I redirect the members to the members page
$_SESSION['login'] = $login;
$_SESSION['password'] = $password;
header("location: members.php"); //I redirect to the members page
}
else
{
echo 'The password is wrong ! Retry !';
}

}
?>


What else ? :p

The member who types a login and a password which are in the DB is redirected to the members zone.

We didn't create a inscription page !


Well, we'll make it now.

The page inscription.php :

Code: PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<form action="inscription.php" method="post">
Login : <input type="text" name="login"></br>
Password : <input type="password" name="password"></br>
Retype password : <input type="password" name="password2"></br>
<input type="submit" value="Send Now !">

<?php
if (isset($_POST['login']) && !empty($_POST['login']) && isset($_POST['password']) && !empty($_POST['password']))
{
//For the security
$login = htmlentities($_POST['login']); 
$password = htmlentities($_POST['password']);
$password2 = htmlentities($_POST['password2']);

if ($password == $password2) //If the 2 password are the same
{
mysql_connect("localhost", "root", ""); // DB connection
mysql_select_db("members_zone"); // Base choice

$sql = mysql_query('INSERT INTO members VALUES("", "'.$login.'", "'.$password.'", "......other informations")'); //You must enter one information in all columns, it may be void.

echo '</br>The inscription is finished ! You can connect to the members zone  now !';
}
else
{
echo '</br>The 2 password are different !';
}

}
else
{
echo 'Error, retry !';
}
?>


Now, we have to welcome him.

The reception

After, the connection : the reception !

The member was sent to the members' zone.
We have to welcome him now !

The code : (the page is members.php)

Code: PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
//I check the session exists, if it doesn't exist, the member is redirect to index.php
session_start(); //Don't forget that, else the member will be disconnected.
if (!isset($_SESSION['login']))
{
header("location: index.php");
}

echo 'Welcome on my website '.$_SESSION['login'].' !</br>
I hope you enjoy it !';
?>


What else again ?
Now, we are going to use the database to ask personal information.
Your database must contain pseudo/password/email/other informations columns.

The code :

Code: PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
//I check the session exists, if it doesn't exist, the member is redirect to index.php
session_start();
if (!isset($_SESSION['login']))
{
header("location: index.php");
}

mysql_connect("localhost", "root", ""); // DB connection
mysql_select_db("members_zone"); // Base choice

//I put all informations on a table
$sql = mysql_query('SELECT * FROM members WHERE pseudo = "'.$_SESSION['login'].'"');
$data = mysql_fetch_array($sql);

$password = $data['password']; //I find informations on this table
$webmail = $data['webmail'];

echo 'Welcome on my website '.$_SESSION['login'].' !</br>
I hope you enjoy it !</br>
Your password is "'.$password.'" and your webmail is "'.$webmail.'" !';
?>


That's all !

Upgrade ideas :



That's all ! Was it very hard for you ?
I hope you understood me, and you enjoyed my tutorial !

Don't hesitate to leave a comment & PM me ! :whistle:

See you. :)
Author : vcarter
Rate and comment this tutorial
Print this tutorial

Change your template | Learn more about NP | Site map | Terms of use | Rules | RSS feed | XHTML 1.0 | CSS 2.0
Powered by Simple IT SARL: Contact us

There is nothing else to read, you have to go up now !

Do you want to be published here? Contact us.

Number of Newbies connected 2 Newbies Connected | SQL requests 7 Requests | Page loading delay 0.1035s (0.091s)