Re: newbie question [message #170961 is a reply to message #170957] |
Sun, 12 December 2010 10:13 |
Helmut Chang
Messages: 22 Registered: September 2010
Karma:
|
Junior Member |
|
|
Am 12.12.2010 03:06, schrieb justaguy:
> The redirection is ok but the email capture failed. It seems that the
> logMessage isn't called.
Yes, it isn't.
> How can we fix it?
In calling the function. As you already said: you declare the function,
but you don't call it:
> <?php
> $email = $_POST['email'];
> // $email = $HTTP_POST_VARS['email'];
> echo "$email<br />";
This is the declaration of the function:
> 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.";
But if you want to run the function, you must call it:
logMessage($email);
> header( 'Location: http://www.mysite.com/newlocation.html' ) ;
> ?>
That's the purpose of a function: you define/declare it once and then
you can call it anytime you want with different values for the parameters:
<?php
/**
* Writes the provided $email to a log file.
*/
function logMessage($email) {
...
}
$email = $_POST['email'];
logMessage($email);
$email2 = 'foo(at)example(dot)com';
logMessage($email2);
logMessage('bar(at)example(dot)com');
?>
It's the same as with PHP's native functions, as in your script:
fwrite($fh, $contentsOld);
fwrite($fh, $stringData);
And I strongly suggest to do some validation in your script!
Helmut
|
|
|