|
|
A Simple Register Script - This Is a Very Simple Register-Script | ||
Discussion by Feelay with 16 Replies.
Last Update: January 12, 2012, 3:49 pm (View Latest) | Page 1 of 2 pages. | ||
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;
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
<?phprequire_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
<?phpif(
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');
?>
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.
Sat Jan 19, 2008 Reply New Discussion
Sun Jun 22, 2008 Reply New Discussion
CODE
Line 16;"Username And Password Can Not Be The Same.";
Should be;
echo "Username And Password Can Not Be The Same.";
CODE
Line 42;"You Are Registered And Can Now Login";
Should be;
echo "You Are Registered And Can Now Login";
CODE
Line 50;"The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
Should be;
echo "The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
CODE
Line 57;"You Could Not Be Registered Because Of Missing Data.";
Should be;
echo "You Could Not Be Registered Because Of Missing Data.";
Other than that, nice job.
Wed Jul 23, 2008 Reply New Discussion
QUOTE
U made many help full tutorials, however I think u forgot the <?php in the regcheck.php page, thanks for the tutorialsQUOTE
In the regcheck.php document, you forgot some of the echo commands.CODE
Line 16;
"Username And Password Can Not Be The Same.";
Should be;
echo "Username And Password Can Not Be The Same.";
CODE
Line 42;
"You Are Registered And Can Now Login";
Should be;
echo "You Are Registered And Can Now Login";
CODE
Line 50;
"The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
Should be;
echo "The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
CODE
Line 57;
"You Could Not Be Registered Because Of Missing Data.";
Should be;
echo "You Could Not Be Registered Because Of Missing Data.";
Other than that, nice job.
So.. changed them. Thanks for the comment
//Feelay
Wed Jul 23, 2008 Reply New Discussion
I'm trying to use this code as a beginner, so this is probably a stupid question, but...
I've gotten the script to work up to the regcheck part. And the regcheck errors work too. But when I try to register with an actual account, for example a password and username that are different and are both longer than four letters, I get this error message:
QUOTE
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\regcheck.php on line 23Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\regcheck.php on line 23
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\regcheck.php on line 29
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\regcheck.php on line 29
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\regcheck.php on line 29
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\regcheck.php on line 38
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\regcheck.php on line 38
You Could Not Register Because Of An Unexpected Error.
What's going on, and how do I fix this?
Thu Aug 12, 2010 Reply New Discussion
QUOTE (silence)
Hi,I'm trying to use this code as a beginner, so this is probably a stupid question, but...
I've gotten the script to work up to the regcheck part. And the regcheck errors work too. But when I try to register with an actual account, for example a password and username that are different and are both longer than four letters, I get this error message:
What's going on, and how do I fix this?
Link: view Post: 147713
Have you connected to the database?
Thu Aug 12, 2010 Reply New Discussion
I'm not sure...
I looked it up here: http://www.webcheatsheet.com/PHP/connect_mssql_database.php
and followed the instructions, but it still won't work.
I really have no idea what I'm looking for. How can I tell if I'm connected?
Thu Aug 12, 2010 Reply New Discussion
Remember to hide the username, password and host.
Thu Aug 12, 2010 Reply New Discussion
CODE
<?$con = mysql_connect('---','---','---');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('register_database');
?>
Thu Aug 12, 2010 Reply New Discussion
mysql_select_db('register_database');
to
mysql_select_db('register_database') or die(mysql_error());
also, please show my your regcheck.php
Thu Aug 12, 2010 Reply New Discussion
CODE
<?phpif(
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.";
}
?>
Thu Aug 12, 2010 Reply New Discussion
You should double check it and make sure that it's correct.
It's not supposed to be like the ones in the tutorial, it's supposed to be replaced with your MySQL account information =)
Thu Aug 12, 2010 Reply New Discussion
At line 1 you may have <?
Try changing that to <?php
See if that helps.
Wed Aug 25, 2010 Reply New Discussion
I think thats the error i know 000webhost(free) has a different host name.
Thu Aug 26, 2010 Reply New Discussion
You shouldn't use localhost. What is your host?
Wed Apr 13, 2011 Reply New Discussion
Attack Script In Php This is a funny attack script that i made (5)
|
(40) How To Make A Private Message System.
|
Index




