PHP script for getting post data from Tampermonkey script I'm trying to shorten this piece of my code into a loop:

&lt;?php header('Access-Control-Allow-Origin: http://mysite.com'); ?&gt;

&lt;?php 

    if(sizeof($_POST))
    {
    file_put_contents;
    } else {
    echo "ERROR! Must be activated from Tampermonkey script!";
    exit();
    }

file_put_contents(
"content.html", 
"&lt;strong&gt; Updated last: " . $_POST['date'] . "&lt;/strong&gt;&lt;br&gt;&lt;br&gt;"  .
//Please add more entries depending on how many accounts you have configured

"&lt;strong&gt;Account 1&lt;/strong&gt;&lt;br&gt;(" . $_POST['accountname1'] . ")" . "&lt;br&gt;CREDITS&lt;br&gt;" . $_POST['credits1'] . "&lt;br&gt;-&lt;br&gt;" . 
"&lt;strong&gt;Account 2&lt;/strong&gt;&lt;br&gt;(" . $_POST['accountname2'] . ")" . "&lt;br&gt;CREDITS&lt;br&gt;" . $_POST['credits2'] . "&lt;br&gt;-&lt;br&gt;" .
"&lt;strong&gt;Account 3&lt;/strong&gt;&lt;br&gt;(" . $_POST['accountname3'] . ")" . "&lt;br&gt;CREDITS&lt;br&gt;" . $_POST['credits3'] . "&lt;br&gt;-&lt;br&gt;" .
"&lt;strong&gt;Account 4&lt;/strong&gt;&lt;br&gt;(" . $_POST['accountname4'] . ")" . "&lt;br&gt;CREDITS&lt;br&gt;" . $_POST['credits4'] . "&lt;br&gt;-&lt;br&gt;" .
"&lt;strong&gt;Account 5&lt;/strong&gt;&lt;br&gt;(" . $_POST['accountname5'] . ")" . "&lt;br&gt;CREDITS&lt;br&gt;" . $_POST['credits5'] . "&lt;br&gt;-&lt;br&gt;" .
"&lt;strong&gt;Account 6&lt;/strong&gt;&lt;br&gt;(" . $_POST['accountname6'] . ")" . "&lt;br&gt;CREDITS&lt;br&gt;" . $_POST['credits6']); 

?&gt;


Here's the JavaScript code I'm posting with:

function bprs() {

    {
        var rowCount = $('#accountsTable tr').length;
        var accountsCount = rowCount -1;                       
        var accounts = [];
        for (var n = 1; n &lt;= accountsCount; n++) {
            accounts[n] = {                          
                name: $('#accountName' + n).text(),
                credits: $('#credits' + n).text()
            };
        }

        var date = new Date();
        var data = "date=" + date + 
            accounts.reduce(function (prev, account, n) {
                return prev + "&amp;accountname" + n + "=" + account.name +
                    "&amp;credits" + n + "=" + account.credits;
            }, '');

        $.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
            options.async = true;
        });

        $.ajax({
            url: 'http://mysite.com/submit.php', // point to the php file here
            async:false,
            type: "POST",
            dataType: "json",
            data: data,
            success: function (result) {
                JSON.parse(result);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr);
            }
        });


Here's the post data my browser gives me:

" array(13) { ["date"]=&gt; string(58) "Sun Dec 27 2015 11:28:53 GMT-0700 (Mountain Standard Time)" ["accountname1"]=&gt; string(28) "*****@gmail.com" ["credits1"]=&gt; string(3) "110" ["accountname2"]=&gt; string(33) "*****@gmail.com" ["credits2"]=&gt; string(3) "109" ["accountname3"]=&gt; string(23) "*****@gmail.com" ["credits3"]=&gt; string(3) "536" ["accountname4"]=&gt; string(32) "*****@outlook.com" ["credits4"]=&gt; string(3) "333" ["accountname5"]=&gt; string(19) "*****@gmail.com" ["credits5"]=&gt; string(3) "188" ["accountname6"]=&gt; string(20) "*****@gmail.com" ["credits6"]=&gt; string(2) "72" } "


What this does is post data from a web page to a PHP script on a server, then the script parses the data into an HTML file.

As it stands right now, the script requires me to add a new entry when I create a new account and add it to the page.

What I'd like is for my PHP page to generate the HTML code based on how many entries are in the post data.
A somewhat better solution would be to take advantage of the fact that PHP auto-generates arrays from REQUEST data, if the fields are named correctly. Thus I propose changing the code posted by you to the following:

var date = new Date();
var data = "date=" + date +
           "&amp;accountsnumber=" + accountsCount +
    accounts.reduce(function (prev, account, n) {
        // Note the use of square brackets here, they tell PHP to create an array.
        return prev + "&amp;d[" + n + "][accountname]=" + account.name +
            "&amp;d[" + n + "][credits]=" + account.credits;
    }, '');


As for the PHP code, it'll look like this:

&lt;?php
// Renamed this to save_contents () as that is what we're actually doing.
function save_contents()
{
    // Use intval () here to ensure that we do indeed have an integer, and not some random injection code.
    $accountnumber = intval ($_POST['accountsnumber']);

    // Validate with DateTime to verify that this is indeed a valid date.
    $date = new DateTime ($_POST['date'], new DateTimeZone (DEFAULT_TIMEZONE));

    // Format the date, and use htmlspecialchars to ensure HTML-conformity of the output.
    $str = '&lt;strong&gt;Updated last: %s&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;';
    $output = sprintf ($str, htmlspecialchars ($date-&gt;format ("Y-m-d")))

    // Create a string template to use with sprintf (), to make the code easier to read.
    $str = '&lt;strong&gt;Account %1$d&lt;/strong&gt;&lt;br /&gt;(%2$s)&lt;br /&gt;CREDITS&lt;br /&gt;%3$s&lt;br /&gt;-&lt;br /';

    for ($id = 1; $id &lt;= $accountnumber; $id++) {            
        // Use htmlspecialchars to protect against XSS.
        $accName = htmlspecialchars ($_POST['d'][$id]['accountname']);
        $credits = htmlspecialchars ($_POST['d'][$id]['credits']);

        $output .= sprintf ($str, $id, $accName, $credits);
    }

    file_put_contents("content.html", rtrim ($output, '&lt;br /&gt;-&lt;br /&gt;'));
}


As you can see I've added some basic protection against attackers as well, as the code posted by you is woefully insecure. Basically allowing whomever wants full access to your web-server, to do whatever they like!
Check out OWASP for more information.