The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > PHP


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-13-2007, 03:21 PM
Blood08's Avatar
Blood08 Blood08 is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial 
Total Awards: 2
Join Date: Jun 2007
Location: Source Code
Posts: 98
iTrader: (0)
Blood08 is on a distinguished road
PHP/MYSQL Register System

Quote:
Credit Goes To Scott From Time 2 Design - Index - Free Tutorial on C++, Photoshop, PHP, MySQL, Web Design, Flash. for this tutorial

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.
PHP Code:
<?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,

PHP Code:
<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.'

hopyfuly that made sense....

__________________
Fersk Webmasters' Forum
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 06-13-2007, 04:16 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Correct me if i am wrong but is this not a register system, not a login system.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 06-13-2007, 04:20 PM
Blood08's Avatar
Blood08 Blood08 is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial 
Total Awards: 2
Join Date: Jun 2007
Location: Source Code
Posts: 98
iTrader: (0)
Blood08 is on a distinguished road
will change... thanks for correcting me

__________________
Fersk Webmasters' Forum
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 06-13-2007, 04:54 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Quote:
Originally Posted by Blood08 View Post
will change... thanks for correcting me
Do i class as a geek now??

I just made a register system like this already so when i saw it i was like o.0

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 06-13-2007, 07:11 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
O_o and you said that I'm a geek. Oh well, we're geek community?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 08-01-2007, 12:20 PM
chrisazy
Posts: n/a
Can someone make a login script that works with this? I love the register script (mainly because it actually works) and i want more

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 08-01-2007, 01:20 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Why dont you take the information in the tutorial, add and remove what you need and if you have any problems you can just post in the php section for help and everyone here will be glad to help you out, that way if your script goes all messed up which things do, you will know whats up
Reply With Quote
  #8 (permalink)  
Old 02-05-2008, 04:18 AM
Norman Norman is offline
Novice
Join Date: Feb 2008
Posts: 1
iTrader: (0)
Norman is on a distinguished road
Where is the login.php code?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #9 (permalink)  
Old 02-06-2008, 05:23 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Quote:
Originally Posted by Norman View Post
Where is the login.php code?
This is for a registration, a login is just as simple and can be done with basic knowledge of php and mysql if you have any.
Reply With Quote
  #10 (permalink)  
Old 02-08-2008, 06:48 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Just change the query to where username = and where password =, if you return a record, then they could login - return their userid, store the userid in a session and you use that site-wide as their hook to the user thats logged in.
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:18 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


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 38 39 40 41 42 43 44 45 46 47 48 49 50