Sign-up & Log-in Script

Supposing you have all the features that make a great and sticky interactive website: feedback forms, guestbooks, tagboards and forums. What's the next step to take to get a real community site happening? A member's section! Whether you want to offer paying members more, or want to validate all your users, the best way to go is a PHP/MySQL powered sign-up and log-in script.

Yes, you can have a password protected section of your site using .htaccess, however with .htaccess you need to manually add users to the file before they can log in, and you can't easily store any extra information about them. With this tutorial you can soon create a completely automated system.

So let's start! First, as with the guestbook tutorial, we'll need to set up a connection to the MySQL database, so that we can set it up for use with our PHP script.

<?php
$server = "localhost";  // server to connect to.
$database = "mydata";  // the name of the database.
$db_user = "myusername";  // mysql username to access the database with.
$db_pass = "mypassword";  // mysql password to access the database with.
$table = "users";    // the table that this script will set up and use.
?>
Put the above code into a text file, and name it config.php. We will include this file into all our pages. But what does it mean?

Remember that everything following these // double slashes is just a comment or a note to yourself.

$server is the location of the MySQL server. Sometimes it will be a name or an IP, but usually it will be "localhost." localhost means that it is on the same computer as the script.
$database is the name of the database you will be using. Usually this is created by your host upon request, or you can do it yourself through your control panel.
$db_user and $db_pass are your MySQL server user information. Ask your host if you are unsure of this information.
$table is the part of the database that will store your user information; we are about to create it.

Next we will write the script which will create our tables on the MySQL database:

<?php

include ("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// create table on database
$create = "create table $table (
id smallint(5) NOT NULL auto_increment,
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username)
);";

mysql_query($create)
or die ("Could not create tables because ".mysql_error());
echo "Complete.";
?>
The first line of the script calls the MySQL connection information we put into config.php. The next two sections connect to the MySQL server with that information, and then selects the database we will be working with.

$create is the command for creating the table. In the table we have three fields: id, username and password. After defining the $create variable, we then use it in a mysql_query to create the tables on the database. If something goes wrong, we will get an error message telling us so.

If you wish to collect more information about your users than their username and password (like their email address, real name, location, etc), simply add more fields to the $create variable, like so:

// create table on database
$create = "create table $table (
id smallint(5) NOT NULL auto_increment,
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
email varchar(200) NOT NULL,
realname varchar(200) NOT NULL,
location varchar(200) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username)
);";
Call this script create.php. Upload it to your webserver and visit it in your browser (i.e. http://domain.com/create.php). It will create the tables in your MySQL database, which will now be ready to use with the script.

Next we will write the user registration script.