Welcome to WebmasterWorld Guest from 220.127.116.11
joined:June 15, 2001
In this case we will use mysql to store the timestamp. on your php page include a file, lets say time.php within time.php we will compare the timestamp stored in the database to the current server time. If the server time is a day ahead of the time stored in the database we will carry out our daily task and update the timestamp in mysql.
This method will work simply because the site wil appear to be up to date when a user visits, but if will still be a day behind until a user hits the site.
This is indeed aa very quick and dirty way of doing it, and could be a problem for a site with high traffic simply due to the number of requests for time.php
You could make it slightlly more efficient by only including the time.php file if the current server time is within 1 hour of midnight. This is assuming you want to run a cron at midnight. for example. You could do this using a conditional if statement to compare current time without neeeding to query mysql.
Hope this gives you some ideas.
thank you Mack, you are a new Mod on this forum right? :P I didn't see you before among others.
Anyway, I already knew the suggested method but that is not going to solve my problem. I am handling with a big linux based software which works like google analytics, may be better than that :), and the cronjobs are used for data filtering tasks where the number of records are above 1000,000 records. I am making a web based installation process for this software where the script is run as user NOBODY.
I was looking for a built-in function in PHP with which I could setup cronjobs. But I think even with Shell scripting it will be a pain-in-neck, as I would need root permissions to run the crontab otherwise it might create problems. So what I will do is to put directions for installer to follow for setting up cronjobs on the servers rather than handling that part from the web interface.
Try this ->
<?php //Name it index.php
$socket = fsockopen($_SERVER['HTTP_HOST'],80,$errorno,$errorstr,10);
$socketdata = “GET /cron.php HTTP 1.1\r\nHost: “.$_SERVER['HTTP_HOST'].”\r\nConnection: Close\r\n\r\n”;
<?php //Name it cron.php
$time = strtotime("now");
$handle = fopen('$time.txt','w');
To prove it, run index.php and close
your browser and will see cron.php
creating a text file every 10 seconds.
The only way to stop this cron is by
stopping the server (Apache).
Or if you use windows doing a taskkill.
Or forcing an exit() via a code within cron.php.
You can activate the cron remotely and
security wise is very dangerous unless
you use a hash!.
This is a cron with NO cron program and
Try it, it works
(Apache, php 5.,).
It appears to be a php bug, however, to me
is indeed a wonderful feature.
One other use of such technique could be in mailing list to start sending email sender script and then close browser. mostly in mailing list you cannot close the browser or disturb internet connection otherwise email sending breaks. so you can use this technique like a linux job running in the background.
very cool :)
Glad you liked it.
Adding to the post, the only requirement is that
php.ini has to have max_execution_time = 0
You can get your actual max_execution_time with
And set it to 0 ->
any call to exit();
will stop the cron so you can safely use
return; break; etc.
Have tried it for 24 hours and have not had any problems
I want to use PHP to set a cronjob on server. is it possible with PHP
I have a couple of hundred such scripts that run from cron. Why wouldn't they?
You can either do
/usr/bin/php -1 <script>
or make the script executable and put #!/usr/bin/php -q in the first line.