-1
<div id="left-body-part-innerpage">
    <h1 class="mainheading">Contact Us</h1>
    <div id="contactus-right-div" class="content">
    <?php session_start();
        if( isset($_POST['button2']))           
        {
            if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) 
            {
                $name = $_POST['name'];
                // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
                session_destroy();
                header("Location: contactdb.php");
?>

i am getting Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at website) in website

Warning: Cannot modify header information - headers already sent by (output started at website) in website

can any one help me?

Thanks in advance....

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Rachel
  • 139
  • 1
  • 4
  • 11

5 Answers5

5
  1. Starting a session requires setting HTTP headers
  2. You can't send headers after you have sent content
  3. Anything outside <?php and ?> constitutes content (as does anything you echo, print, etc)

Move your session code to the top of the script.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • hi david, i have moved session_start() at the beginning of my page but i am still getting the same warnings. – Rachel Jun 24 '10 at 10:49
  • @Rachel have you moved the whole code block to the very beginning of the page? – Pekka Jun 24 '10 at 10:49
  • Believe it or not, the `header` function also sends headers which can't appear after content. – Quentin Jun 24 '10 at 10:53
  • i can't move the code if( isset($_POST['button2'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) as it is used to check whether the captcha code has been filled properly or not. because if it is filled properly than only user should be redirected to next page. – Rachel Jun 24 '10 at 10:55
  • 1
    @Rachel you absolutely can and must put it to the top of the page. You can not start a page redirect once HTML has been output. You may have to rewrite the whole thing a bit in order to display an error message if the captcha doesn't match but that `header()` command *must* come before amnything is output. – Pekka Jun 24 '10 at 11:03
  • how would it check the captcha security? i am not getting what you are saying – Rachel Jun 24 '10 at 11:06
  • i have put this code in the starting of the page and than i have put this code Sorry, you have provided an invalid security code
    '; ?> but still the same output :(
    – Rachel Jun 24 '10 at 11:12
  • I don't really know how the captcha would work, but one thing is certain: It isn't going to involve loading half a page, pausing the PHP script, waiting for the user to type in the answer to the captcha and then resuming the page. HTTP doesn't work like that. You would normally present the capcha on one page with a form, then another script would process the results of that once the user hits submit to make a new HTTP request. – Quentin Jun 24 '10 at 11:13
2

all work with session, cookies, header() etc(everything that modifies http headers) must be done before first output of the script... put your php block before the markup

<?php session_start();
    if( isset($_POST['button2']))           
    {
        if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) 
        {
            $name = $_POST['name'];
            // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
            session_destroy();
            header("Location: contactdb.php");?>

<div id="left-body-part-innerpage">
<h1 class="mainheading">Contact Us</h1>
<div id="contactus-right-div" class="content">
Sergey Eremin
  • 10,994
  • 2
  • 38
  • 44
0

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at website) in website

Warning: Cannot modify header information - headers already sent by (output started at website) in website

Regarding this two issue :

--- Use session start in top of your code. Before that you can use ob_start() for output buffer clearing.

Like,

<?php
ob_start();
session_start();
?>
Karthik
  • 3,221
  • 5
  • 28
  • 38
0

As a general rule, do all business logic first (such as session management) before starting output of content.

As already pointed out, starting to print page content will automatically send headers.

Fredrik
  • 113
  • 8
0

Use output buffering to prevent output before you send headers.

<?php

function callback($buffer) {

  // заменить все apples на oranges
  return (ereg_replace("apples", "oranges", $buffer));

}

ob_start("callback");
//HERE you can send any headers you want. callback is not required here if you don't want
?>

<html>
<body>
<p>It's like comparing apples to oranges.
</body>
</html>

<?php
//And here you can
ob_end_flush();
//If send headers here - you'll get warning
?>
GOsha
  • 689
  • 5
  • 13