Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Simple Php Login And Registration System
MiniK
post Oct 21 2007, 12:15 AM
Post #1


Advanced Member
*******

Group: Members
Posts: 111
Joined: 29-September 07
From: United Kingdom
Member No.: 50,853



Hello. This is my first web tutorial ever. This is basically a simple register and login script. Yes, I know it’s a bit rubbish but I’m quite new to PHP/MySQL.

Here’s the register form. This can be any file extension you like. I’d recommend calling it register.html.

CODE
<!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=iso-8859-1" />
<title>Register</title>
</head>

<body>
<h1>Register</h1>
<table><tr>
<form action=register.php method=post>

<td width="81">Username:</td>
<td width="247"><input name="username" size="30" autocomplete="off" value="" type="text" /></td>

</tr><tr>

<td>Password:</td>
<td><input name="password" size="30" type="password" /></td>

</tr>

<tr>

<td>First Name:</td>
<td><input name="firstname" size="30" type="text" /></td>

</tr>

<tr>

<td>Last Name:</td>
<td><input name="lastname" size="30" type="text" /></td>

</tr>

<tr>

<td>Age:</td>
<td><input name="age" size="30" maxlength="2" /></td>

</tr>
</table>


<p><input type="submit" class="button" value="Register" /></p>
</form>
</body>
</html>


Now create a MySQL database. Then create a file that will be called
CODE
mysql-connect.php
. Here is the file:

CODE
<?php
$con = mysql_connect("DB_HOST","DB_USER","DB_PASS");
mysql_select_db("DB_NAME", $con);
?>


Replace DB_HOST with the host of your database. This is usually “localhost”, but some hosts differ. Replace DB_USER with the username for your database, and DB_PASS with the password of your database and then replace DB_NAME with the name of your database. Enough with this file, let’s get onto the actual registration script. Save this as
CODE
register.php
.

CODE
<?php
include 'mysql-connect.php';
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$ip = $_SERVER['REMOTE_ADDR'];

$result = mysql_num_rows(mysql_query("SELECT * FROM TABLENAME WHERE username='$username'"));
if($result == 1)
    {
    echo ‘<h1>ERROR!</h1>The username you have chosen already exists!’;
    }
else
    {
    mysql_query("INSERT INTO TABLENAME (username, password, firstname, lastname, age, ip)
VALUES ('$username', '$password', '$firstname', '$lastname', '$age', '$ip')");

    echo '
  <p>Congratulations! You have successfully registered! </p>
  <p>Click <a href="login.php">here</a> to login.</p>‘;
?>


OK, let’s break this down:

CODE
include 'mysql-connect.php';


Include the database connection file.

CODE
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$ip = $_SERVER['REMOTE_ADDR'];


This part gets all of the variables: username, password, first name, last name, age and ip address.

CODE
$result = mysql_num_rows(mysql_query("SELECT * FROM TABLENAME WHERE username='$username'"));


This checks to see if the username already exists in the database. Make sure you change “TABLENAME” to the name of the table in which the user information is stored.

CODE
if($result == 1)
    {
    echo ‘<h1>ERROR!</h1>The username you have chosen already exists!’;
    }
else
    {
    mysql_query("INSERT INTO TABLENAME (username, password, firstname, lastname, age, ip)
VALUES ('$username', '$password', '$firstname', '$lastname', '$age', '$ip')");

    echo '
  <p>Congratulations! You have successfully registered! </p>
  <p>Click <a href="login.php">here</a> to login.</p>‘;


If the username already exists, display an error message, and if not, insert the user information into the database and display a login link. Make sure you change “TABLENAME” to the name of the table in which the user information is stored. Now onto the login form. This is quite simple. Just save it as login.php.

CODE
<html>
<head>
<title>Login</title>
</head>
<body>
<form name="login" action="login2.php" method="post">


<table align="center"><tr>

<td class="title">Username</td>
<td><input name="user" size="30" autocomplete="off" value="" type="text" /></td>

</tr><tr>

<td class="title">Password</td>
<td><input name="pass" size="30" type="password" /></td>

</tr></table>


<p style="text-align:center;"><input type="submit" class="button" value="Login" /></p></form>
</body>
</html>


Basically, that asks for username and password, and sends them to another file called
CODE
login2.php
which we shall move onto now…

CODE
<?php
include 'mysql-connect.php';

$username = $_POST['user'];
$password = $_POST['pass'];
$query1 = mysql_query("SELECT * FROM TABLENAME WHERE username='$username'");
$result = mysql_num_rows($query1);
if($result == 0)
{
include '<h1>Error!</h1>The username you specified does not exist!';
}
else
{

$checkuser = mysql_query("SELECT * FROM TABLENAME WHERE username='$username'");

            $row = mysql_fetch_array($checkuser);
                            $password2 = $row['password'];
                            $status = $row['status'];
                if ($password == $password2)
                    {
                    //PUT PASSWORD PROTECTED INFORMATION HERE
                    }
                else
                    {
                    echo '<h1>Error!</h1>The username and password combination you entered does not match the ones we have in the database.';
                    }

}
?>


Let’s break this file down aswell.
CODE
$username = $_POST['user'];
$password = $_POST['pass'];


This grabs the username and password that they entered.

CODE
$query1 = mysql_query("SELECT * FROM TABLENAME WHERE username='$username'");
$result = mysql_num_rows($query1);


This checks to see if the user exists in the database. Make sure you change “TABLENAME” to the name of the table in which the user information is stored.

CODE
if($result == 0)
{
include '<h1>Error!</h1>The username you specified does not exist!';
}


If not, display an error message.

CODE
else
{

$checkuser = mysql_query("SELECT * FROM TABLENAME WHERE username='$username'");

            $row = mysql_fetch_array($checkuser);


If the user does exist, get the information stored in the database about that user. Make sure you change “TABLENAME” to the name of the table in which the user information is stored.

CODE
$password2 = $row['password'];


Get the user’s password.

CODE
if ($password == $password2)
                {
                //PUT PASSWORD PROTECTED INFORMATION HERE
                }


If the password in the database matches the one they entered, display password protected information.

CODE
else
                {
                    echo '<h1>Error!</h1>The username and password combination you entered does not match the ones we have in the database.';
                }

}


If not, display yet another error message.

OK, that’s the script. Hope you liked it. It was for a website I was making but I have no need for it anymore, so I thought I would post it here so that other people can learn from it. wink.gif This /should/ work, but if it doesn't, just let me know and I can advise you on what is wrong and can edit it. We can ALL learn from our mistakes.
Go to the top of the page
 
+Quote Post
karlosvalencia
post Jan 5 2008, 01:35 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 17
Joined: 5-January 08
From: Ottawa - Canada
Member No.: 55,741



Hello,

I'm new to PHP myself and have what I hope is a basic question. I'd like to have password protected pages on my site. Simple have no more than 10 pages, each one with its own password. Is this doable with PHP, do I need MySQL installed for that?

Furthermore, if I see a server like the one provided by trap17.com how can I install my PHP script on it?
Go to the top of the page
 
+Quote Post
Acid
post Jan 5 2008, 10:07 PM
Post #3


Newbie [Level 1]
*

Group: Members
Posts: 22
Joined: 26-December 07
From: Denmark
Member No.: 55,309



You mention in the end, that this system is for the purpose that other peoples can/will learn from it - To be honest, if I was new to PHP/MySQL, I wouldn't understand half of it. I think you should comment more what the codes do, give a little bit description.

But good job on making it, I guess.
Go to the top of the page
 
+Quote Post
coldasice
post Jan 6 2008, 10:56 PM
Post #4


Newbie [Level 2]
**

Group: Members
Posts: 34
Joined: 10-December 07
From: Norway
Member No.: 54,556



any ways.. use md5($password) for secure password smile.gif
Go to the top of the page
 
+Quote Post
GaiaZone
post Jan 10 2008, 10:37 PM
Post #5


Newbie [Level 2]
**

Group: Members
Posts: 25
Joined: 8-January 08
From: PR
Member No.: 55,961



Thanks for this tutorial!

I'm just starting with MySQL, and I actually understood everything.

Great job!

EDIT: Never mind that last question, found the answer.

Another question thought, I'm getting this error when I run the script (after correcting all the other errors):

QUOTE
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gaiazone/public_html/mysql/login2.php on line 7


All my problems seem to be with that command.

Any help?

This post has been edited by GaiaZone: Jan 11 2008, 12:23 AM
Go to the top of the page
 
+Quote Post
Imtay22
post Jan 13 2008, 12:53 PM
Post #6


Super Member
*********

Group: Members
Posts: 292
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



QUOTE(karlosvalencia @ Jan 5 2008, 08:35 AM) *
Hello,

I'm new to PHP myself and have what I hope is a basic question. I'd like to have password protected pages on my site. Simple have no more than 10 pages, each one with its own password. Is this doable with PHP, do I need MySQL installed for that?

Furthermore, if I see a server like the one provided by trap17.com how can I install my PHP script on it?


Am am not totally sure how to do this, but I know it has something to do with the .htaccess file. I will search it up in a sec and get it back to you.

Back on Topic- This is a very nice tutorial, I am needing one of these for my site. Do you mind if I adjust it so it can be a login for my forum, instead of a site?

Thanks,

Imtay
Go to the top of the page
 
+Quote Post
coldasice
post Jan 13 2008, 05:40 PM
Post #7


Newbie [Level 2]
**

Group: Members
Posts: 34
Joined: 10-December 07
From: Norway
Member No.: 54,556



QUOTE(GaiaZone @ Jan 10 2008, 11:37 PM) *
Thanks for this tutorial!

I'm just starting with MySQL, and I actually understood everything.

Great job!

EDIT: Never mind that last question, found the answer.

Another question thought, I'm getting this error when I run the script (after correcting all the other errors):
All my problems seem to be with that command.

Any help?



mby u forgot or did anything wrong in the connection of mysql db.. or else mby iu forgot to change table name or somthing =D
Go to the top of the page
 
+Quote Post
hitmanblood
post Jan 13 2008, 07:58 PM
Post #8


Privileged Member
*********

Group: [HOSTED]
Posts: 775
Joined: 13-April 07
From: mreža
Member No.: 41,558



Hello I must say that this is good tutorial however there are few things to attent first of all write more comments user secure password and name all your files wit