IPB
Your FREE membership includes:
  • A friendly community where you can come to chat
  • Receive completely FREE quality web hosting!
  • Get reviews and suggestions for your websites!
  • Take part in competitions where you can win prizes from cash, vouchers, or win special unadvertised hosting

Welcome Guest ( Log In | Register )


Shortcut links:
Like this topic? Digg it... Digg this topic · Save to del.icio.us · Slashdot It · Post to Technorati · Post to Furl · Submit to Reddit · Share on Facebook · Fark It · Googlize This Post · Add to ma.gnolia · Tag to Wink · Add to MyWeb · Add to Netscape
 
Reply to this topicStart new topic
> PHP/MySQL Login System
Nishi
post Oct 24 2006, 12:15 PM
Post #1


Member
Group Icon

Group: Members
Posts: 647
Joined: 18-September 06
Member No.: 1



Credit Goes To Scott From http://www.time-2-design.com/ for this tutorial[i]

In this tutorial I will explain how you can make your own PHP Login Script using a MySQL database. This includes a register, login, and member page.

1. First you need to create the database and to store your users information. If you have already done so then you should skip to step two. To create the database you may have to use a MySQL admin administrater because some host's don't always support it being done from a script. So create a database if you haven't already done so..

2. Now that we have a database or if we already had one then we need to setup the table and columns to store our users information. So you can use this code to setup the columns and you can fine tune it to customize your site.

QUOTE
<?php
// set your infomation.
$dbhost='localhost';
$dbusername='username';
$dbuserpass='password';
$dbname='database';
// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

//create the table, customize it if you want
$query = 'CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
username VARCHAR(30) NOT NULL,
password VARCHAR(20) NOT NULL,
email VARCHAR(40) NOT NULL)';
$result = mysql_query($query);
echo "Table Created!";
?>


Lets break this code down.
CREATE TABLE users - makes a table called users in the database.
id Int NOT NULL Auto_INCREMENT - Makes an int column called "id" that will increase by 1 for each entry.
PRIMARY KEY(id) - Makes "id" primary, so it is used as an index.
username VARCHAR(30) - Makes a column called "username" that is VARCHAR which can hold almost any character. The (30) means it can't be any larger than 30.
password VARCHAR(20) - Makes a column called "password" that is also VARCHAR and its maximum length is 20.
email VARCHAR(40)- Makes a column called "email" which is also VARCHAR but its maximum length is 40.
mysql_query($query) - Run the MySQL query to create the table.
echo "Table Created!"; - Print "Table Created!" on the screen.

3. Now we have our database setup, on to the next part, register,login, and index. I will start with a simple register page for the users to insert their username, password, and email.
Heres register.php,

QUOTE
<center>
<?php
// set your infomation.
$dbhost='localhost';
$dbusername='username';
$dbuserpass='password';
$dbname='database';
// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die("Cannot select database");

//Are they just getting here or submitting their info?
if (isset($_POST["username"])) {
$username = $_POST["username"];
$password = $_POST["password"];
$cpassword = $_POST["cpassword"];
$email = $_POST["email"];
//Was a field left blank?
if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL) {
echo "A field was left blank.";
}else{
//Do the passwords match?
if($password!=$cpassword) {
echo "Passwords do not match";
}else{
//Has the username or email been used?
$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'");
$username_exist = mysql_num_rows($checkuser);

$checkemail = mysql_query("SELECT email FROM users WHERE email='$email'");
$email_exist = mysql_num_rows($checkemail);

if ($email_exist>0|$username_exist>0) {
echo "The username or email is already in use";
}else{
//Everything seems good, lets insert.
$query = "INSERT INTO users (username, password, email) VALUES('$username','$password','$email')";
mysql_query($query) or die(mysql_error());
echo "The user $username has been successfully registered.";
}
}
}
}
?>
<h1>Register</h1>
<form action="register.php" method="POST">
<table style="border:1px solid #000000;">
<tr>
<td align="right">
Username: <input type="text" size="15" maxlength="25" name="username">
</td>
</tr>
<tr>
<td align="right">
Password: <input type="password" size="15" maxlength="25" name="password">
</td>
</tr>
<tr>
<td align="right">
Confirm Password: <input type="password" size="15" maxlength="25" name="cpassword">
</td>
</tr>
<tr>
<td align="right">
Email: <input type="text" size="15" maxlength="25" name="email">
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="Register">
</td>
</tr>
<tr>
<td align="center">
<a href="login.php">Login Here</a>
</td>
</tr>
</table>
</form>
</center>


Lets review this code.
$username = $_POST["username"]; - Get the POST variables and register them with local variables. The two variables below it do the same.
if($username==NULL|$password==NULL|$cpassword==NUL L|$email==NULL) - Check if any of the fields were left blank. This is the beginning of our chain of "if" statements.
if($password!=$cpassword) { - If the password is the same as confirm password.
mysql_query("SELECT username FROM users WHERE username='$username'"); - Check if the username is already in use, it also does the same for the email.
mysql_num_rows($checkuser) - Turns the result into a number which means how many rows it selected, $checkemail also does this.
if ($email_exist>0|$username_exist>0) { Check if the email or username is in use.
The rest is inserting the values and the HTML design.

4. Now that we have our register page and the database set up we can test out our register page. If you have a different name for the columns then what I had at the top then you will have to go through the scripts changing the MySQL queries. Otherwise when you register it should return 'The user $username has been successfully registered.'
Go to the top of the page
 
+Quote Post
Aquire
post Oct 24 2006, 02:12 PM
Post #2


Advanced Member
Group Icon

Group: Members
Posts: 215
Joined: 23-October 06
Member No.: 90



Nice tutorial.

Thank you Admin for help


--------------------
Go to the top of the page
 
+Quote Post
Ales
post Nov 1 2006, 11:51 AM
Post #3


Penguin fan
Group Icon

Group: Members
Posts: 185
Joined: 1-November 06
Member No.: 111



WoW Omg you have now showed me how to do something that doesnt bore me to death. Ill try it out when I can host cya around the forums. smile.gif


--------------------
Go to the top of the page
 
+Quote Post
Flexic
post Nov 2 2006, 09:26 PM
Post #4


Newbie


Group: Members
Posts: 29
Joined: 2-November 06
From: USA
Member No.: 116



time-2-design.com is a AWESOME website.

He is pure genius.


--------------------
Blog of Josh >> www.flexicdesigns.tfcph.com/joshtessin/
Go to the top of the page
 
+Quote Post
josh
post Sep 21 2007, 11:06 AM
Post #5


Starter Member
Group Icon

Group: Members
Posts: 33
Joined: 21-September 07
Member No.: 1046



cool
Go to the top of the page
 
+Quote Post
gamescoper
post Sep 29 2007, 10:32 AM
Post #6


Starter Member
Group Icon

Group: Members
Posts: 43
Joined: 29-September 07
Member No.: 1128



thats strange i sweari have seen this before but who cares it is great


--------------------
Go to the top of the page
 
+Quote Post
yo12334
post Oct 14 2007, 10:21 PM
Post #7


Advanced Member
Group Icon

Group: Advertising Team
Posts: 404
Joined: 24-September 07
From: de WATER
Member No.: 1084



i know i`m probally a idiot for asking this but how do you change the colors, like iv seen where people have the submit button a image how do they do that?


--------------------
"If everyone is thinking alike, then somebody isn't thinking."
"No bastard ever won a war by dying for his country. He won it by making the other poor dumb bastard die for his country."

Go to the top of the page
 
+Quote Post
WorldEvolution
post Oct 15 2007, 11:36 AM
Post #8


Starter Member
Group Icon

Group: Members
Posts: 55
Joined: 14-October 07
Member No.: 1177



Thankz I Will Use This On My Site. Much Propz
Go to the top of the page
 
+Quote Post
aungcho
post Aug 5 2008, 11:31 PM
Post #9


Advanced Member
Group Icon

Group: Members
Posts: 218
Joined: 17-October 06
From: Myanmar
Member No.: 69



A cool script. But I love sqlite. Sqlite is a file based system and I use it. MySQL and sqlite are almost the same.


--------------------
Go to the top of the page
 
+Quote Post
taytersalad
post Aug 6 2008, 05:22 PM
Post #10


Starter Member
Group Icon

Group: Members
Posts: 108
Joined: 12-April 08
Member No.: 1557



That's an extremely insecure way of doing things! None of the data is checked, someone could put ANYTHING in those fields and inject it into the database! HTML, Java, PHP, anything. The input maxlength does nothing, it can be easily bypassed using some simple Firefox plugins. If you value your db's security, do not use this script - or modify it to sanitize the data!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are browsing this forum (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts  
3 thegrrl 279 20th September 2006 - 10:42 AM
Last post by: Admin
No New Posts  
13 Nishi 572 10th November 2008 - 04:56 PM
Last post by: sam06
No new  
80 filip 1874 10th May 2008 - 02:29 PM
Last post by: thijzie
No new  
20 filip 1016 12th November 2007 - 06:39 PM
Last post by: Darren
No New Posts  
9 mtorregiani 471 28th November 2008 - 09:23 AM
Last post by: danta



© GrandForums.com