-2

I am struggling to wrap my head around this.

I am creating two forms on a page to generate a configuration file for a device, one form for each of the two different devices we use. The form works fine and submits the values correctly, but I need the file to initiate a download. Using one form I have been able to achieve this, but with two forms I get an error regarding the header information.

Here is the head of my file

<html>
<head>
  <link rel="stylesheet" type="text/css" href="screen.css" />
  <?php 
      include('header.php'); 
      include('config/wan.php');
      include('config/wan-voip.php');
      include('config/pppoe.php');
      include('config/pppoe-voip.php');
      include('config/dlink.php');
   ?>
 </head>
<body>

This si followed by the a standard HTML form and then here is my code to validate the inputs and act accordingly:

if(isset($_POST['submit']))
{

    if(!empty($_POST['type']) && isset($_POST['router']) && $_POST['router'] == 'netgear')
    {
        if($_POST['type'] == 'pppoe' && (empty($_POST['username']) || empty($_POST['password'])))
        {
            echo 'Please fill in the username and password';
            exit;
        }



        if(empty($_POST['ssid']) || empty($_POST['pass']))
        {
            echo 'Please provide the SSID and Wireless Key';
            exit;
        }

        if(($_POST['type'] == 'wan-voip' || $_POST['type'] == 'pppoe-voip') && (empty($_POST['ext']) || empty($_POST['extpass'])))
        {
            echo 'Please provide the Extension Information';
            exit;
        }
    }
    elseif(isset($_POST['router']) && $_POST['router'] == 'dlink')
    {
        if(empty($_POST['ssid']) || empty($_POST['pass']))
        {
            echo 'Please provide the SSID and Wireless Key';
            exit;
        }
    }


    ob_end_clean();

    if($_POST['router'] == 'netgear')
    {
        header("Content-type: text/plain");
        header("Content-Disposition: attachment; filename=".date('d-m-Y')."-".$_POST['type'].".ini");

        $file = str_replace('-', '', $_POST['type']); 
        echo $$file;
    }
    elseif($_POST['router'] == 'dlink')
    {
        header("Content-type: text/plain");
        header("Content-Disposition: attachment; filename=".date('d-m-Y')."-dlink.cfg");
        echo $dlink;
    }



}

Could someone please suggest how I could go about solving this. I have read some other related spots and tried moving bits of the script around and trying some of the suggestions but I seem to keep hitting brick walls.

Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73

1 Answers1

2

in http protocol, headers should always be sent before body.

that is why you are getting this error, you are using

header("Content-type: text/plain");

after you have already outputted some text.

if you want to use the header method, you have to do it before any 'echo' or any text output

Dima
  • 8,586
  • 4
  • 28
  • 57