[PHP] Warning: Cannot modify header information - headers already sent by

giumazzi

Nuovo Utente
16 Feb 2017
27
2
3
provincia PESARO
ricevo questo warning, problema alla riga 92
PHP:
Warning: Cannot modify header information - headers already sent by (output started at /home/ilguitto/domains/ilguitto.org/public_html/book/register.php:17) in /home/ilguitto/domains/ilguitto.org/public_html/book/register.php on line 92
Qui sotto la riga 92
PHP:
header('Location: book.php?e='.urlencode($error)); exit;
Qui sotto il listato completo del file register.php dove alla riga 92 c'è il problema
PHP:
<?php
    session_start();
?>
<!--
testo omessso
-->

<?php
include('db_login.php');
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection)
{
    die ("Could not connect to the database: <br />". mysql_error());
}
mysql_select_db('ilguitto_book');

// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
    header('Location: book.php'); exit;
}

   
// get the posted data
$doj = strip_tags( utf8_decode( $_POST['journey_date'] ) );
$name = strip_tags( utf8_decode( $_POST['user_name'] ) );
$address = strip_tags( utf8_decode( $_POST['address'] ) );
$mobile = strip_tags( utf8_decode( $_POST['mobile'] ) );
$userid = strip_tags( utf8_decode( $_POST['userid'] ) );
$email = strip_tags( utf8_decode( $_POST['email'] ) );
$password = strip_tags( utf8_decode( $_POST['password'] ) );
$message = strip_tags( utf8_decode( $_POST['message'] ) );
   
// check that name was entered
if (empty($name))
    $error = 'You must enter your name.';

// check that address was entered
if (empty($address))
    $error = 'You must enter your address.';

// check that mobile number was entered
if (empty($mobile))
    $error = 'You must enter your mobile number.';

// check that user id was entered
if (empty($userid))
    $error = 'You must enter your user id.';

// check that an email address was entered
elseif (empty($email))
    $error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email))
    $error = 'You must enter a valid email address.';
   
// check that a message was entered
if (empty($password))
    $error = 'You must enter password.';



//Check whether the student is already registered.
$select = mysql_query("select * from register where userid = '" . $userid . "'", $connection);

//$query = mysql_real_escape_string($select);

$num_rows = mysql_num_rows($select);

if ( $num_rows )
    $error = 'You are already registered.';
   
   
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
    header('Location: book.php?e='.urlencode($error)); exit;
}

// Success
$query = "INSERT INTO register (userid, name, email, password, address, contact, message) VALUES ('" . $userid . "','" . $name . "','" . $email . "','" . $password . "','" . $address . "','" . $mobile . "','" . $message . "')";

//$insert = mysql_real_escape_string($query);

$results = mysql_query($query);

if (!$results)
{
    die ("Could not insert to the register: <br />". mysql_error());
}

$seatNumber = NULL;

for($i=1; $i<11; $i++)
{
    $chparam = "ch" . strval($i);
    $calcPNR = $doj . "-" . strval($i);
    if( !empty($_POST[$chparam]) )
    {
        $query = "INSERT INTO seat(userid, number, PNR, date) VALUES ('". $userid ."', '" . intval($i) . "', '". $calcPNR ."', '". $doj ."')";
//        $results = mysql_real_escape_string($query);
        $results = mysql_query($query);
        if (!$results)
        {
            die ("Could not update seat: <br />". mysql_error());
        }
        $seatNumber = $seatNumber .", ". "$i";
    }
}
if(!empty($message))
{
    $message = "A new user '". $name ."' has booked seat number: '". $seatNumber ."'." . "His message is as below." . $message;  
}
else
{
    $message = "A new user '". $name ."' has booked seat number: '". $seatNumber;  
}

// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email\n";
$email_content .= "Message:\n\n$message";

$messageUser = "Your ticket is booked. The seat number is: " . $seatNumber;
   
// send the email to admin
//Please replace below email to email of your website admin, so that admin will receive email on every seat book.
mail ("xxxxxxx@xxxx.com", "New ticket booked", $email_content);

//send email to user
mail ($email, "Ticket booked", $messageUser);

mysql_close($connection);  
// send the user back to the form

header('Location: book.php?s='.urlencode('Your seat is booked.')); exit;
qui sotto il listato del file db_login.php (include)
PHP:
<?php
 $db_host='localhost';
 $db_username='myuser';
 $db_password='mypassw';
?>
<!--

testo omesso

-->
Qui sotto il listato del file book.php richiamato alla riga 92
PHP:
<!--

testo omesso

-->

<!DOCTYPE HTML>
<HTML>

 <HEAD>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Boot the ticket</title>
  <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  <link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.css">
 </HEAD>

 <BODY>

  <br>
  <div class="container">
         <div class="page-header">
             <h1>Book the tickets</h1>
         </div>   
   <?php

     // check for a successful form post
     if (isset($_GET['s'])) 
     {
      echo "<div class=\"alert alert-success\">".$_GET['s']." You will be automatically redirected after 5 seconds.</div>";
//      echo "You will be automatically redirected after 5 seconds.";
      header("refresh: 5; index.php");
     }

     // check for a form error
     elseif (isset($_GET['e'])) echo "<div class=\"alert alert-error\">".$_GET['e']."</div>";

   ?> 
   <form name="contactForm" action="register.php" method="POST" class="form-horizontal">
    <div class='control-group'>
     <label class='control-label' for='input1'>Seat Numbers</label>
     <div class='controls'>
     <?php 
      for($i=1; $i<11; $i++)
      {
       $chparam = "ch" . strval($i);
       if(isset($_POST[$chparam]))
       {
        echo "<input type='text' class='span3' name=ch".$i." value=".$i." readonly/><br>";
       }
      }
     ?>
                 </div>
             </div>
   
     <?php
      if(isset($_POST['doj']))
      {
       echo "<div class='control-group'>";
       echo "<label class='control-label' for='input1'>Date of Journey</label>";
        echo "<div class='controls'>";
         echo "<input type='text' name='journey_date' id='input1' class='span3' value=". $_POST['doj'] ." readonly />";
        echo "</div>";
       echo "</div>";
      }
     ?>
             
    <div class="control-group">
                 <label class="control-label" for="input1">Name</label>
                 <div class="controls">
                     <input type="text" name="user_name" id="input1" placeholder="Type your name" class="span3" pattern="[A-z ]{3,}" title="Please enter a valid name." required>
                 </div>
             </div>
    
             <div class="control-group">
                 <label class="control-label" for="input2">Address</label>
                 <div class="controls">
                     <input type="text" name="address" id="input2" placeholder="Type your address" class="span3" required>
                 </div>
             </div>

             <div class="control-group">
                 <label class="control-label" for="input3">Contact Number (only 10 digits)</label>
                 <div class="controls">
                     <input type="text" name="mobile" pattern=".{10}" title="Please enter 10 digit no." class="span3" placeholder="Type your mobile number" maxlength="10" required/>
                 </div>
             </div>
   

             <div class="control-group">
                 <label class="control-label" for="input4">User ID</label>
                 <div class="controls">
                     <input type="text" class="span3" placeholder="Type your user id" name="userid" pattern="[A-z ]{3,}" title="Please enter a valid username." required/>
                 </div>
             </div>


             <div class="control-group">
                 <label class="control-label" for="input5">Email ID</label>
                 <div class="controls">
                     <input type="text" class="span3" placeholder="Type your email id" name="email" pattern="^[a-zA-Z0-9-\_.]+@[a-zA-Z0-9-\_.]+\.[a-zA-Z0-9.]{2,5}$" title="Please enter a valid email id." required/>
                 </div>
             </div>

             <div class="control-group">
                 <label class="control-label" for="input6">Password</label>
                 <div class="controls">
                     <input type="password" class="span3" name="password" placeholder="Type your password" required/>
                 </div>
             </div>

             <div class="control-group">
                 <label class="control-label" for="input6">Message</label>
                 <div class="controls">
                     <textarea class="span3" rows="3" name="message" pattern="^[a-zA-Z][a-zA-Z0-9-_\.]{5,150}$" title="Please enter a valid message." ></textarea>
                 </div>
             </div>

             <div class="form-actions">
                 <input type="hidden" name="save" value="contact">
     <button type="submit" class="btn btn-info">
      <i class="icon-ok icon-white"></i> Book
     </button>
     <button type="reset" class="btn">
      <i class="icon-refresh icon-black"></i> Clear
     </button>
             </div>

   </form>
  </div>

  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script>window.jQuery || document.write('<script src="js/jquery-latest.min.js">\x3C/script>')</script>
  <script type="text/javascript" src="js/bootstrap.js"></script>
 </BODY>
</HTML>
C'è qualcuno che ha avuto esperienze analoghe?
Grazie per l'aiuto.
 

claudiolor

Nuovo Utente
23 Feb 2017
2
0
1
Il problema dovrebbe stare nel fatto che chiami più volte il comando header.
Prova prima di session_start() a chiamare ob_start()