TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-25-2010, 01:03 PM   #1 (permalink)
The Visitor
 
Join Date: Feb 2010
Posts: 2
Thanks: 3
Arbaces is on a distinguished road
Default scheduling the server to do updates/jobs

Hello,

<offtopic>This community looks great and its my first topic so I should at least say I entered here because I've recently started to become more and more into web programming and decided to create an online game. Right now I'm digging all around the web to find things to start with. I have basic PHP & MySQL knowledge (and good Java and C), but that's why I want to learn along the way as I'm making something..

So, what I'm trying to make is a game similar to Cybernations, Travian and Hattrick, with ideas and concepts from many other games but brought online, in-browser.</offtopic>


The question that bugs me lately is: how will I make the server update itself (scheduled updates and scheduled script running, say from 10 to 10 hrs or something like it).

For example, as an exercise - say I'm making a page which I want from 5 to 5 minutes to update itself with some text (automatically, by the server). for ex:

i upload the page on a free hosting service with php and mysql - like FreeHostia
now the page is blank ..
//after ~12 minutes it shows up:
5 minutes passed! update [1]
5 minutes passed!

I hope you get it, I want to know how can I configure a server / a hosting service / everything , to run scripts on a scheduled time (scripts to update a db, process some algorithms etc). I need something I can start studying on.


Thanks in advance
-Arbaces
Arbaces is offline  
Reply With Quote
Old 02-25-2010, 04:14 PM   #2 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

You need to look into Cron Jobs I think.....
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
The Following User Says Thank You to Hightower For This Useful Post:
Arbaces (02-25-2010)
Old 02-25-2010, 04:14 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Arbaces View Post
Hello,

<offtopic>This community looks great and its my first topic so I should at least say I entered here because I've recently started to become more and more into web programming and decided to create an online game. Right now I'm digging all around the web to find things to start with. I have basic PHP & MySQL knowledge (and good Java and C), but that's why I want to learn along the way as I'm making something..

So, what I'm trying to make is a game similar to Cybernations, Travian and Hattrick, with ideas and concepts from many other games but brought online, in-browser.</offtopic>


The question that bugs me lately is: how will I make the server update itself (scheduled updates and scheduled script running, say from 10 to 10 hrs or something like it).

For example, as an exercise - say I'm making a page which I want from 5 to 5 minutes to update itself with some text (automatically, by the server). for ex:

i upload the page on a free hosting service with php and mysql - like FreeHostia
now the page is blank ..
//after ~12 minutes it shows up:
5 minutes passed! update [1]
5 minutes passed!

I hope you get it, I want to know how can I configure a server / a hosting service / everything , to run scripts on a scheduled time (scripts to update a db, process some algorithms etc). I need something I can start studying on.


Thanks in advance
-Arbaces
If you are on a linux server, Cron Jobs is what you are looking for. If you are on windows, Task Scheduler.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Arbaces (02-25-2010)
Old 02-25-2010, 08:14 PM   #4 (permalink)
The Visitor
 
Join Date: Feb 2010
Posts: 2
Thanks: 3
Arbaces is on a distinguished road
Default

Yay! Cron Jobs got it done for me, found a nice service setcronjob.com which I can use to schedule updates on my free hosting server :) Thanks.
Arbaces is offline  
Reply With Quote
Old 02-28-2010, 07:55 PM   #5 (permalink)
The Wanderer
 
Join Date: Aug 2009
Posts: 17
Thanks: 0
Rhinos is on a distinguished road
Smile Alternative 5 minute cron job

if you are going to use a 5 minute cron job, make sure it is not going to start taking a long time when the amount of data you have increases.

If the 5 minute cron is updating members then you should consider an alternative way:

On your members table (or a OneToOne table for the members table; members_resets) add a new field named something like: last_5min_reset with a type of unsigned int(10).

When a member is first registered set this to the current time using the time() function.

In your header.php or the place where your code is run before the action code you will need the following code:

PHP Code:
<?php
// If the time from the last reset is greater than 5 minutes
if ((time() - $stat['last_5min_reset']) > 300)
{
    
$num_resets floor((time() - $stat['last_5min_reset']) / 300);

    for (
$i $num_resets$i 0; --$i)
    {
        
// perform member updates
        
$stat['energy'] += 0.5;
    }

    
$stat['last_5min_reset'] = time() - ((time() - $stat['last_5min_reset']) - ($num_resets 300));

    
// Make the updates, or if you do other things to the $stat variable then update it further down the code so that there is only 1 update query for the user
    
mysql_query('UPDATE members SET energy = ' (int) $stat['energy'] . ', last_5min_reset = ' . (int) $stat['last_5min_reset'] . ' WHERE id = ' $stat['id'] . ' LIMIT 1;');
}
?>
What this basically does is only perform the reset when the member logs in and whenever they make subsequent page requests whilst logged in. It is more efficient because it means there is not a script running every 5 minutes updating however many members there are.

I have found that I need to run this script before fights/battles in order to make it seem like the defending member is up to date with the resets. So I simply delegated the functionality to a function and made it possible to be used for any member and then updated the appropriate member that the function was called for.

If this isn't what you wanted I'm sure it is what someone else wanted and therefore anyone who may come across this now or in the future should find it useful.
Rhinos is offline  
Reply With Quote
The Following User Says Thank You to Rhinos For This Useful Post:
Arbaces (03-01-2010)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP socket based communication with a python server tripy Advanced PHP Programming 7 07-15-2009 08:20 PM
MySQL Server error #2003(HY000): Can't connect to MySQL Server on 'localhost'(10061) Yoosha MySQL & Databases 3 06-15-2009 02:45 PM
ldap server Gibou Advanced PHP Programming 4 07-10-2008 10:38 AM
Running a Server CMellor The Lounge 9 12-31-2007 10:15 PM


All times are GMT. The time now is 01:40 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design