We then compare the two passwords that the user provided in the form. This is just to make sure that the passwords match before we insert it into the database: if("$pw1" !== "$pw2" ){ print "your confirmation password has been mistyped or is empty,please try again"; $conf="your confirmation password has been mistyped or is empty,please try again"; exit; } The next step is to check that the user name which the new user provided does not already exist in the database. To do this we need to connect to the database server, and then run a query to see if the username is found: //connect to the db server , check if uname exist include('../config.inc'); $query="Select name from user where uname='$name'"; $result= mysql_query($query); We use the $num variable to see if the query returned any results. If there are results, then it means that there already is a name that is the same as the one provided by the user, in which case an appropriate error message is sent: $num=mysql_num_rows($result); if ($num > 0) {//Username already exist print "The username is already taken,please try again"; $taken="The username is already taken,please try again"; print "<p><a href=http://localhost/loginscript/register.php>Click here to try again.</a></p>"; exit; Otherwise, the name provided by the user does not exists in the database, so the new details of the user are inserted in the database, the appropriate query string value is set, and the user is directed to the login page: }else{ //if username does not exist insert user details $query="INSERT INTO users (uname, pw,email) VALUES ('$name',password('$pw1'),'$email')" if (@mysql_query ($query)) { //print "Your details have been added"; header("location:login.php?reg=1"); exit; } } } } The HTML form is responsible for collecting the registration information from the user. The actual form code is detailed and explained below. The first line in the code declares the form header, which includes the name of the form, the method that the form uses to send information, onsubmit and finally the name of the script that processes the form data: <form name="form1" action="register.php" method="post" onSubmit="return checkform(this)" > A table is then created that will contain the actual form elements: <table width="657" border="0"> <tr> <td width="122"><div align="left">Name</div></td> The first element of the form takes the name of the user: <td width="525"><input name="name" type="text" size="40"></td> </tr> <tr>
blog comments powered by Disqus |
|
|
|
|
|
|
|