• (3 Pages)
  • +
  • 1
  • 2
  • 3

Text Based Mafia Game || Register and Forgot pass Part 2. Creating the Register and Forgot pass on our Text Based Game Rate Topic: -----

#1 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 102
  • Joined: 29-July 09

Posted 04 August 2010 - 02:57 PM

Before you attempt this tutorial please make sure you have done PART 1.

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.

Posted Image


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.


--------
Mod: the code can be found here:
https://github.com/m...ext_Based_Mafia

This post has been edited by modi123_1: 08 January 2014 - 09:02 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Text Based Mafia Game || Register and Forgot pass Part 2.

#2 Eetu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-September 10

Posted 09 September 2010 - 10:33 PM

Im stuck in so stupid part , when we r addin' that mailvarch(100)NOT NULL to the DB
How I do it?

So I pressed structure, heres pic what I see:
Posted Image

Then what? button ADD? where? :o!
Help me again >.<
Was This Post Helpful? 0
  • +
  • -

#3 scott142  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 03-September 10

Posted 10 September 2010 - 02:30 AM

View PostEetu, on 09 September 2010 - 09:33 PM, said:

Im stuck in so stupid part , when we r addin' that mailvarch(100)NOT NULL to the DB
How I do it?

So I pressed structure, heres pic what I see:
Posted Image

Then what? button ADD? where? :o!
Help me again >.<

In the action area click the structure icon (the second icon)
then the add bit is at the bottom of the structure page and then that will add the field to your table.
Was This Post Helpful? 0
  • +
  • -

#4 Eetu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-September 10

Posted 10 September 2010 - 05:08 AM

I register :
Put name
email and pass and click REGISTER,
I get error about email
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\xampp\htdocs\register.php on line 84


Helpp? :o

This post has been edited by Eetu: 10 September 2010 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 102
  • Joined: 29-July 09

Posted 10 September 2010 - 06:58 AM

View PostEetu, on 10 September 2010 - 11:08 AM, said:

I register :
Put name
email and pass and click REGISTER,
I get error about email
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\xampp\htdocs\register.php on line 84


Helpp? :o


that is a problem on your part. Your XAMPP SMTP port is configured properly. Click here to read this topic to help you
Was This Post Helpful? 0
  • +
  • -

#6 Lightme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 06-March 10

Posted 11 September 2010 - 10:10 PM

Attempt 1:

Evrything now seems to work perfect. Except for one thing the password recovery script is not quite clear for me. First I add the code in a seperated file called pass.php the html and above that the code for the password generator. And the external function code directly under the lines of functions.php and put a link in register.php too pass.php and also a require line of functions.php in the pass.php this first attempt results in an blank-page.


Attempt 2:

For the first try dit not seems to work I tried too put evrything of the code of pass.php in one file. The one called pass.php if I try too load this one directly nothing happens just giving me a blank-page.

For the rest I am very thankfull you take the time to share this knowledge. I am learning hard by reading a php for dummies book (lol) and hope after this and your tutorial too come up with something of my own mind. And also looking forward for the solution.

This post has been edited by Lightme: 11 September 2010 - 10:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 102
  • Joined: 29-July 09

Posted 12 September 2010 - 02:51 AM

View PostLightme, on 12 September 2010 - 04:10 AM, said:

Attempt 1:

Evrything now seems to work perfect. Except for one thing the password recovery script is not quite clear for me. First I add the code in a seperated file called pass.php the html and above that the code for the password generator. And the external function code directly under the lines of functions.php and put a link in register.php too pass.php and also a require line of functions.php in the pass.php this first attempt results in an blank-page.


Attempt 2:

For the first try dit not seems to work I tried too put evrything of the code of pass.php in one file. The one called pass.php if I try too load this one directly nothing happens just giving me a blank-page.

For the rest I am very thankfull you take the time to share this knowledge. I am learning hard by reading a php for dummies book (lol) and hope after this and your tutorial too come up with something of my own mind. And also looking forward for the solution.


copy and paste your code so i can see how you structured it
Was This Post Helpful? 0
  • +
  • -

#8 Lightme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 06-March 10

Posted 12 September 2010 - 10:03 AM

Here is only attempt 2 because I removed the first try but its basicly the same code because I load in the code trough the require function: pass.php


<?php

 require 'functions.php';

if (isset($_POST['Register'])) {

$password = substr(md5($_SERVER['REMOTE_ADDR'].microtime().rand(1,100000)),0,6);
		// Generate a random password
}


?>

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


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

?>





Was This Post Helpful? 0
  • +
  • -

#9 Guest_Kelvin*


Reputation:

Posted 30 January 2011 - 01:45 AM

Hi:
I couldn't get the function.php to run. It keeps telling me syntax error, when I didn't change or touch any of the codes. It's frustrating. I've been stuck here for two days now. Someone please help
Was This Post Helpful? 0

#10 Roting  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 11

Posted 31 January 2011 - 12:45 PM

View PostKelvin, on 30 January 2011 - 01:45 AM, said:

Hi:
I couldn't get the function.php to run. It keeps telling me syntax error, when I didn't change or touch any of the codes. It's frustrating. I've been stuck here for two days now. Someone please help


What is the error ?
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Michael*


Reputation:

Posted 18 February 2011 - 05:57 AM

part 1 isnt work anymore ! this tutorial cant be done ! there are much of errors on this site !
Was This Post Helpful? -3

#12 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 102
  • Joined: 29-July 09

Posted 18 February 2011 - 01:12 PM

View PostMichael, on 18 February 2011 - 12:57 PM, said:

part 1 isnt work anymore ! this tutorial cant be done ! there are much of errors on this site !

if you would like to post the error message you are getting and on what page it will help us to help you.
Was This Post Helpful? 0
  • +
  • -

#13 kingfresh13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-July 11

Posted 18 July 2011 - 06:21 PM

Hello I get this error:

Parse error: syntax error, unexpected T_ECHO, expecting '(' in /home/p12r7526/public_html/lostpassword.php on line 31

Here are the lines in my code:

		
	
30	  if(empty($name)){ if // there is no name with the entered email
31		echo 'Invalid information.';
32	}else{
	


Can someone help me?
Was This Post Helpful? 0
  • +
  • -

#14 Roting  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 11

Posted 19 July 2011 - 01:09 AM

View Postkingfresh13, on 18 July 2011 - 06:21 PM, said:

Hello I get this error:

Parse error: syntax error, unexpected T_ECHO, expecting '(' in /home/p12r7526/public_html/lostpassword.php on line 31

Here are the lines in my code:

		
	
30	  if(empty($name)){ if // there is no name with the entered email
31		echo 'Invalid information.';
32	}else{
	


Can someone help me?


Why you have one IF after the first condition?
30 if(empty($name)){ if // there is no name with the entered email

It's cause you have the error ;)
Was This Post Helpful? 0
  • +
  • -

#15 kris123  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-September 11

Posted 24 September 2011 - 01:33 AM

here

if(empty($name)){ if // there is no name with the entered email
echo 'Invalid information.';


and this happens Parse error: syntax error, unexpected T_ECHO, expecting '(' in /home/mafiaass/public_html/lost_pass.php on line 45 this is line 45 help me pls
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3