Re: newbie question [message #170962 is a reply to message #170957] |
Sun, 12 December 2010 13:26 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 12/11/2010 9:06 PM, justaguy wrote:
> Hi,
>
> This is my very first php script and I'm already in trouble. I'd like
> it to do two things:
> a) append the email address to the Log.txt file on the server at the
> same location;
> b) redirect to newlocation.html page
>
> The redirection is ok but the email capture failed. It seems that the
> logMessage isn't called. How can we fix it? Thanks.
>
> <?php
> $email = $_POST['email'];
> // $email = $HTTP_POST_VARS['email'];
> echo "$email<br />";
>
> function logMessage($email)
> // debug
> echo "email recording function called.";
> {
> $myFile = "Log.txt";
> $contentsOld = file_get_contents($myFile, true);
> $fh = fopen($myFile, 'w') or die("couldnt locate log file");
>
> $stringData = date("l F j, Y, g:i:s a") . "\nEmail: " . $email . "\n
> \n";
> fwrite($fh, $contentsOld);
> fwrite($fh, $stringData);
> fclose($fh);
> }
>
> // debug
> // echo "email has been processed.";
>
> header( 'Location: http://www.mysite.com/newlocation.html' ) ;
> ?>
In addition to Helmut's comments, your logMessage() function is not safe
in a multitasking environment. Two different processes could get in and
read/write the file at the same time, corrupting your file.
At a minimum, you need to lock the file before reading and unlock it
after closing it. Ensure that locking works on your web server; some
OS's support it, some don't (an even better solution would be to use a
database, which is made to run in a multitasking environment).
Also, one performance improvement - since you're adding to the end of
the file, you really don't need to read the entire file and write it
back out. Just open the file for append and write your new string (you
still need to lock/unlock the file).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|