But how do you use a login-script, if you can't register.
So this morning, I decided to make a register-script..
What you should already know:
The php basics and a little more.
How to use php and mysql together.
The HTML basics (to make the forms).
The first thing we should do, is creating the database tables. Here is the code:
CODE
CREATE TABLE `user` (
`id` int(4) unsigned NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`level` int(4) default '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;
`id` int(4) unsigned NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`level` int(4) default '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;
The first thing we do, is adding the "id" column. Nothing important with that one.
Then we add the "username" and "password" columns. Nothing important here either.
But then, we add the "level" column. In my login script, i have an admin system. This is why i made the "level" column. The default value is 1.. but if you change it to 9, the user will become an admin.
Now Lets Move On to "reg.php"
CODE
<?php
require_once 'database.php';
?>
<h1><strong>Register</strong></h1>
<form name="register" method="post" action="regcheck.php">
<label>
<input type="text" name="user" id="user">
</label>
Username <br><br>
<label>
<input type="password" name="pass" id="pass">
Password<br />
</label><label>
<input type="submit" name="reg" id="reg" value="Register">
</label>
</form>
<label></label>
<form name="Back" method="post" action="Login.php">
<input type="submit" name="back" id="back" value="Back to Home">
<p> </p>
require_once 'database.php';
?>
<h1><strong>Register</strong></h1>
<form name="register" method="post" action="regcheck.php">
<label>
<input type="text" name="user" id="user">
</label>
Username <br><br>
<label>
<input type="password" name="pass" id="pass">
Password<br />
</label><label>
<input type="submit" name="reg" id="reg" value="Register">
</label>
</form>
<label></label>
<form name="Back" method="post" action="Login.php">
<input type="submit" name="back" id="back" value="Back to Home">
<p> </p>
The first thing I do, is requiring the "database.php".
It will open a connection to the database.
Then, we add the forms and buttons
It isn't harder than that. Now we have the Register Page
Now, we need the regcheck.php page.
CODE
<?php
if(
isset( $_POST['user'] ) &&
isset( $_POST['pass'] )
)
{
if( strlen( $_POST['user'] ) < 4 )
{
echo "Username Must Be More Than 4 Characters.";
}
elseif( strlen( $_POST['pass'] ) < 4 )
{
echo "Passwrod Must Be More Than 4 Characters.";
}
elseif( $_POST['pass'] == $_POST['user'] )
{
echo"Username And Password Can Not Be The Same.";
}
else
{
include( 'database.php' );
$username = mysql_real_escape_string( $_POST['user'] );
$password = md5( $_POST['pass'] );
$sqlCheckForDuplicate = "SELECT username FROM user WHERE username = '". $username ."'";
if( mysql_num_rows( mysql_query( $sqlCheckForDuplicate ) ) == 0 )
{
$sqlRegUser = "INSERT INTO
user( username, password )
VALUES(
'". $username ."',
'". $password ."'
)";
if( !mysql_query( $sqlRegUser ) )
{
echo "You Could Not Register Because Of An Unexpected Error.";
}
else
{
echo "You Are Registered And Can Now Login";
$formUsername = $username;
header ('location: Login.php');
}
}
else
{
echo "The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
$formUsername = $username;
}
}
}
else
{
echo "You Could Not Be Registered Because Of Missing Data.";
}
?>
if(
isset( $_POST['user'] ) &&
isset( $_POST['pass'] )
)
{
if( strlen( $_POST['user'] ) < 4 )
{
echo "Username Must Be More Than 4 Characters.";
}
elseif( strlen( $_POST['pass'] ) < 4 )
{
echo "Passwrod Must Be More Than 4 Characters.";
}
elseif( $_POST['pass'] == $_POST['user'] )
{
echo"Username And Password Can Not Be The Same.";
}
else
{
include( 'database.php' );
$username = mysql_real_escape_string( $_POST['user'] );
$password = md5( $_POST['pass'] );
$sqlCheckForDuplicate = "SELECT username FROM user WHERE username = '". $username ."'";
if( mysql_num_rows( mysql_query( $sqlCheckForDuplicate ) ) == 0 )
{
$sqlRegUser = "INSERT INTO
user( username, password )
VALUES(
'". $username ."',
'". $password ."'
)";
if( !mysql_query( $sqlRegUser ) )
{
echo "You Could Not Register Because Of An Unexpected Error.";
}
else
{
echo "You Are Registered And Can Now Login";
$formUsername = $username;
header ('location: Login.php');
}
}
else
{
echo "The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
$formUsername = $username;
}
}
}
else
{
echo "You Could Not Be Registered Because Of Missing Data.";
}
?>
Now the first thing we do here, is to check if the password and user field is checked.
If they are, we check if the fields are more than 4 characters.
And then, we check if the password is the same as the username.
If no errors occurred, we include the database.php
Then make 2 variables.
The $username variable, and the $password variable.
As you can see, the password is md5 protected.
Then we check if the username exists.
if it doesn't, the account will be registered,
and the user will be redirected to the index.php page.
But.. If it is being used, the code will write that the username is being used.
As you can see, i do include the database.php in both files.
Here it is:
CODE
<?
$con = mysql_connect('localhost','mysql_username','mysql_password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mysql_database');
?>
$con = mysql_connect('localhost','mysql_username','mysql_password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mysql_database');
?>
The first thing we do here, is:
Start the connection to the database server.
If it couldn't connect, an error will occur.
Then, we select the database we want to use.
This is my third tutorial. Tell me if you liked it, and if you found any errors. tell me, and I will change them.

