newbie question [message #170957] |
Sun, 12 December 2010 02:06 |
justaguy
Messages: 16 Registered: December 2010
Karma: 0
|
Junior Member |
|
|
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' ) ;
?>
|
|
|
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: 0
|
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
|
|
|
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: 0
|
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
==================
|
|
|
Re: newbie question [message #170963 is a reply to message #170957] |
Sun, 12 December 2010 14:39 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
justaguy wrote:
> 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?
Just a wild guess, but why not try calling 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);
Do you not find it the least bit strange to read the content of a file,
replace the content with the content, then write new content, and call that
process "appending" the new content to the file?
You have already discovered the `w' flag for fopen(). Guess what other
flags there might be …
Bottom line: RTFM.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
|
|
|
Re: newbie question [message #170964 is a reply to message #170963] |
Sun, 12 December 2010 15:00 |
Magno
Messages: 49 Registered: October 2010
Karma: 0
|
Member |
|
|
On 12/12/2010 11:39 AM, Thomas 'PointedEars' Lahn wrote:
> justaguy wrote:
>
>> 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?
>
> Just a wild guess, but why not try calling 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);
>
> Do you not find it the least bit strange to read the content of a file,
> replace the content with the content, then write new content, and call that
> process "appending" the new content to the file?
>
> You have already discovered the `w' flag for fopen(). Guess what other
> flags there might be …
>
> Bottom line: RTFM.
As expected, PointedEars like always waiting for other to answers, and
comes with his stupid version to insult the OP.
justaguy: don’t pay attention to PointedEars, he is a troll which always
insults newcomers and treats them like idiots.
Everybody else has filtered him for the same reason.
|
|
|
Re: newbie question [message #170965 is a reply to message #170964] |
Sun, 12 December 2010 16:07 |
justaguy
Messages: 16 Registered: December 2010
Karma: 0
|
Junior Member |
|
|
On Dec 12, 10:00 am, Magno <marbar...@gmail.com> wrote:
> On 12/12/2010 11:39 AM, Thomas 'PointedEars' Lahn wrote:
>
>
>
>> justaguy wrote:
>
>>> 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?
>
>> Just a wild guess, but why not try calling 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);
>
>> Do you not find it the least bit strange to read the content of a file,
>> replace the content with the content, then write new content, and call that
>> process "appending" the new content to the file?
>
>> You have already discovered the `w' flag for fopen(). Guess what other
>> flags there might be …
>
>> Bottom line: RTFM.
>
> As expected, PointedEars like always waiting for other to answers, and
> comes with his stupid version to insult the OP.
>
> justaguy: don’t pay attention to PointedEars, he is a troll which always
> insults newcomers and treats them like idiots.
> Everybody else has filtered him for the same reason.
Thank you all, probably I didn't word it correctly in that the code
initially by someone else who's much into php and I added the last
element of redirection. Since I didn't know how php works and assumed
he did the right thing and that seemed to stop me from thinking...
Thanks again, problem solved.
|
|
|
|