Search code examples
phphtmlstringoutput-buffering

Output Buffering: Easy way to make string out of HTML-Code


I'm currently using Chatfuel to open the index.php-file of my website which sends the user the html code into his browser. There he can register and set up his account. An example URL might look like this:

https://my.domain.com?key_value='123456789'

Depending on if that user is a new or a existing one, I wanna present him with a different form. In order to check so, I do a simple query to the MySQL db and see if the passed on key_value is already in the db and safe true or false to a boolean. Stating the obvious: If hes not an existing user, the 'empty' form with no values should show up. If he is registered he should see the information he filled in from last time.

My idea: At the top of my index.php I do the check whether he's an existing customer or not (Note: This is working already). Then I want to use outputbuffering to alter the html-code depending on the boolean, before it is sent to the client.

My problem: I developed the blueprint of the website in plain html (see code below). And OB only catches it as output if its within a string. Since I use " as well as ' in the document the string gets interrupted every few lines. Is there a simple workaround to this? Because the OB function is unable to access anything within the <html>...</html> tags. Or do i need to use redirecting after the check (in my index.php) and create a separate form + script for both edit customer data and add new customer data?

<?php

//Connection stuff


// Prepare statment: !TODO: string needs to be escaped properly first
$query_string = "SELECT * FROM tbl_customer WHERE unique_url = '$uniqueurl'";
$query_rslt = mysqli_query($conn, $query_string);

if($query_rslt == FALSE)
{
    // Failure
    echo "<br> Oops! Something went wrong with the querying of the db. " . $conn->connect_error;
    //Handle error
}
else
{
    if ($query_rslt->num_rows > 0)    
    {
        // Set boolean
        $existing_customer = TRUE;

        // Create an array called row to store all tuples that match the query string
        while($row = mysqli_fetch_assoc($query_rslt)) {
            //...
        }
    }
}

// Custom post processing function
function ob_postprocess($buffer)
{
    // do a fun quick change to our HTML before it is sent to the browser
    $buffer = str_replace('Testing', 'Working', $buffer);

    // Send $buffer to the browser
    return $buffer;
}

// start output buffering at the top of our script with this simple command
// we've added "ob_postprocess" (our custom post processing function) as a parameter of ob_start
if (!ob_start('ob_postprocess'))
{
    // Failure
    echo "<br> Oops! Something went wrong with output buffering. Check that no HTML-Code is sent to client before calling this start function.";
    // Handle error
}
else
{
    // Success
    // This is where the string should get accessed before sending to the client browser
    echo "Testing OB.";
}

?>

<!--DOCTYPE html-->
<html lang="en">
    <head>
        <meta charset="utf-8">
        //...
</body>
</html>

<?php

// end output buffering and send our HTML to the browser as a whole
ob_end_flush();

?>

Output: "Working OB."

EDIT: I added source code example. This code won't compile.


Solution

  • tl;dr: The thing I was looking for was a combination of file_get_contents() and object buffering.

    file_get_contents() returns a string of a plain html-file of your choice. I could post a ton of explanation here or simply link you to phppot.com. The article offers you a directly executable demo with source (Download here). In case you wanna try it with a html file of yours, simply change the file path.

    So once the whole html was converted into a string, I used the postprocessing function of OB to alter the string (= basically my html) if it's an existing user that came to alter his data. Then all the html-code (in a string still at this point) is sent to the client using ob_end_flush(). I will put up the actual code asap :)