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

giumazzi

Utente Attivo
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 ("[email protected]", "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()
 
Discussioni simili
Autore Titolo Forum Risposte Data
G [PHP] Problema - Warning: session_start() [function.session-start] PHP 9
catellostefano CLASSE PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource PHP 2
W Warning: main(./WEB-INF/etc/config.php): PHP 3
F Cerco Hosting con VECCHIE versioni di php Hosting 0
Cosina Captcha php PHP 1
S passare un valore da un form a un file .php con metodo post PHP 4
N php msyql PHP 6
N php problemi a visualizzare video PHP 3
A menu a tendina php PHP 1
D protezione cartelle: blocco visualizzazione/scaricamento contenuto, ma abilitazione utilizzo dati da parte di file .php presenti sul sito Web Server 1
F Php date_diff PHP 1
K [PHP] Aggiungere caratteri ad una stringa in base alla lunghezza della stessa PHP 2
C Wp-admin a file php WordPress 5
Lino80 [Retribuito] Cerco programmatore php per modifica/inserimento funzione/valori da un plugin importer wordpress Offerte e Richieste di Lavoro e/o Collaborazione 0
csi Inviare file jpg in locale alla stampante con php PHP 0
M Passaggio variabili array php su un tasto jq PHP 3
E Php aggiornamento tabella PHP 9
G phpmailer e php 8.1 con estensione mysqli PHP 6
M Invio dati database via email php PHP 0
K [php] Problema con inner join PHP 4
K [php]form invio dati PHP 0
P Codifica caratteri speciali mysql php PHP 0
K [PHP] Problema con variabili concatenate. PHP 1
E Stampante termica escpos-php PHP 6
JeiMax Modifica codice php personalizzato PHP 2
G Come modificare un pdf in php PHP 1
U Link a doppio file PHP PHP 0
E PHP & jQuery PHP 8
N Passare array da php a javascript PHP 5
F Applicazione PHP/MySQL per prenotazioni: limitare il numero massimo di posti prenotabili PHP 20
L tipo boolean non funzionante su mariadb (mysql). E codice php 7.4. PHP 0
U PHP creare un file excel dopo ricerca nel DB PHP 0
M PHP/MySQL - Estrarre valori min e max di ogni gruppo PHP 5
F Php e fatturazione elettronica PHP 0
P lanciare script asp (o php) da jquery Javascript 1
Couting95 inserire dati da un file di testo in una tabella in php PHP 1
P Data scraping in PHP non funziona PHP 4
C Calcoli matematici in php PHP 5
F Scrivere dei dati in word con php PHP 0
D PHP leggere cartella di Windows PHP 1
I dominio aruba versione php server linux Domini 3
G Colorare menu select attraverso ricerca php PHP 0
L PHP motore di ricerca nel sito PHP 1
S PHP e Mysqli PHP 0
Y Stampare da php su un foglio A6 attraverso una stampante esterna PHP 1
M Visulizzare immagine con php PHP 8
G [PHP] Creare script di prenotazione con controllo disponibilità. PHP 7
G leggere file txt e stampare con php il contenuto a video PHP 7
F Ricreare struttura php+mysql su Xampp Apache 0
Z PHP.INI - STMP per invio email con PHP Server Dedicati e VPS 0

Discussioni simili