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

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 this

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