FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Logging in a php game





snowboardalliance
I am working on a php rpg type game. I want to add logging so I can keep track of things. Basically it will show all transactions, log ins, battles, etc. The question is, should I log everything to mysql or files? I think the logs might get too big for mysql, but files might write slower. Would it make sense to log to mysql, and then back it up to a file every night? And if so, how would I do this back up? Thanks.
ock1991
You're attempting a RPG type game by php should mean you know MySQL well. Inform me if i'm wrong though.

Basically you would, every night, have a script list all values from your log table, or do this during a daily/weekly/occasional maintainence. Listing huge numbers of values would take quite some time.

You might like to have separate tables for different actions (e.g. tades, battles, chats) that you can organize easily.
snowboardalliance
I know mysql pretty well, but I don't know as much about file read/writes.

ock1991 wrote:
You might like to have separate tables for different actions (e.g. tades, battles, chats) that you can organize easily.


I was thinking of doing that.
But really, the reason I'm asking, is what is the best way to back everything up and clear mysql, but not lose any information? Like if something is written during the back up, and not backed up? Should I just read it all, write it to a file, and figure out the last record, deleting from there? Like "DELETE FROM logs WHERE id <= $high_id;".

Also, If I log it in xml, like:
Code:
<entry>
<id>1231</id>
<time>1233454354</time>
<action>log in</action>
<ip>123.33.4.124</ip
</entry>


Is it easy to read from the file later? How can I put it in a file to easily read from it later?

Thanks
SlowWalkere
How you store the information and how you get it back is entirely up to you... =)

If you store it as xml, then you can retrieve it using php's built in xml functions. I'm not very familiar with them, though, so I couldn't help you there.

The other alternative is take the mysql table and convert it back into some kind of delimited file - either a \t or , delimited file would work.

To do this, you first get the result of the row from mysql using a standard "SELECT *" query. Now you need to cycle through the results and convert each result-array into a single string.

Let's say you have four fields in your table, field1, field2, field3, field4.

For each row of the mysql result, you would do this...
Code:
$line = $result['field1'] . "," . $result['field2'] . "," . $result['field3'] . "," . $result['field4'] . "\n";


Then, write that line to the file.

When you want to get the results back, you would read the file using file(), which will conveniently put it into an array where each array element is one line from the file (or, one row from our old table). file() leaves the '\n' character at the end of the line, so use rtrim() to get rid of it.

Then, you need to take the line and convert it back into an array of the table fields. You'll probably want to write a function to do this for you, but it's pretty simple.
Code:
$temp = explode(",", $line);
$table['field1'] = $temp[0];
$table['field2'] = $temp[1];
$table['field3'] = $temp[2];
$table['field4'] = $temp[3];


Now, the line from the file is back into an array as if it were pulled out of your mysql table.

Anyhow, that's the general gist of it. Check out php.net for the syntax for fopen() (to open the file), fwrite() (to write to the file), and file() (to get the contents into the array). Hope that helps.

Oh, and the other convenient thing about writing the backup to a comma-delimited file like this is that you can import the whole file back into mysql using phpmyadmin, instead of parsing it through php and putting it back into the database. You'd only need to use the php function to read the file if you wanted to handle the old data without putting it back into your database.

- Walkere
snowboardalliance
Ok, one more thing. Can I create the file with php? If I do like weekly back ups, I want it automated, probably in separate files. What is the best way to do this? Just a blank file and use the copy() function to make a new file?
SlowWalkere
Yes, you can create the files in php. When you use fopen to open a file that doesn't exist, a new file with that name should be created. If you want to create a new file each time, add the date at the end of the filename (i.e. backup082806). You might have to play with permissions to make sure it works properly, though.

You can set it to run automatically once you have the script working properly. Simply go to DirectAdmin and set up a cronjob that runs the backup script every so often (once a day, week, month, whatever you want).

- Walkere
Rhysige
If your happy with writing to a file then ignore me Razz but I would suggest that the best way to deal with mass amounts of data is to migrate it from one table to another, I know the online RPG im involved in (Star Wars Combine) migrates all events to the archive table every.. week I think This means the main table isnt searching thousands of rows when its recent but you can still quickly access old data.

EDIT: Obviously for this you just write up a PHP file that is run via cron and have that do it all for you.
snowboardalliance
Rhysige wrote:
If your happy with writing to a file then ignore me Razz but I would suggest that the best way to deal with mass amounts of data is to migrate it from one table to another, I know the online RPG im involved in (Star Wars Combine) migrates all events to the archive table every.. week I think This means the main table isnt searching thousands of rows when its recent but you can still quickly access old data.

EDIT: Obviously for this you just write up a PHP file that is run via cron and have that do it all for you.


I guess that could work, do you think I could get any size issues (taking up too much space)?
coreymanshack
automated is with cron jobs...and yes use mysql.... writing and reading from files is dangerous.... and please stop by my website http://www.php-gamers.com/ and tell us about your game! We will even test it for you!
SlowWalkere
Using files is not dangerous if you know what you are doing. And if you don't know what you're doing, programming anything that handles data is dangerous.

Migrating the information to another table is a perfectly acceptable way to ease stress on the main table. And it partially achieves the goal of backing up the data.

However, it doesn't fully protect the data because if something happens to the whole mySQL database the data is shot. Saving the information to a file and downloading it to a separate location will protect against any mySQL crashes, sql injection attacks, etc, however unlikely that may be.

Also, backing the information to a file and moving it off site frees up space on the server.

The only downside that files have is that they are slower to access repeatedly because they are not organized for rapid search and access. If you're goal is to copy data to a backup, though, and only access it rarely, they are a perfect solution.

- Walkere
Rhysige
True it all depends on the one key factor, how often do you want to access the data stored in these archives? in the game I develop for we access the data reasonably frequently considering so its best to have them in the database, we also do a full database backup every night to allow for roll backs incase of errors etc.
snowboardalliance
SlowWalkere wrote:
However, it doesn't fully protect the data because if something happens to the whole mySQL database the data is shot. Saving the information to a file and downloading it to a separate location will protect against any mySQL crashes, sql injection attacks, etc, however unlikely that may be.


I automate SQL backups on all my databases through email already, so just using an archive table will work. Thanks everyone.
Related topics
Game Server For Frihost?_? Lol..
Ever made one?
hi !
[php]putting script fragments together?
My online php game
[JS] onclick to print out info
Does anyone know a really cool php game?
Inutile mais rigoureusement indispensable :)
the best car game ever???
Great games to play
Creating a page with php
Online game, anyone?
PHP Gaming?
PHP Game..
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.