• (2 Pages)
  • +
  • 1
  • 2

Text Based Mafia Game || Usersonline/ Inside game Design part 4 We are going to look at the inside game design Rate Topic: ***** 1 Votes

#1 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

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

Posted 12 August 2010 - 06:55 PM

We are continuing from part 3 please make sure you have tried The Part 3 tutorial before trying this one.

OK this tutorial is going to be very lengthy and packed with much information. We will be new tricks and techniques to manipulate php to do what we want.

Safe.php
Safe.php is a page we are going to create to save our most request variables from the database like $name , $money , $rank etc etc. We are making safe so that we don't have to keep creating mysql query on every page to connect to users and get us information. It is like a safe that has valuables. Safe is also going to contain other information that we are going to use to check the last time the user is active on the website ( this information will be used on users online). We will also use to check if the user's session is active and valid (if not we log them out). In a more Advance use we are going to put little function which we will use when an user has certain amount of exp it will rank up their account and send them a message saying so(Which we will talk more about later). So in a way safe is really like a real safe with many valuables and information on an user which can be precious.

Let Begin.

Step One - Creating our safe. Create a safe page called Safe.php ( you can called it anything you wish) to really test our safe work we are going to use it on Sample.php which we created in the last tutorial.

If you remember on Sample we made a Logout link to destroy our session but if you were to type in Sample.php it will take you to the sample page and display the information without login. We want to make it co that if an user were to type in Sample.php without a valid session it will redirect them to the login page.

On Safe.php create a connection to the database and put it on line one.

<? include_once("connect.php"); ?>


On your Mysql database we need to add a field to the users to hold when last the were online and active. Add this field to the 'users' table in the database.
lastactive varchar(255) NOT NULL,


with the field added to the database add the following to capture the last active and also to check if a user have a valid session.

<?
if(isset($_SESSION['user_id'])) {
 // Login OK, update last active
 
 $sql = "UPDATE users SET lastactive=NOW() WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
 mysql_query($sql);
 
}else{
   header("Location: index.php");
     exit();
 }
?>


NOW() is a pre-built function to insert the current time and date. Back on Your Sample page add the following below on line one on top of any html code.
<? require("Safe.php"); ?>



If everything is done correctly and if you login on the sample page without login it will redirect you to the index page. Then if you login you can see on the database that last active is set with the date and time. Well Done you have just create the inside game. Now we have separated the login page to the inside game.

Step Two - Calling a Global Mysql Query
We are going to call a mysql query to go to the database and fetch the basic information on the user and it will be stored on safe when the session is valid, and on every page we create we are going to include Safe.php on the top so that the we can just type in like $name we out calling anything, because it has being done on Safe already. Add this on safe.php

$sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$name = htmlspecialchars($row->name);
$sitestate = htmlspecialchars($row->sitestate);
$password = htmlspecialchars($row->password);
$mail = htmlspecialchars($row->mail);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$rank = htmlspecialchars($row->rank);
$health = htmlspecialchars($row->health);
$points = htmlspecialchars($row->points);


that's all we need for Safe.php for now.

Left.php and Right.php


OK look at this image because this is the basic structure of the game
Posted Image

Where the links are is the Left.php and the player information is the right.php. On each page we create for the game like users online we are going to include both left and right.php.

Step One - Left.php
The reason why we have the links on different pages is because when we make changes to one link we dont have to go changing it on every page, this makes our life easier.

Create a page called Left.php. On that page we dont need to connect to database all we need to do is include Safe.php Like this.

<? include_once("Safe.php"); ?>


Then add this html that contain a table with a couple of rows. Look it over and understand it. This is where our links are going to be, to link players from one page to another.

<table width="0%" border="0" cellspacing="0" cellpadding="0" >
  <tr>
    <td align="left" valign="top" >
    
<table width="123" height="262" border="1" align="left">
  <tr>
    <td height="23">Main Game</td>
  </tr>
  <tr>
    <td height="74">Some Links here</td>
  </tr>
  <tr>
    <td height="23">Social</td>
  </tr>
  <tr>
    <td height="105">More links here</td>
  </tr>
  <tr>
    <td height="23">LogOut</td>
  </tr>
</table>
  <td width="100%" align="center" valign="top">
  



Don't worry the reason why we didn't close that table we opened is because we are going to close it on Right.php so when right and left are included on the same page the will act as one.

Thats all for Left.php for now. No links added and the only php is to connect to our safe.php. Now for our right.

Step Two - Right.php

Now this is the beauty part, because we have included Safe.php on Left.php we dont have to do it on Right.php, the reason for that is it is the both pages will be displayed at the same time on the page so information will get passed from left to right and from right to left. So with that in mind all you need on Right.php is

<td width="150" align="right" valign="top" >
<table width="135" border="1" align="right">
  <tr>
    <td>Players information</td>
  </tr>
  <tr>
    <td height="369">Players information here.</td>
  </tr>
  </table>
   </td></td>
  </tr>
</table>


The reason why we have a closed table tag here is because we opened it on Left.php dont worry is not a syntax error in the code. You will see the finish will be awesome you will see.

Usersonline.php
The Users online page allows us to see who is online playing the game. We are focusing more on the php assuming you know the html.

Step One - design
Create a page Called Usersonline.php and add the following html with php.

<? require("Left.php"); ?>
<html>
<head>
</head> 
<body>
<table width="90%" height="94" border="1">
  <tr>

    <td height="23" align="center">Users online</td>
  </tr>
  <tr>
    <td>Users list here</td>
  </tr>
</table>
</body>
</html>
<? require("Right.php"); ?>


Now if you run all our scripts and login and direct to Usersonline.php you should see thisPosted Image

Step Two
- PHP Code.

The php code for this is sample.Add the following code in the html code where it says User list here
<?php

$sql = "SELECT name FROM users WHERE DATE_SUB(NOW(),INTERVAL 5 MINUTE) <= lastactive ORDER BY id ASC"; //  Searches the database for every one who has being last active in the last 5 minute
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
$i = 1;
while($row = mysql_fetch_object($query)) {
 $online_name = htmlspecialchars($row->name);


  echo "$online_name."; // displays the names that fit our search
 
 if($i != $count) { // this counts the amount of people that are online and display the results.
  echo "<label> - </label>";
 }
 $i++;
}
echo "<p><center>Total Online: ".$count."</center></p>";
?>


the code above searches the database for everyone who lastactive has a 5 minute interval. Which means for every one who has clicked and updated their lastactive in the last 5 minutes.


Improvements.

Ok i am going to Run some improvements that you should to do to improve your game so far.
  • Add a users online link under the main game on Left.php Like shown below on the HTML CODE.
     <tr>
        <td align="left" valign="top" >
        
    <table width="123" height="262" border="1" align="left">
      <tr>
        <td height="23">Main Game</td>
      </tr>
      <tr>
        <td height="74" valign="top"><a href="Usersonline.php">Users online</a></td>
      </tr>
      <tr>
        <td height="23">Social</td>
      </tr>
      <tr>
        <td height="105">More links here</td>
      </tr>
      <tr>
        <td height="23">LogOut</td>
      </tr>
    </table>
      <td width="100%" align="center" valign="top">
    


  • On the index page redirect users to Usersonline instead of Sample.php


I know i said that we were going to do a mini forum in this part but there is too much information here already for you to play with. you an experiment with different ways of displaying your site. Find a way that works best for you and remember KISS (KEEP IT SIMPLE STUPID). I know you are egar to get on and be designing with fancy photoshop but this isn't the stage for that, as you can see for no wall that happens is someone can create an account and login to a users online page that tells them who else is online. In the next part we are going to start with a mini forum.

Please post any improvements of your own or if you see any errors / mistake. Thanks
See you in part 5


--------
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:03 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Text Based Mafia Game || Usersonline/ Inside game Design part 4

#2 Exosion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-August 10

Posted 17 August 2010 - 10:34 PM

Bravo.. more please! :)
Was This Post Helpful? 0
  • +
  • -

#3 Eetu  Icon User is offline

  • New D.I.C Head

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

Posted 11 September 2010 - 12:48 AM

I have a 2 Questions:

1: Is these settings right in PhpMyAdmin?

Posted Image
MAIL AND LASTACTIVE



Posted Image
ONLY MAIL SETTINGS!




Posted Image
ONLY LAST ACTIVE SETTINGS

Is those settings right?
If they got somethin wrong plz, tell me what I should replace!


2: When I have done that part when I go to localhost/sample.php it will lead me 2 index.php right away!
BUT NOW! When I login on index.php > Should go to sample.php! but it will lead to index.php right away!

Whats wrong?

Here's the code:
<?php
if(isset($_SESSION['user_id'])) {

// if already logged in.

session_unset();

session_destroy(); 

echo "You have been logged out.";
}
?>


And its in Index.php after the 1st PHP CODE

Is it in wrong file?


:) Waitin' for help some help



And u can help in that in msn 2 , thx u rock ;P
Was This Post Helpful? 0
  • +
  • -

#4 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:56 AM

View PostEetu, on 11 September 2010 - 06:48 AM, said:

I have a 2 Questions:

1: Is these settings right in PhpMyAdmin?

Posted Image
MAIL AND LASTACTIVE



Posted Image
ONLY MAIL SETTINGS!




Posted Image
ONLY LAST ACTIVE SETTINGS

Is those settings right?
If they got somethin wrong plz, tell me what I should replace!


2: When I have done that part when I go to localhost/sample.php it will lead me 2 index.php right away!
BUT NOW! When I login on index.php > Should go to sample.php! but it will lead to index.php right away!

Whats wrong?

Here's the code:
<?php
if(isset($_SESSION['user_id'])) {

// if already logged in.

session_unset();

session_destroy(); 

echo "You have been logged out.";
}
?>


And its in Index.php after the 1st PHP CODE

Is it in wrong file?


:) Waitin' for help some help



And u can help in that in msn 2 , thx u rock ;P


make sure you have added the safe.php on and you have full php tags. <?php ?>
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Anton*


Reputation:

Posted 19 September 2010 - 02:22 AM

Hello. thank you for the good tuts
all my things is correct.
i create a user in register and when i login i get redirectet to Index.php.
then i go into Safe.php and edit the Index.php to Register.php
}else{
header("Location: Index.php");
exit();

TO

}else{
header("Location: Register.php");
exit();

and then when i login i get Redirectet to Register.php :(
so i think the session doesnt start or something.
i upload all my files in a .RAR here
http://www.plunder.c...-21b7f170d9.htm
DOWNLOAD
i cant attach the file
Was This Post Helpful? 0

#6 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Posted 29 September 2010 - 02:34 AM

Hey denis great tutorials. Though with the safe thing i aleady have this in my games but they are called globals. Keep up the great work
EDIT: I also have my database connections iin a oop class so when i call them rs like
$db->query("SELECT u.* FROM uses u");

This post has been edited by aaron1178: 29 September 2010 - 02:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 TheMaster1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 10

Posted 03 January 2011 - 01:33 PM

I have been working on this game on a localhost which has been working fine, however when I use a real web host and when I try login into the game it won't log me in it will just send me back to index.php. I have came to the conclusion that the code doesn't seem to create a session when login and I can't understand why because on the login.php has:

$_SESSION['user_id'] = $row['id'];

which should create the session.

Have any clues why this is doing this?
Was This Post Helpful? 0
  • +
  • -

#8 TheMaster1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-October 10

Posted 04 January 2011 - 08:54 AM

View PostTheMaster1, on 03 January 2011 - 12:33 PM, said:

I have been working on this game on a localhost which has been working fine, however when I use a real web host and when I try login into the game it won't log me in it will just send me back to index.php. I have came to the conclusion that the code doesn't seem to create a session when login and I can't understand why because on the login.php has:

$_SESSION['user_id'] = $row['id'];

which should create the session.

Have any clues why this is doing this?

I found what the problem was and fixed the problem.

This post has been edited by TheMaster1: 04 January 2011 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_MrNice*


Reputation:

Posted 15 February 2011 - 09:58 PM

View PostTheMaster1, on 04 January 2011 - 08:54 AM, said:

View PostTheMaster1, on 03 January 2011 - 12:33 PM, said:

I have been working on this game on a localhost which has been working fine, however when I use a real web host and when I try login into the game it won't log me in it will just send me back to index.php. I have came to the conclusion that the code doesn't seem to create a session when login and I can't understand why because on the login.php has:

$_SESSION['user_id'] = $row['id'];

which should create the session.

Have any clues why this is doing this?

I found what the problem was and fixed the problem.

Was This Post Helpful? 0

#10 Klone1222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-July 11

Posted 15 July 2011 - 06:36 AM

View PostTheMaster1, on 04 January 2011 - 08:54 AM, said:

View PostTheMaster1, on 03 January 2011 - 12:33 PM, said:

I have been working on this game on a localhost which has been working fine, however when I use a real web host and when I try login into the game it won't log me in it will just send me back to index.php. I have came to the conclusion that the code doesn't seem to create a session when login and I can't understand why because on the login.php has:

$_SESSION['user_id'] = $row['id'];

which should create the session.

Have any clues why this is doing this?

I found what the problem was and fixed the problem.

How did you fix it? I'm having the same problem...
Was This Post Helpful? 0
  • +
  • -

#11 Easyslider01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-September 11

Posted 25 October 2011 - 04:34 PM

For some weird reason, when I add in the php code in Usersonline that shows other users online it doesn't work. It says that I haven't selected a database and the my right php doesn't show up.

Here is my code:
<?php require("left.php"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Users Online</title>
</head>

<body>

<table width="90%" height="94" border="1">
  <tr>

    <td height="23" align="center">Users online</td>
  </tr>
  <tr>
    	<td><?php

$sql = "SELECT name FROM users WHERE DATE_SUB(NOW(),INTERVAL 5 MINUTE) <= lastactive ORDER BY id ASC"; //  Searches the database for every one who has being last active in the last 5 minute
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
$i = 1;
while($row = mysql_fetch_object($query)) {
 $online_name = htmlspecialchars($row->name);


  echo "$online_name."; // displays the names that fit our search
 
 if($i != $count) { // this counts the amount of people that are online and display the results.
  echo "<label> - </label>";
 }
 $i++;
}
echo "<p><center>Total Online: ".$count."</center></p>";
?></td>
    </tr>
</table>

</body>
</html>

<?php require("Right.php"); ?>

Was This Post Helpful? 0
  • +
  • -

#12 Shadowing  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 26-November 11

Posted 02 December 2011 - 04:51 AM

Ya im having the same problem as all of these guys are having.
After making the Safe.php redirect to log in page it makes it where when I log in now it redirects me back to the log in page and doesnt take me to the sample page.

I notice on Part 3 that it adds the ip address to the database but it doesnt add the cookie file
I looked for it on IE8 cookies and it doesnt exist. So cause it doesnt create this session which i guess the session is the cookie?????
Im really clueless at this point but i will be looking into it and will post my findings to help out everyone that has made it this far into this crazy tutor haha. I still appreicate the tutor though :) just has alot of bugs in it.

But I have posted in every part saying how I fixed the bugs up to this point and I will continue to do so.
Was This Post Helpful? 0
  • +
  • -

#13 Shadowing  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 26-November 11

Posted 05 December 2011 - 06:56 AM

Alright everything worked just fine in this tutor. If anyone needs help in fixing any thing wrong with there script up to now I have made all the nessary fixes in step 3. So go back to tutor 3 and read what I wrote on how to fix the session issue and other things.

actually here is a link to it

http://www.dreaminco...tdesign-part-3/


see you all on step 5
Was This Post Helpful? 0
  • +
  • -

#14 Phantom Coder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 26-April 09

Posted 06 February 2012 - 07:13 AM

Hello everything seems to be working fine no errors but for some reason it wont update the lastactive field in the database.
Was This Post Helpful? 0
  • +
  • -

#15 Tgrooms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-April 12

Posted 12 April 2012 - 02:52 AM

anyone else getting double the tables when you click users online link?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2