Hello, At this point we have established a connection to your database and we have tested it all in the previous tutorial. In this tutorial we are going to Create 3 pages (Register.php and Forgot_pass.php) and an extra optional one (tos.php Which is a term of service.). You dont need to have one in your game but it will help if you do.
The reason why the login is pushed to the next tutorial is because there is a lot of information to take in with this tutorial, and login have a lot of cookies and sessions etc.
Register.php
Registration is very important in your game, it is a way for users to create their account to sign in and play your game. Throughout this tutorial we will not be focusing on the design aspect of the pages. In other words we will not be designing any fancy photoshop design layout with amazing colours etc etc. When you have completed this tutorial you can choose to do so on your own. (Remember i am helping you to build the basic blocks of setting up a game and getting it running.)
Registration can change the mind of a player who is signing up to you game, with that in mind there are three things you must remember.
- 1) KISS = Keep It Simple Stupid. - DO NOT try to over complicate things it will just confuse new players and they will get annoyed and leave. so KISS
- 2) Clear and understanding - Make it clear to understand. when it comes to rules it has to be clear that the player see and understand them. If you are giving any new items or packages or promotions to new users make sure you add this on the page and make it clear it will help to persuade new users in signing up.
- 3) Straight Forward. Do not add no hidden fees, or forms. everything that will be available to them must be made clear and simple.
Thats the main things to keep in mind when designing pages throughout the game.
Step One- connect the page to the database
Create a page and call it Register.php
With every page that you create we will need to connect it to the database.(we will use it less once we get in to creating the inside game.
add the following code on line 1 of Register.php before any html.
<?php include_once("connect.php"); ?>
Step Two - Functions
I was thinking we do this later but it is important we do it now. Functions are just a small piece of code that can be executed whenever we need it it saves time and space in stead of writing endless line of codes functions can be used to simplified things. Two main types of function
- PHP Built-in Functions
- User Built Functions
We are going to be making our own functions, so it can make things easy for us. For this to work well we need a whole page dedicated to functions,
Create a Page called functions.php
Add the following two functions.
<?
function checkEmail($str) // This function is going to help us filter out bad email from the good one and it makes sure the email enter is in the format of as@as.com
{
return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}
function send_mail($from,$to,$subject,$body)// this is a send email function that will help us send email to the registered users.
{
$headers = '';
$headers .= "From: $from\n";
$headers .= "Reply-to: $from\n";
$headers .= "Return-Path: $from\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "@" . $_SERVER['SERVER_NAME'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Date: " . date('r', time()) . "\n";
mail($to,$subject,$body,$headers);
}
?>
Dont worry much about this we are going to use them on the register page and i will point them out when it comes.
If you want to learn more on functions check out some tutorials. Make sure this is done in-order to gain full understanding of functions
Under the <?php include_once("connect.php"); ?> add this, again make sure there is no html above them.
<? require 'functions.php'; ?>
Step Three - The registration form
Before we continue i have realized that we didn't add an email field in the database.
so you need to add this to the mysql.
mail varchar(100) NOT NULL default '',
if you are using phpmyadmin, click on structure in the table view and scroll down to where it says add.
fill out the form to add 1 form field ad the end of the table.
then fill out the insert form with the values above.
MAKE SURE THIS IS DONE OTHERWISE THE DATA WONT BE INSERTED IN TO THE DATABASE.
In this tutorial we will be creating 4 form items.
- Username
- Password
- Email
- Accept Terms of Services
On Register.php add the following in the body part of the html
<form method="post" >
<center>
<h1><strong>Register</strong></h1>
<p>UserName:
<input type="text" name="Username" id="Username">
</p>
<p>Password:
<input type="password" name="Password" id="Password">
</p>
<p>Email:
<input type="text" name="Email" id="Email">
</p>
<p>
<input type="checkbox" name="Agree" id="Agree">
<br>
<input type="submit" name="Register" id="Register" value="Register">
</p>
</center>
</form>
the checkbox is so that members can agree to your rules (This is optional)
Step Four- PHP code
The php code is going to be similar to the one we created in the last tutorial. We are going to add 3 piece of data to the database table.
Add this code on top of the html you just add. make sure it is in the body section.
<?
if (isset($_POST['Register'])) {
if(strlen($_POST['Username'])<3 || strlen($_POST['Username'])>32)
{ // This check the characters of the username and it makes sure if it is longer that 3 letters.
echo 'Your name must be between 3 and 32 characters!';
}else{
if(empty($_POST['Password'])){ // This checks the password field to see if it is empty
echo 'You need to select a password!';
}else{
if(preg_match('/[^a-z0-9\-\_\.]+/i',$_POST['Username']))
{// this checks the user for any symbols space etc .You can remove this is you choose
echo 'Your name contains invalid characters!';
}else{
if(!checkEmail($_POST['Email']))
{ // this is one of the functions we added on the function page. for this to work make sure the function is required on this page
echo 'Your email is not valid!';
}else{
$password = md5($_POST['Password']); // this is a md5 hash. its encrypt your password so it isnt easily hackable
// The id is blank because it is an auto_increment which mean it will auto add a value to every user and the are all different. this is mainly so we dont have duplicate.
$sql = "INSERT INTO users SET id = '', name = '".$_POST['Username']."' , password= '$password', mail= '".$_POST["Email"]."'";
$res = mysql_query($sql);
echo "".$_POST['Username'].", Welcome to the game.";
}
}
}
}
}
?>
I know there is a lot on this page. I have tried to explain the new bits in the php. if you dont understand any part of it please post below and i will explain it more.
The code above has some basic validations to stop accounts with bad names/ wrong email being made. and it helps with security.
Step Five - Account validations / welcome email.
Account validations is done so that we done have duplicated account user name or duplicated email etc.
Replace all the code you have just added with the one below.
<?
if (isset($_POST['Register'])) {
if(strlen($_POST['Username'])<3 || strlen($_POST['Username'])>32)
{ // This check the characters of the username and it makes sure if it is longer that 3 letters.
echo 'Your name must be between 3 and 32 characters!';
}else{
if(empty($_POST['Password'])){ // This checks the password field to see if it is empty
echo 'You need to select a password!';
}else{
if(preg_match('/[^a-z0-9\-\_\.]+/i',$_POST['Username']))
{// this checks the user for any symbols space etc .You can remove this is you choose
echo 'Your name contains invalid characters!';
}else{
if(!checkEmail($_POST['Email']))
{ // this is one of the functions we added on the function page. for this to work make sure the function is required on this page
echo 'Your email is not valid!';
}else{
if(empty($_POST['Agree'])){ // Check if the Checkbox is checked to agree with the terms of services
echo "You need to accept the Terms & conditions in order to sign up.!";
}else{
// this check and makes sure that their are no duplication with the email
$sql = "SELECT id FROM users WHERE mail='".mysql_real_escape_string($_POST['Email'])."'";
$query = mysql_query($sql) or die(mysql_error());
$m_count = mysql_num_rows($query);
if($m_count >= "1"){
echo 'This email has already been used.!';
}else{
// this makes sure that all the uses that sign up have their own names
$sql = "SELECT id FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'";
$query = mysql_query($sql) or die(mysql_error());
$m_count = mysql_num_rows($query);
if($m_count >= "1"){
echo 'This name has already been used.!';
}else{
$password = md5($_POST['Password']); // this is a md5 hash. its encrypt your password so it isnt easily hackable
// The id is blank because it is an auto_increment which mean it will auto add a value to every user and the are all different. this is mainly so we dont have dupilcate.
$sql = "INSERT INTO users SET id = '', name = '".$_POST['Username']."' , password= '$password', mail= '".$_POST["Email"]."'";
$res = mysql_query($sql);
$to = $_POST['Email'];
$from = "no-reply@Game.co.uk";
$subject = "Registration - Your Registration Details";
$message = "<html>
<body background=\"#4B4B4B\">
<h1>Game Registration Details</h1>
Dear ".$_POST['Username'].", <br>
<center>
Your Username: ".$_POST['Username']."<p>
Your Password: ".$_POST['Password']."<p>
<p>
<font size=3> You recived this mail because someone used this mail to sign up to a game</font>
</body>
</html>";
$headers = "From: Game Registration Details <no-reply@Game.co.uk>\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
echo "".$_POST['Username'].", Welcome to the game.";
}}}
}
}
}
}
}
?>
Again i have added comments in the php to explain what is going. This will also send a welcome email to the user.
Thats about it on the register page i have added a screen shot to show you mine.

Lost_Pass.php
On this page we are going to have a form that sends us a copy of their password if the forgot it.
Step One - HTML
The html part is straight forward and easy.
<form method="post" >
<center>
<h1><strong>Lost Password</strong></h1>
<p>Email:
<input type="text" name="Email" id="Email">
<br>
<input type="submit" name="Send" id="Send" value="Send">
</p>
</center>
</form>
Step Two - Generating a password.
The logic behind sending users their pass isn't that complex. Since we used md5 hash unfortunately we will not be able to reverse it so we are going to generate a new password and send one to them in the email and replace other in that database so the can login
Add the following above the html you just added
<?
if (isset($_POST['Register'])) {
$password = substr(md5($_SERVER['REMOTE_ADDR'].microtime().rand(1,100000)),0,6);
// Generate a random password
}
?>
Step Three - The Send Mail Function.
This is where function can be helpful. Remember we have our function page well that's good because now instead of writing out the send mail function we can just write it in one line.
Expand you current php code with the one below.
<?
if (isset($_POST['Send'])) {
$password = substr(md5($_SERVER['REMOTE_ADDR'].microtime().rand(1,100000)),0,6);
// Generate a random password
$nsql = "SELECT * FROM login WHERE mail='".mysql_real_escape_string($_POST['Email'])."'";
$query = mysql_query($nsql) or die(mysql_error());
$row = mysql_fetch_object($query);
$name = htmlspecialchars($row->name);
$pass = htmlspecialchars($row->pass);
$mail = htmlspecialchars($row->email);
if((empty($_POST['Email']))){ // if the email field is empty there will be an error
echo 'You one field empty.';
}else{
if(empty($name)){ if // there is no name with the entered email
echo 'Invalid information.';
}else{
if($_POST['Email'] != $mail){
echo 'Invalid information.'; // if their is no match in the email
}else{
if(!checkEmail($_POST['Email'])){ // the checkEmail function we have in our function that saves us time and sapce
echo 'Your email is not valid!';
}else{
$result = mysql_query("UPDATE users SET password='$password' WHERE name='" .mysql_real_escape_string($name). "'")
or die(mysql_error());
$to = $_POST['Email'];
$from = "no-reply@Game.co.uk";
$subject = "Registration - Your Registration Details";
$message = "<html>
<body background=\"#4B4B4B\">
<h1>Game Registration Details</h1>
Dear $name, <br>
<center>
Your Username: $name <p>
Your Password: $password <p>
</body>
</html>";
$headers = "From: Game Lost Details <no-reply@Game.co.uk>\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
echo 'We sent you an email with your Details!';
}
}
}// check if name is unused.
}// check if accepted to the tos.
}// name check.
// if post register.
?>
I have done some validations and i have explain what each do on each stage.
Thats Then end of this tutorial. To improve on this you can try adding a link between the two pages which makes easier browsing
If you find any improvements / mistakes/ error in this tutorials please post below and i will correct them.
In the next part we will create the login and start to make the inside game. things get a bit complex so please make sure you knowledge of php is enough to understand what i am talking about. SEE you then.
This post has been edited by Denis1: 10 August 2010 - 03:39 AM






MultiQuote






|