[SOLVED] work without opening pages

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!

Moderator: General Moderators

Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

work without opening pages

Post by Tolarianacademy »

hi,
is possible to create some php pages which works also if no one open it?
something that automatically works.

thx :)
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

When does it work then?
I assume that you mean a regular program that you start without a webpage. Try searching for PHP CLI.
objectifier
Forum Newbie
Posts: 3
Joined: Wed Oct 20, 2004 1:24 pm

Re: work without opening pages

Post by objectifier »

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.
Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

Post by Tolarianacademy »

Thx a lot!

bye :)
Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

Post by Tolarianacademy »

Is possible to do something that, for exaple, starts every hour?

thx :)
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

Hi Tolarianacademy

Depends on what operating system you are running on.

If you are running on a *nix OS, then 'cron' is your man.

If you are running on Windoze, the you can set up a job via the task scheduler, or user the 'at' command.

If you are using another operating system, search the manuals.

Hope this helps

swdev
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Cron daemon in unix can be set to call any web page with various settings.

Handy for running php scripts online or locally to do some maintanance etc.
Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

Post by Tolarianacademy »

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.
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

do you mean you want something like

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
Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

Post by Tolarianacademy »

the second one! :D
and running every minute.

:)

every minute:
for each user
look at the database the timestamp
if > 30 minutes
do something
end if
end for
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

You must find out whether your host will

a) let you run automated tasks
b) let you run PHP from the command line as part of that task

assuming you can, then the logic you require goes something like

Code: Select all

--- 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)
&#123;
  die ('Failed To Run ' . $sql . ' ' . mysqlerror());
&#125;

--- iterate over all the users returne
while ($row = mysql_fetch_assoc($result))
&#123;
  do some action for user $row&#1111;'userid'];
&#125;

--- 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)
&#123;
  die ('Failed To Run ' . $sql . ' ' . mysqlerror());
&#125;

mysql_free_result($result);
mysql_close($link);
Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

Post by Tolarianacademy »

thank you very much.

for start the page every minute I have to use CLI?

thanx :)
Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

Post by Tolarianacademy »

I don't understand how culd I say to my server:

"1 minute is passed, please start to read {FILE}"...

thx :)
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

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)
Tolarianacademy
Forum Newbie
Posts: 8
Joined: Fri Oct 29, 2004 11:24 am

Post by Tolarianacademy »

ok, all clear.

thx a lot :)
Post Reply