FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » newbie question
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
newbie question [message #170957] Sun, 12 December 2010 02:06 Go to next message
justaguy is currently offline  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 Go to previous messageGo to next message
Helmut Chang is currently offline  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 Go to previous messageGo to next message
Jerry Stuckle is currently offline  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 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  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 Go to previous messageGo to next message
Magno is currently offline  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 Go to previous messageGo to next message
justaguy is currently offline  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.
Re: newbie question [message #170970 is a reply to message #170957] Mon, 13 December 2010 08:15 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 12/12/2010 3:06, justaguy escribió/wrote:
> header( 'Location: http://www.mysite.com/newlocation.html' ) ;
> ?>

Last but not least, I recommend you add an explicit exit when you do
HTTP redirections, it'll save you future headaches:

header( 'Location: http://www.mysite.com/newlocation.html' ) ;
exit;




--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Use of Includes
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 21:16:37 GMT 2024

Total time taken to generate the page: 0.02737 seconds