Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 8, 2009, 12:25   #1
co.ador
SitePoint Evangelist
 
Join Date: Apr 2009
Posts: 573
Help me with this wwarning "Warning:session_start()"

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home3/nyhungry/public_html/includes/header.php:7) in /home3/nyhungry/public_html/includes/header.php on line 57

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home3/nyhungry/public_html/includes/header.php:7) in /home3/nyhungry/public_html/includes/header.php on line 57
( Hungers)

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" href="stylesheets/webpageprueba.css" rel="stylesheet" media="all" />
<title>www.Nyhungry.com</title>
<script type="text/javascript" src="scripts/prototype.js"></script>
<script type="text/javascript" src="scripts/rating.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
(
function()
{
  var default_image = $('td.largethumb img').attr('src');
  $('table.smallthumbs a').mouseover(function() { $('td.largethumb img').attr('src', $('img', this).attr('src')); });
});
</script>
</head>


<body>



<div id="sidebar1">
<ul id="mainNav">
      <li id="home"><a href="restaurants/index.php" title="All restaurants inside nyhungry" class="first">Home</a></li>
                <li id="music"><a href="../../Take Outs/Take Outs frontpage.html" title="Information about all take Outs">Hungry for Music!</a></li>
              <li id="pizzerias"><a href="../../Pizzeria/Pizzeria frontpage.html" title="All the pizzerias inside nyhungry">Contact us</a></li>
                   </ul>
<!-- end #sidebar1 --></div>
<!--<table id="restaurantview">

<a href=""><td width="40" id="contenido1">tropiezo></td></a>
<a href=""><td width="40" id="contenido2">colozar1></td></a>
<a href=""><td width="40" id="contenido3">colozar2></td></a>
<a href=""><td width="40" id="contenido4">colozar3></td></a> 
<a href=""><td width="40" id="contenido4">colozar4></td></a>
<a href=""><td width="40" id="contenido4">colozar3</td></a>
</table>-->

<div id="right">

<?php /*?><?php
 $data=file_get_contents('counter.dat');
 $counter=unserialize($data);
 $counter[$_SERVER["REMOTE_ADDR"]]=time();
 foreach ($counter as $key => $value) if ($value < (time()-180)) unset $counter($k);
 $data=serialize($counter);
 file_put_contents('counter.dat', $data);
 echo'<p>count($counter)." viewing"</p>';
?> </div><?php */?>

<?php  
 session_start(); // this is Line 57 where the warning is coming from... 

/* Define how long the maximum amount of time the session can be inactive. */ 
define("MAX_IDLE_TIME", 3); 

function getOnlineUsers(){ 

if ( $directory_handle = opendir( session_save_path() ) ) { 
$count = 0; 
while ( false !== ( $file = readdir( $directory_handle ) ) ) { 
if($file != '.' && $file != '..'){ 
// Comment the 'if(...){' and '}' lines if you get a significant amount of traffic 
if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) { 
$count++; 
} 
} 
closedir($directory_handle); 

return $count; 

} 
}
else { 
return false; 
} 

} 

echo '(' . getOnlineUsers() . ' viewer)<br />'; ?>

</div>
co.ador is offline   Reply With Quote
Old Nov 8, 2009, 12:36   #2
sheardben
SitePoint Enthusiast
 
Join Date: Oct 2009
Posts: 81
You need to have session_start() right at the top of the page.

ie. <?php session_start(); ?> THEN <!DOCTYPE... etc
sheardben is offline   Reply With Quote
Old Nov 8, 2009, 15:39   #3
Force Flow
Computer Slayer
 
Force Flow's Avatar
 
Join Date: Jul 2003
Location: Northeastern USA
Posts: 1,287
In order for a session to be started, no output can be sent to the browser yet. This also means that if you print out PHP error messages before session_start(), you'll get the same error as well.
Force Flow is offline   Reply With Quote
Old Nov 8, 2009, 17:39   #4
SpacePhoenix
SitePoint Guru
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 822
There is nothing wrong with the script as posted, I tried it locally and did not get any errors so possibly something is wrong with the OPs setup. co.ador is the script you posted in a include used by another file, if it is the culprit is most likely in the other file.
SpacePhoenix is offline   Reply With Quote
Old Nov 8, 2009, 18:05   #5
crmalibu
SitePoint Wizard
 
Join Date: Jul 2008
Posts: 4,770
Quote:
Originally Posted by SpacePhoenix View Post
There is nothing wrong with the script as posted, I tried it locally and did not get any errors so possibly something is wrong with the OPs setup.
php has a feature which can buffer output for you.
http://www.php.net/manual/en/ref.outcontrol.php

This is probably turned on automatically in your configuration files.
crmalibu is offline   Reply With Quote
Old Nov 8, 2009, 22:35   #6
co.ador
SitePoint Evangelist
 
Join Date: Apr 2009
Posts: 573
what is the OPs setup?

Yes the script I have put is included in another file, is that the cause of the problem?

I include that script in another file.

What is the culprit?


Hope this can be solved...
co.ador is offline   Reply With Quote
Old Nov 8, 2009, 23:27   #7
PHPycho
SitePoint Guru
 
PHPycho's Avatar
 
Join Date: Dec 2005
Posts: 672
Zend Framework utilizes the
<?php
//code goes here

pattern, leaving the (?>) closing tag and its main purpose is to avoid the unecessary space outputs.
PHPycho is offline   Reply With Quote
Old Nov 9, 2009, 05:40   #8
co.ador
SitePoint Evangelist
 
Join Date: Apr 2009
Posts: 573
space outputs.

I have downloaded the script and analyzed and i notice the ?> closing tag was missing. i thought they have don't a mistake so i put it back again.


Well now with the ?> closing tags and the script code below this is the error that comes alone.

Quote:
Notice: Undefined variable: REMOTE_ADDR in C:\wamp\www\nyhungry\includes\header.php on line 62
Useronline Insert Failed >
Notice: Undefined variable: PHP_SELF in C:\wamp\www\nyhungry\includes\header.php on line 74
0 users online
?>

PHP Code:

<?php 
//fill in some basic info
$server = "localhost";
$db_user = "root";
$db_pass = "trementdo";
$database = "colo";
$timeoutseconds = 300;

//get the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

//connect to database
mysql_connect($server, $db_user, $db_pass);

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR'['REMOTE_ADDR'],'$_SERVER'['PHP_SELF']"
);
if(!(
$insert)) {
print
"Useronline Insert Failed > ";
}

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!(
$delete)) {
print
"Useronline Delete Failed > ";
}

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!(
$result)) {
print
"Useronline Select Error > ";
}

//number of rows = the number of people online
$user = mysql_num_rows($result);


//spit out the results
mysql_close();
if(
$user == 1) {
print(
"$user Hungry online\n");
} else {
print(
"$user users online");
}
?>
If you notice i have changed the script a little bit.
co.ador is offline   Reply With Quote
Old Nov 9, 2009, 11:57   #9
SpacePhoenix
SitePoint Guru
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 822
You've got the ' in the wrong place:

PHP Code:

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR'['REMOTE_ADDR'],'$_SERVER'['PHP_SELF']"
);
if(!(
$insert)) {
print
"Useronline Insert Failed > ";
}
should be:

PHP Code:

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR['REMOTE_ADDR']','$_SERVER['PHP_SELF']'"
);
if(!(
$insert)) {
print
"Useronline Insert Failed > ";
}
SpacePhoenix is offline   Reply With Quote
Old Nov 9, 2009, 12:51   #10
co.ador
SitePoint Evangelist
 
Join Date: Apr 2009
Posts: 573
When I do it like you last suggested then this parse error come up

Quote:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\nyhungry\includes\header.php on line 55
Then if I put curly braces then the following erros come alone.

Quote:
Useronline Insert Failed > Useronline Delete Failed > Useronline Select Error >
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\nyhungry\includes\header.php on line 74

the three requests to the data base fail


The Insert:

PHP Code:

//insert the values

$insert = mysql_query( "INSERT INTO useronline VALUES
('$timestamp','{$_SERVER['REMOTE_ADDR']}','{$_SERVER['PHP_SELF']}'"
. mysql_error());
if(!(
$insert)) {
print
"Useronline Insert Failed > " ;
}
The Delete:

PHP Code:

//delete values when they leave

$delete = mysql_query("DELETE FROM useronline WHERE timestamp<$timeout" . mysql_error());
if(!(
$delete)) {
print
"Useronline Delete Failed > ";
}
The SELECT:

PHP Code:

//grab the results

$result = mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='{$_SERVER['PHP_SELF']}'" . mysql_error());
if(!(
$result)) {
print
"Useronline Select Error > ";
}
and as a result of not connection then mysql_num_rows fail too.

PHP Code:

//number of rows = the number of people online

$user = mysql_num_rows($result);
The error as I stated above is

Quote:
Useronline Insert Failed > Useronline Delete Failed > Useronline Select Error >
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\nyhungry\includes\header.php on line 74
The connection i think is ok...


The connection i have set up goes as follows

it stars with including constant.php in connection.php

constant.php

PHP Code:

<?php


// Database Constants
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "username");
defined('DB_PASS')   ? null : define("DB_PASS", "pass");
defined('DB_NAME')   ? null : define("DB_NAME", "colo");

?>

then in connection.php include constant.php. Inside connection.php I work the function mysql_connect

Connection.php
[code] <?php require("constant.php");
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!
$connection){
die(
"Database connection failed:" . mysql_error());
}
Then the mysql_select_db function

PHP Code:

mysql_set_charset('utf8',$connection);

$db_select = mysql_select_db(DB_NAME, $connection);
if(!
$db_select){
die(
"Database selection failed: " . mysql_error());
}
and finally the mysql_query functions which are three

PHP Code:

$insert = mysql_query( "INSERT INTO useronline VALUES

('$timestamp','{$_SERVER['REMOTE_ADDR']}','{$_SERVER['PHP_SELF']}'"
. mysql_error());
if(!(
$insert)) {
print
"Useronline Insert Failed > " ;
}


//delete values when they leave
$delete = mysql_query("DELETE FROM useronline WHERE timestamp<$timeout" . mysql_error());
if(!(
$delete)) {
print
"Useronline Delete Failed > ";
}

//grab the results
$result = mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='{$_SERVER['PHP_SELF']}'" . mysql_error());
if(!(
$result)) {
print
"Useronline Select Error > ";
}
Help please....

is the connection alright?
co.ador is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 04:28.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved