PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Tolarianacademy wrote:hi,
is possible to create some php pages which works also if no one open it?
something that automatically works.
thx
No problem! Normally PHP is used with web pages but that is not a requirement. PHP can do just about anything you need and you don't have tell it to send output to a web page. There are also tools available that let you build GUI interfaces to PHP so they function like normal GUI programs on a Mac, Windows, etc.
I explain better my problem:
I wont something that automatically check into the database a time value and if it is > than 30 minutes do something.
and it for all users.
so for each user read into the database the time value, check it and eventualy do something.
run a script every , say, 5 minutes
This script would
read a timestamp from the database
If this timestap was more that 30 minutes ago then
for each user in the database
do some action
end for
update timestamp int he database
end if
or do you mean something like this
run a script every , say, 5 minutes
This script would
for each user in the database
read the timestamp for this user
if this user's timestamp is more than 30 minutes old
do some action
update this user's timestamp
endif
end for
--- work out time parameters
$current_time = get current time
$old_time = $ current_time - 30 minutes
--- connect to MySQL server - assumes a MySQL database
$link = mysql_connect ($hostname, $username, $password) or die('Failed To Connect ' . mysqlerror());
--- select database to use
if (false == mysql_select_database ($database_name))
{
die ('Failed To Select Database ' . $database_name . ' ' . mysqlerror());
}
--- name of table where the usernames and tmestamps are held
$table_name = 'users';
$timestamp_column = 'user_timestamp';
$where_clause = 'timestamp_column < $old_time';
--- create select sql to get list of users for whom to run the required actions
$sql = SELECT userid, timestamp_column FROM $table_name WHERE $where_clause
$result = mysql_query($sql, $link);
if ($result == false)
{
die ('Failed To Run ' . $sql . ' ' . mysqlerror());
}
--- iterate over all the users returne
while ($row = mysql_fetch_assoc($result))
{
do some action for user $rowї'userid'];
}
--- cleanup
mysql_free_result($result);
--- update all those users for which we have just performed the action
$sql = UPDATE $table_name SET timestamp_column = $current_time WHERE $where_clause
$result = mysql_query($sql, $link);
if ($result == false)
{
die ('Failed To Run ' . $sql . ' ' . mysqlerror());
}
mysql_free_result($result);
mysql_close($link);
Automatic running of this script. For this to happen, you must ask your webhost if they will allow you to schedule a program to run at a particualr time or frequency. if they do, they will tell you all you need to know on how to set the job up. You should also explain that the job you want to run is writen in PHP so you will need access to the PHP CLI (Command Line Interpreter)
Once you have access to the PHP CLI and the scheduler, you can set up the job. Once it is set up, the server machine will automatically run the job at the scheduled time(s)