PHP Logger

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

Post Reply
shadowx360
Forum Newbie
Posts: 6
Joined: Sun Oct 10, 2010 3:53 pm

PHP Logger

Post by shadowx360 »

Greetings,

To practice the PHP I've learned in the last few weeks, a friend of mine as came up with an interesting challenge. The challenge is to have a constant monitoring of a certain web page using a server. The challenge details:
1) A destination, say, examplesite.com/PHP_challenge.php, contains a number that changes over time
2) Find this number, and log the time as well as the value into a file, log.txt
3) check every 30 minutes, and be able to stop when a file named stop.txt appears in the same directory as the script

I can do steps 1 and 2 without problem, but the main problem is checking every 30 minutes. (Please don't suggest that I make a C++ or javascript program, I can do it, but any client side programming relies on the client to be online all the time and have uninterrupted internet access 24/7, and the reason why there is a challenge is because I can't leave my computer online 24/7). At first, I thought I would put the script into an infinite loop that breaks when stop.txt exists, but then I found out about the restriction of runtime of PHP scripts. On the hosting I have, the maximum runtime of a PHP script is 60 seconds.

I came up with an idea that involves a basic loop between two pages. The idea is that the script (let's name it script1.php) opens the log.txt, finds out the last time it checked the site, if it's more than 30 minutes, it checks the site and writes the values to the file, but if it's less than 30 minutes, it sleeps for 45 seconds and then calls script2.php that sleeps for 45 seconds and then calls script1.php, creating a loop. In script1.php, it first checks to see if stop.txt exists, and if it does stop, or if not, repeat the cycle again. So, before I waste any more time writing code that doesn't work, may I ask if this idea would work? I can set the timeout of the CURL session to 45+ seconds (I know that the script will wait 45 seconds before loading, so I have to set the time limit of the CURL session higher to prevent any timeouts). And any other ideas that would cost less bandwidth and CPU time? I'm all ears :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Logger

Post by John Cartwright »

Short and sweet answer: see cron (*nix) / Task Scheduler (Windoze). Both utilities provide means to "call" a script depending on the time/intervals.
shadowx360
Forum Newbie
Posts: 6
Joined: Sun Oct 10, 2010 3:53 pm

Re: PHP Logger

Post by shadowx360 »

Well, it's on a PHP server, is cron available for use without admin access? I can't use system(). EDIT: Just asked the hosting company, cron jobs aren't available for free hosting accounts...

Also, would that idea I posted actually work?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Logger

Post by John Cartwright »

It's a bit silly to recursively call scripts. Your best bet would be to create a sleeper script, which would loop indefnitely and sleep until required. You would basically open up the page, and close it (and it will continue to run in the background on the server).

See ignore_user_abort(), set_time_limit(0), sleep()

I haven't had to do something like that in many years, and if memory recalls, it did involve cron to check to see if the process was running (incase it crashed), and needed to be restarted.

Basically, what your trying to do is not straightforward without the tools meant to do the job. I.e., Cron / Task Scheduler.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: PHP Logger

Post by cpetercarter »

If you are running on a Unix/Linux server, it is likely that you have access to Perl. Perl does not have a maximum execution time in the way that PHP does, and PHP can call Perl scripts in a cgi-bin directory. If you are interested, why not ask further in a Perl forum.
shadowx360
Forum Newbie
Posts: 6
Joined: Sun Oct 10, 2010 3:53 pm

Re: PHP Logger

Post by shadowx360 »

Ok, would this code actually work for my purpose, to close the HTTP request session from another PHP file and then continue doing whatever?

Code: Select all

ob_end_clean();
header("Connection: close");
ignore_user_abort();
ob_start();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();    
flush();   


I'll try to look into set_time_limit and see if my host will allow that. As for Perl, thanks for that suggestion, I'll be sure to keep it in mind, but for right now, learning just PHP is hard enough without trying to learn Perl too, lol.

Also, would forking work? pcntl_fork() ?
Post Reply