Can anyone help me with a bit of php? I need to append the copntents of one file on to another when a web page is accessed. Cron is no use as the timing isn't fixed. I can append a line of text so far, but not the contents of a file, and as I'm away for the rest of the week and have been doing this manually till I find the fix, I'm panicking! - - help! Cheers, Jenny.
On Mon, Feb 18, 2002 at 01:12:54PM +0000, Jenny_Hopkins@toby-churchill.com wrote:
Can anyone help me with a bit of php? I need to append the copntents of one file on to another when a web page is accessed. Cron is no use as the timing isn't fixed. I can append a line of text so far, but not the contents of a file, and as I'm away for the rest of the week and have been doing this manually till I find the fix, I'm panicking! - - help! Cheers, Jenny.
Hrm, can't you open both files, concat them in memory, or to a seperate file, and then splat it to theright place? is it just printing the contents of the 2 files, in essence?
if so:
$bigfile = join(file("filename1"), file("filename2")); print $bigfile;
That make sense? (sorry, had to look that up, so not tested at all ;)
Cheers,
Brett
Hi Jenny,
On 18-Feb-02 Jenny_Hopkins@toby-churchill.com wrote:
Can anyone help me with a bit of php? I need to append the copntents of one file on to another when a web page is accessed. Cron is no use as the timing isn't fixed. I can append a line of text so far, but not the contents of a file, and as I'm away for the rest of the week and have been doing this manually till I find the fix, I'm panicking!
- help!
Cheers, Jenny.
1. One way to do something in Linux/Unix when something else happens is to run a little script in the background which watches for the event and then takes action. The logic is:
while true ; do if <test for occurrence of event> ; then <do action> fi sleep <some seconds> done
This, every <some seconds>, checks whether the event has occurred and, if it has, takes action.
2. To append one file to another, do
cat appended_file >> other_file
3. I don't know how one "registers" an access to a web page within Linux. Suppose, however, that the access updates some file called "acces_log". Then the script in (1) could be
touch watch_access_log while true ; do if [ access_log -nt watch_access_log ] ; then cat appended_file >> other_file fi sleep 10 touch watch_access_log done
This works by (a) updating the time-stamp on watch_access_log at startup (creating it if it doesn't exist); (b) going into an infinite loop; every time round, it checks whether access_log's time-stamp is newer than ("-nt") watch_access_log's time-stamp (which will happen if access_log has been changed since last time the file watch_access_log was touched); if so, then (c) it does the append; (d) then, in any case, it touches watch_access_log so that its time-stamp carries the latest time the loop was run, waits 10 seconds, then re-runs the loop.
Suppose the script is called "watch_web_page". Then you start it up with
watch_web_page &
Hope this gives you some useful ideas, even if it isn't exactly what's needed! Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) Ted.Harding@nessie.mcc.ac.uk Fax-to-email: +44 (0)870 167 1972 Date: 18-Feb-02 Time: 13:51:07 ------------------------------ XFMail ------------------------------
On 18-Feb-02 Ted Harding wrote:
Hi Jenny,
- I don't know how one "registers" an access to a web page
within Linux. Suppose, however, that the access updates some file called "acces_log". Then the script in (1) could be
touch watch_access_log while true ; do if [ access_log -nt watch_access_log ] ; then cat appended_file >> other_file fi sleep 10 touch watch_access_log done
It occurs to me that, while you can set the "sleep" to anything you like down to 1 second (but you can't set it smaller), you might have two or more web-page accesses during the sleep period; and you wanted to do the append for every access.
The only way I can see how to get round that would be for the web-access to trigger a signal to another program which then immediately did the append. I don't know how you would arrange for the signal to be sent. However, you can start up a shell which can detect signals by using the "trap" command in bash, on the lines of the (outline) script
#!/bin/bash trap "command_to_append_file" SIGINT while true; do <nothing_much> ; done
For example, try the following little script (call it "temp"):
#!/bin/bash trap "ls -l" SIGINT while true ; do true > /dev/null ; done
and then
chmod 755 temp
1. Make sure you have another window from which you can do
killall -15 temp
Then start temp with the command
temp
Now keep trying to interrupt it with ^C -- each time, you will get the output of "ls -l". When you're happy about that, kill it as above from the other window.
To make it work in the background, do
temp &
and now, from anywhere, you can
killall -2 temp
which will send it the SIGINT signal, resulting in a prompt "ls -l"; and this will continue until you "killall -15 temp".
You can use this mechanism instead of the "watch" loop in my other mail provided you have a way of getting the web-page access to send the signal to the above program. That I can't help with.
The only thing you need to change in the script is to substitute "cat appended_file >> other_file" for "true > /dev.null".
Alternatively, if you reckon that "sleep 1" (or whatever) is safe from multiple wep-page accesses, then you can stick to the other script which at least is independent of getting something else to send the signal, since all it does is watch a file.
Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) Ted.Harding@nessie.mcc.ac.uk Fax-to-email: +44 (0)870 167 1972 Date: 18-Feb-02 Time: 14:26:55 ------------------------------ XFMail ------------------------------
It occurs to me that, while you can set the "sleep" to anything you like down to 1 second (but you can't set it smaller), you might have two or more web-page accesses during the sleep period; and you wanted to do the append for every access.
Much easier to do a "tail -f" on the Web server's log file, read lines in a loop and do whatever we want to do for each line.
On 18-Feb-02 Eric Sellin wrote:
Much easier to do a "tail -f" on the Web server's log file, read lines in a loop and do whatever we want to do for each line.
Assuming that's how you tell a page has been accessed, then this is a neat way of doing it.
BTW, I think it's time Jenny told in a bit more detail what she needs to do when the web-page is accessed. Is it any web-page, with a different action for each web page? Or is it the same action for any access? Is it simply access to the web-site as a whole?
In any case, following up on Eric's suggestion above, 'awk' would be a very good way of doing it, since it is designed to be a program which automatically responds line-by-line to line-by-line input. This would go on the following lines:
tail -f -l 1 access_log | awk 'BEGIN{next} {system("cat append_file >> other-file")}'
'tail' tracks access_log, outputting the most recent line added to it. The BEGIN action discards the stale initial line which would be read from access_log on startup. Thereafter, each new line output by tail will trigger the append.
However, this assumes that a web-page access appends one line only to access_log. and would fail if a web-page access generated several lines in access_log, since then each of these would trigger the append. In that case one would need to make the 'awk' command a bit more intelligent.
For instance, suppose -- no matter how many lines a single web-page access generated -- there was amongst these a single line containing a certain pattern. Then you can make 'awk' respond only to lines containing this pattern, as in
tail -f -l 1 access_log | awk 'BEGIN{next} /pattern/{system("cat append_file >> other-file")}'
Since (in the 'awk' command) /pattern/ can be a regular expression, this also allows several different (but equivalent) forms of pattern to be recognised as indicating that a web-page has been accessed; and, should a different action be required for some, this can also be accomodated; as in:
tail -f -l 1 access_log | awk 'BEGIN{next} /pattern1/{system("action 1")} /pattern2/{system("action 2")} .... /patternN/{system("action N")}'
And so it goes ... Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) Ted.Harding@nessie.mcc.ac.uk Fax-to-email: +44 (0)870 167 1972 Date: 18-Feb-02 Time: 15:46:48 ------------------------------ XFMail ------------------------------
On Mon, Feb 18, 2002 at 01:12:54PM +0000, Jenny_Hopkins@toby-churchill.com wrote:
Can anyone help me with a bit of php? I need to append the copntents of one file on to another when a web page is accessed. Cron is no use as the timing isn't fixed. I can append a line of text so far, but not the contents of a file, and as I'm away for the rest of the week and have been doing this manually till I find the fix, I'm panicking! - - help!
Off the top of my head ..
$fp = fopen("file1",a); $arr = file("file2"); while(list($i,$line) = each($arr)) fwrite($fp,$line);
where file1 is the file to be appended.
Mal