Creating A MySQL and PHP Registration Script


reating A Registration Script--]

This is an easy tutorial on how to create a nice looking User registration script for a user interface.
I normally use this type of stuff for gaming sites e.g. www.thugs-incorporated.info but it
can be used for any site really.

Ok First make sure you have a nice and easy mysql database dump.
For my mysql table i have used a table called `users`. The simple rows in that which i used
were id, username, password, email and remote ip.

Now we need to create the simple db_connect script in a folder named includes. Just use this file and name it db_connect.php

Code
<?php
$mysql_server = "localhost";
$mysql_user = "yourdatabaseuser_yourdatabase";
$mysql_password = "your DB pass";
$mysql_database = "yourcpanelusername_yourdatabase";





$connection = mysql_connect("$mysql_server","$mysql_user","$mysql_password") or die ("Unable to connect to MySQL server.");
$db = mysql_select_db("$mysql_database") or die ("Unable to select requested database.");


?>


Now We just create a new file named register.php and enter this html inside it and edit it to your liking:

Code
<html>

<head>

<title>EOTM</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link href="includes/in.css" rel="stylesheet" type="text/css">

<script language="JavaScript" type="text/JavaScript">

<!--

function MM_reloadPage(init) { //reloads the window if Nav4 resized

if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {

document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}

else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();

}

MM_reloadPage(true);

//-->

</script>

</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<!-- ImageReady Slices (a try.psd) -->

<table width="402" height="600" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">

<!--DWLayoutTable-->

<tr>

<td width="211" height="98">&nbsp;</td>

<td width="401">&nbsp;</td>

<td width="366">&nbsp;</td>

</tr>

<tr>

<td height="189">&nbsp;</td>

<td valign="top"><form name="form2" method="post" action="">

<table width="400" border="1" cellpadding="0" cellspacing="0" class=thinline rules=none>

<tr>

<td bgcolor="#FF0000"><div align="center"><font color=white>Register</font></div></td>

</tr>

<tr bgcolor=black>

<td height=1 colspan=3></td>

</tr>

<tr>

<td align="center" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="1">

<tr>

<td colspan="2"><div align="center"><font color=red>

<? echo "$message"; ?>

</font></div></td>

</tr>

<tr>

<td width="25%">Username:</td>

<td width="75%"><input style="background-color:#CD8500; color:#000000; border: 1px solid #555555;" name="reg_username" type="text" id="reg_username" value="" size="30" maxlength="40"></td>

</tr>

<tr>

<td width="25%">Password:</td>

<td width="75%"><input style="background-color:#CD8500; color:#000000; border: 1px solid #555555;" name="reg_password" type="password" id="reg_password" value="" size="30" maxlength="40"></td>

</tr>

<tr>

<td>Email address:</td>

<td><input style="background-color:#CD8500; color:#000000; border: 1px solid #555555;" name="email" type="text" id="username3" value="" size="30" ></td>

</tr>

<tr>

<td>Confirm email:</td>

<td><input style="background-color:#CD8500; color:#000000; border: 1px solid #555555;" name="email1" type="text" id="email" value="" size="30"></td>

</tr>
<tr>

<td>&nbsp;</td>

<td><input style="background-color:#CD8500; color:#000000; border: 1px solid #555555;" type="submit" name="Submit" value="Register"></td><br>

</tr>

</table></td>

</tr>

</table>

</form></td>

<td>&nbsp;</td>

</tr>

<tr>

<td height="14"></td>

<td valign="top"><form name="form1" method="post" action="">

</form></td>

<td></td>

</tr>

<tr>
<td colspan="2" valign="top" height="36"></td>
</tr>

<tr>

<td height="487"></td>

<td>&nbsp;</td>

<td></td>

</tr>

</table>

<!-- End ImageReady Slices -->

</body>

</html>



What we first do is see if they are already logged in:

Code
<?php
session_start(); /// Starts Session

include_once"includes/db_connect.php"; /// This file is included to connect to your database and is a must!

if (isset($_SESSION['username'])){ /// If the server detects they are logged in...

header("Location: newlocation.php"); /// Send them to the page where they are already logged in.

exit(); /// Dont include anymore of this script if theyre logged in.



if ($_POST['Submit']){ /// If they press submit



// Define post fields into simple variables

$reg_username = $_POST['reg_username']; /// Check what they typed for username box,

$reg_password = $_POST['reg_password']; /// password box,

$email = $_POST['email']; /// email box

$email1 = $_POST['email1']; /// Email confirmation box,


$reg_username=trim($reg_username);




$reg_username = stripslashes($reg_username);

$email = stripslashes($email);

$quote = stripslashes($quote);

$reg_username = strip_tags($reg_username);

$email = strip_tags($email);


if((!$reg_username) || (!$email))){ /// if the username box or the email box or the are empty...

$message="Fill in all fields"; /// Say this.

}else{

if ($email != $email1){ /// If email box and email confirmation box are not same then...

$message="Emails do not match"; /// Say this.

}elseif ($email == $email1){ /// If they are equal, carry on...

/// From Here you should know what these (if) commands do so i will stop explaining them and start explaining other stuff.



if (ereg('[^A-Za-z0-9]', $reg_username)) { $message="Your username can only contain letters and numbers.";

}elseif (!ereg('[^A-Za-z0-9]', $reg_username)) {

/// This line basically checks if your using certain characters in your username and doesnt allow them.



if (strlen($reg_username) <= 3 || strlen($reg_username) >= 20){

$message= "Username must be over 3 and under 20";

}elseif (strlen($reg_username) > 3 || strlen($reg_username) < 20){


/// This checks if your username is below 20 characters or above 3 characters and allows only that unless you change it.


$sql_email_check = mysql_query("SELECT email FROM users

WHERE email='$email'");

$sql_username_check = mysql_query("SELECT username FROM users

WHERE username='$reg_username'");

/// These check for users in the database which have the email the user specified or the username the user specified.

$email_check = mysql_num_rows($sql_email_check);

$username_check = mysql_num_rows($sql_username_check);

/// This checks how many people are currently using this email/username.


if(($email_check > 0) || ($username_check > 0)){

echo "Im sorry but there has been an error please read on..<br />";

/// If there is more than 0 people with that email/username then echo the above and then the following...

if($email_check > 0){

$message= "Your email address has already been used!";

unset($email);

}

if($username_check > 0){

$message="Your desired username is already in use!";

unset($reg_username);

}

$ip = $_SERVER['REMOTE_ADDR']; // Fetches their remote IP.

mysql_query("INSERT INTO `users` ( `id` , `username` , `password` , `email` , `r_ip` )

VALUES (

'','$reg_username','reg_password','$email','$ip'

)");

/// That basically just inserts the information into the mysql table. As you can see it uses the variables we used.

// Let's mail the user!

$subject = "Your Site (New Account)";

$message = "$reg_username,

Hello $reg_username , thank you for choosing (Your Site), your login information is below.

Login Information:

Username: $reg_username
Password: $reg_password

Thanks,
Owner



This Is An Automated Reply. Please Do NOT Reply!";



mail($email, $subject, $message,

"From: (Your Site) Welcome<youremail@hotmail.com>");

$message= 'Account created, You Can Now <a href=\'login.php\'>Login</a> ';

} }}}}} /// You might need to add or delete one of these since i havnt counted them but you should know if you get an error on this line.

?>


Now this should get your registration script running. If you have any questions, problems or comments please feel free to post them on syxe and
i will definitly reply to them quickly.
ilyas-shezad's Avatar
Views:
2,451
Rating:
Posted on Saturday 18th August 2007 at 10:03 AM
ilyas-shezad
ilyas-shezad's Avatar
wat do u mean?
Posted on Friday 17th August 2007 at 06:24 PM
gbt91
gbt91's Avatar
this is as rmb's