Jump to content

foreach loop not working


c_pattle

Recommended Posts

I have the following form the just has one field. 

 

<html>
    <head>
        <title>Post</title>
    </head>
    <body>
        <form action="worldpay_callback.php" method="post">
            <input type="text" name="username" id="username" />
            <input type="submit" name="submit" id="submit" value="send" />
        </form>
    </body>
</html>

 

I am then posting the form to the following page that is meant to loop through all of the post values and save them to a text file.  For some reason my foreach loop doesn't seem to be working as done of the post values are getting written to the text file.  Can anyone see anything I'm doing wrong?

 

$myFile ="notifications.txt";
$fh = fopen($myFile, 'w+') or die("can't open file");

foreach($_POST as $k=>$v)
{
   $data = 'POST: Key:'.$k.' - Value:'.$v ." \n";
   fwrite($fh, $data);
}
   
fclose($fh);

return "[OK]";

 

Thanks for any help

Link to comment
Share on other sites

The highest permissions a file should ever have are 0755.

Why do you need a loop if only one value is being sent to the server?

You do realize that opening a file in "w+" mode will truncate the file every time correct?

You should have error_reporting set to E_ALL or -1, and Display_errors set to "On", if it is a file permissions error, an error will be triggered.

A couple debugging precautions should be put in place here:

 

if(isset($_POST['submit']))
{
    $myFile ="notifications.txt";
    $username = trim($_POST['username']);
    $fh = fopen($myFile, 'w+') or die("can't open file");
    $data = 'POST: Key: ' . $k . ' - Value: ' . $v . " \n";
    $fw = fwrite($fh, $data);
    if($fw === false)
    {
        echo "failed to write to file.";
    }
    fclose($fh);
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.