ob_start(), ob_end_clean() and header() combination produces WARNING

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    ob_start(), ob_end_clean() and header() combination produces WARNING

    Based upon the following articles:

    Find answers to ob_start and redirecting from the expert community at Experts Exchange






    The class PHP script html.inc has as its constructor method a means by
    which it has to call an outside function, preSelect, to determine
    whether or not to insert " checked" (or in this case, " selected")
    into the written dropdown. The existing function is written to ECHO
    the string onto the page. Since preSelect is required within the
    scope of my constructor method that produces dategroup dropdowns, I
    knew I would have to encase the preSelect command inside output
    buffering to capture it and send it to a variable.

    $html = '<select name="' . $enumKeyValArra y[$i][0] . '">';
    foreach($monthA rray as $innerKey => $innerVal) {
    ob_start();
    preSelect($enum KeyValArray[$i][1], $innerKey);
    $preSelectText = ob_get_contents ();
    ob_end_clean();
    $html .= '<option value="' . $innerKey . '" ' . $preSelectText
    .. '>' . $innerKey . ' - ' . $innerVal . '</option>';
    }
    $html .= '</select>';

    Everything works fine.. however, there is a bit of a bubble in my
    logic.

    There is a form produced, view_internship _applicant.html , that will
    incorporate this class method into its parent script, viewinterns.php .
    When the user clicks SUBMIT, they are redirected back to
    viewinterns.php to do form processing. Everything is done just
    right...

    ...until you get to

    header("Locatio n: viewinterns.php ");

    Then I get a WARNING thrown "Headers already included on ../html.inc
    line 162" which happens to be this line:

    ob_start();

    When you clicked SUBMIT you went to the output buffering and then you
    go to header(), which apparently you can't do both. My workaround was
    to replace the header() commands in viewinterns.php to echo HTML meta
    redirection tags instead, which works, but for some reason the page
    now takes 15 - 20 seconds to pull up.

    I'm not sure what I must do; I was told based on what I read to move
    the ob_end_clean() command after header(), but that would fail the
    entire scope of the existing class, which I lovingly provided for you
    to see what I mean:

    -----------------------------------------------------------------------------------------------

    class DateGroupHTMLGe nerator {
    var $propertyArray, $htmlArray,
    $hasMonth, $hasDay, $hasYear;

    // CONSTRUCTOR
    function DateGroupHTMLGe nerator($classP ropertyArray, $hasMonth = 1,
    $hasDay = 1, $hasYear = 1) {

    foreach(array(' month', 'day', 'year') as $key => $val) $this->{'has'
    .. ucfirst($val)} = ${'has' . ucfirst($val)};

    $this->propertyArra y = $classPropertyA rray;

    // CONVERT THE INPUT PARAMETER ARRAY INTO A 2-DIM ENUMERATIVE ARRAY
    TO PRESERVE KEYS AND VALS AND HAVE ENUMERATION FOR SWITCH
    $enumKeyValArra y = array();
    foreach($this->propertyArra y as $key => $val)
    array_push($enu mKeyValArray, array($key, $val));

    // GET THE VALUE OF THE LAST ELEMENT OF THE INPUT PARAMETER ARRAY
    WHICH WILL BE THE TEXT FIELD LENGTH
    $textFieldLengt hArray =
    array_values(ar ray_reverse($th is->propertyArray) );
    $textFieldLengt h = $textFieldLengt hArray[0];

    $this->htmlArray = array();

    $monthArray = array('01' => 'January',
    '02' => 'February',
    '03' => 'March',
    '04' => 'April',
    '05' => 'May',
    '06' => 'June',
    '07' => 'July',
    '08' => 'August',
    '09' => 'September',
    '10' => 'October',
    '11' => 'November',
    '12' => 'December'
    );


    for ($i = 0; $i < sizeof($enumKey ValArray); $i++) {
    $html = '';
    switch ($i) {
    case '2': // TEXT FIELD SLOT IN INPUT ARRAY PARAMETER
    if ($this->hasYear) {
    if ($textFieldLeng th == 2) $html = '20';
    $html .= '<input name="' . $enumKeyValArra y[$i][0] . '"
    value="' . str_replace('"' , '&quot;', $enumKeyValArra y[$i][1]) . '"
    size="' . $textFieldLengt h . '">';
    }
    break;

    case '3': // SIZE OF TEXT FIELD - NOTHING NEEDS TO BE DONE
    // DO NOTHING
    break;

    case '0': // MONTH DROPDOWN
    if ($this->hasMonth) {
    $html = '<select name="' . $enumKeyValArra y[$i][0] . '">';
    foreach($monthA rray as $innerKey => $innerVal) {
    ob_start();
    preSelect($enum KeyValArray[$i][1], $innerKey);
    $preSelectText = ob_get_contents ();
    ob_end_clean();
    $html .= '<option value="' . $innerKey . '" ' . $preSelectText
    .. '>' . $innerKey . ' - ' . $innerVal . '</option>';
    }
    $html .= '</select>';
    }
    break;

    case '1': // DAY DROPDOWN
    if ($this->hasDay) {
    $html = '<select name="' . $enumKeyValArra y[$i][0] . '">';
    for ($j = 1; $j <= 31; $j++) {
    $myDay = ($j < 10) ? '0' . $j : $j;
    ob_start();
    preSelect($enum KeyValArray[$i][1], $myDay);
    $preSelectText = ob_get_contents ();
    ob_end_clean();
    $html .= '<option value="' . $myDay . '" ' . $preSelectText . '>'
    .. $myDay . '</option>';
    }
    $html .= '</select>';
    }
    break;

    default: // THIS WILL BE EXPANDED FOR FUTURE IMPLEMENTATION OF
    DATEGROUP, FOR NOW IF ANYTHING ELSE IS FOUND DO NOTHING
    // DO NOTHING
    break;
    }

    $this->htmlArray[$i] = $html;
    }


    }

    function getMonthDropdow n() { // STRING "METHOD" TO PRODUCE MONTH
    DROPDOWN
    return $this->htmlArray[0];
    }

    function getDayDropdown( ) { // STRING "METHOD" TO PRODUCE DAY
    DROPDOWN
    return $this->htmlArray[1];
    }

    function getYearText() { // STRING "METHOD" TO PRODUCE YEAR TEXT
    FIELD
    return $this->htmlArray[2];
    }

    }

    ---------------------------
    class DateGroupHTMLGe nerator from .../html.inc included within
    viewinterns.php

    Has anyone here ever worked with output buffering and redirection
    combinations in PHP before? What advice would you give me based on
    what you see and what I have learned so far?

    Thanx
    Phil
Working...