Jump to content
xisto Community
Feelay

A Simple Register Script This Is a Very Simple Register-Script

Recommended Posts

Some time ago, i made a login-script.

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:

 

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"

<?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.

 

<?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:

 

<?$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.

Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

In the regcheck.php document, you forgot some of the echo commands.

Line 16;  "Username And Password Can Not Be The Same.";Should be;echo "Username And Password Can Not Be The Same.";
Line 42;"You Are Registered And Can Now Login";Should be;echo "You Are Registered And Can Now Login";
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.";
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.

Share this post


Link to post
Share on other sites

U made many help full tutorials, however I think u forgot the <?php in the regcheck.php page, thanks for the tutorials

haha ^.^ Thanks for the comment :mellow:

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 :D

//Feelay
Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

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:

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 23
Warning: 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?
Edited by silence (see edit history)

Share this post


Link to post
Share on other sites

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?



Have you connected to the database?

Share this post


Link to post
Share on other sites

Try changingmysql_select_db('register_database');to mysql_select_db('register_database') or die(mysql_error());also, please show my your regcheck.php

Share this post


Link to post
Share on other sites

Thanks, here's the regcheck:

<?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.";}?>

Share this post


Link to post
Share on other sites

My guess is that your database connection is at fault, and now I mean the username, password and host.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 =)

Share this post


Link to post
Share on other sites

Is your host 'localhost' and what are you using to host your website please as some websites have there own hostname...I think thats the error i know 000webhost(free) has a different host name.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.