Page 1 of 1

Session started error

Posted: Thu Sep 11, 2008 11:54 am
by ardan16
Hi all,
Can someone please tell me a way to stop a session started error with the following code.

Code: Select all

for ($i = 1; $i <= $num_pages; $i++) {
        if ($i != $current_page) {
        echo '<a href="view_comments.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
        } else {
            echo $i . ' ';
        }
    }
    
    // If it's not the last page, make a Next button.
    
    if ($current_page != $num_pages) {
        echo '<a href="view_comments.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
        }
If there is more than one page to display I get the session started error.
Thanks :banghead:

Re: Session started error

Posted: Thu Sep 11, 2008 12:03 pm
by jayshields
You don't have a session_start() call in that snippet?

Post your full script (in [ php ] tags) and the full error message.

I'll take a stab in the dark and guess it's a headers already sent message. In which case, a search on Google or these forums will provide your answer.

Re: Session started error

Posted: Thu Sep 11, 2008 12:40 pm
by ardan16
Hi Jay,
earlier in that code I call

Code: Select all

require_once ("main.php");
require_once ("includes/header.php");
This is the code for main.php

Code: Select all

<? 
session_start(); 
include("mysql_connect.php");
?>
<div id="login">
<?php
require_once("login.php");
?>
<? displayLogin(); ?>
</div>
This is the full page

Code: Select all

<?php 
 
$page_title = 'View Comments about Utilities';
require_once ("main.php");
require_once ("includes/header.php");
 
require_once ('mysql_connect.php'); // Connect to the db.
 
// Number of records to show per page:
$display = 5;
 
// Determine how many pages there are. 
if (isset($_GET['np'])) { // Already been determined.
 
    $num_pages = $_GET['np'];
 
} else { // Need to determine.
 
    // Count the number of records
    $query = "SELECT COUNT(*) FROM banks2 ORDER BY name ASC";
    $result = mysql_query ($query);
    $row = mysql_fetch_array ($result, MYSQL_NUM);
    $num_records = $row[0];
 
    // Calculate the number of pages.
    if ($num_records > $display) { // More than 1 page.
        $num_pages = ceil ($num_records/$display);
    } else {
        $num_pages = 1;
    }
    
} // End of np IF.
 
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
    $start = $_GET['s'];
} else {
    $start = 0;
}
        
$query = "SELECT(name) AS name, (comment) AS com, DATE_FORMAT(date, '%e %b, %y') AS date, (face) AS face FROM banks2 ORDER BY `banks2`.`date` DESC
 LIMIT $start, $display";
 
$result = @mysql_query ($query); // Run the query.
$num = mysql_num_rows($result);
?>
<a href="view_comments.php">Banks</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="view_comments_u.php">Utilities</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="view_comments_t.php">Phone/Mob/Net</a>
<?php
if ($num > 0) { // If it ran OK, display the records.
 
    echo "<p>You are viewing $num user comments at a time.</p>\n";
 
    // Table header.
    echo '<table align="center" border="1px" cellpadding="1" width="100%">
    
    <tr><td align="left"><font size="2" ><b>Name of Bank</b></font></td><td align="left" ><font size="2" ><b>Comments</b></font></td><td align="left" ><font size="2" ><b>Date</b></font></td><td align="left" ><font size="2" ><b>Pos/Neg<br />/Other</b></font></td></tr>
';
    
// Fetch and print all the records.
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
         $img = null;
 
 
 if ($row['face'] == 'Pos') {
       $img = '<img src="includes/smile.jpg" />';
       } elseif ($row['face'] == 'Neg') {
       $img = '<img src="includes/sad.jpg" />';
    } elseif ($row['face'] == 'Oth') {
       $img = '<img src="includes/other.jpg" />';
    } 
 
 
   // Now build the table row using the face image instead of the face field value
    echo '<tr><td align="center" ><font size="2" >' . $row['name'] . '</font></td><td align="left"><font size="2" >' . $row['com'] . '</font></td><td align="center"><font size="2" >' . $row['date']. '</font></td><td align="center">' . $img  .'</td></tr>';
        
    }
 
    echo '</table>';
    
    mysql_free_result ($result); // Free up the resources.  
 
} else { // If it did not run OK.
    echo '<p class="error">There are none, or no more user comments.</p>';
}
 
mysql_close(); // Close the database connection.
for ($i = 1; $i <= $num_pages; $i++) {
        if ($i != $current_page) {
        echo '<a href="view_comments.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
        } else {
            echo $i . ' ';
        }
    }
    
    // If it's not the last page, make a Next button.
    
    if ($current_page != $num_pages) {
        echo '<a href="view_comments.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
        }
    
require_once ("includes/footer.php");
 
?>
Cheers

Re: Session started error

Posted: Thu Sep 11, 2008 12:43 pm
by ardan16
Sorry here is the error message,

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/arbira/public_html/tellemaustralia/view_comments.php:1) in /home/arbira/public_html/tellemaustralia/main.php on line 4

Re: Session started error

Posted: Thu Sep 11, 2008 12:56 pm
by Ziq
Are you include view_comments.php before main.php?

If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser

Re: Session started error

Posted: Thu Sep 11, 2008 1:30 pm
by jayshields
Headers already sent means that something has been sent to the browser before the session_start() call. You need to get rid of that something. Search the forums for tips on this subject.