Jump to content

Cannot send session cookie - headers already sent


MidOhioIT

Recommended Posts

I am confused why I am getting the following error and was wondering if I could get a 2nd pair of eyes..

error:

[29-Jul-2010 20:58:01] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent in /home/curren11/public_html/mysite***.com/file_monitor.php on line 2

[29-Jul-2010 20:58:01] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/curren11/public_html/mysite***.com/file_monitor.php:2) in /home/curren11/public_html/mysite***.com/file_monitor.php on line 2


 

Here is the code:

<?php
session_start();

$root_path = realpath(getcwd());
$error_monitor_file = $root_path.'/file_modify.txt';
$hour = date("H");
$today = date("m-d-Y");



     $myFile2 = "file_monitor.php.log";
     $fh3 = fopen($myFile2, 'a');
     $log_msg = "Last ran on: ".$today." at: ".$hour." \n";
     fwrite($fh3, $log_msg);
    

function scan_dir($path)
{	
$out = array();	
$files = opendir($path);	

while ($f = readdir($files))
{	
  $extension = substr($f,-3);
  	  	
   if ($f=='.' or $f=='..' or $extension=='jpg' or $extension=='JPG' or $extension=='png' or $extension=='gif' or $f=='file_monitor.php' or $f=='file_modify.txt' or $f=='file_monitor.php.log' or $f =='error_log'  ) 
     {
	   continue;	
	 }	

  if (is_dir($path.'/'.$f))
    {
	  $out = array_merge($out, scan_dir($path.'/'.$f));
	} 		
  //if  (((!is_dir($path.'/'.$f)) && ($f!='.' or $f!='..' or $extension!='jpg' or $extension!='JPG' or $extension!='png' or $extension!='gif'))
  else
  //  {
	    $out[] = $path. '/'. $f;
	//}
}	

  $_SESSION['all_files'] = $out;
  
return $out;
}

$a = scan_dir(getcwd());

$count_array = count($_SESSION['all_files']);
$loop_count=0;
while($loop_count < $count_array)
{
  // file in directory when was it last changed ?
  $filechange1 = date("m-d-Y-H", filemtime($_SESSION['all_files'][$loop_count]));

// was it chnaged today and have I sent out an alert yet?
if($filechange1 == $today."-".$hour) 							
  {
$fh2 = fopen($error_monitor_file, 'a');
fwrite($fh2, "\n alert, file: ".$_SESSION['all_files'][$loop_count]." was changed today within the last hour ! \n");
    fclose($fh2);
  }
 $loop_count ++;	
 // log file? was it changed this past hour?
 if (file_exists($error_monitor_file))
 {
  	$filechange_log = date("m-d-Y-H", filemtime($error_monitor_file));

		if ( ($loop_count == $count_array ) && ($filechange_log == $today."-".$hour ) )
		{
     		 	email();
    		}	
}				
}

// function for email...
function email()
{
require_once ('class.phpmailer.php');

$mail = new PHPMailer();

$mail->IsSMTP();                                   // send via SMTP
$mail->Host     = "localhost"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "**c66@***ar.com";  // SMTP username
$mail->Password = "*****"; // SMTP password
$mail->From     = "***@***.com";
$mail->FromName = "Colonial Cottage File Changed";
$mail->AddAddress("email@*****.com"); 

$mail->WordWrap = 50;                              // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); 
$mail->IsHTML(true);                               // send as HTML

$mail->Subject  ="Unauthorized file changed today!";
$mail->Body     =  "" ;

$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send())
   {
     exit();
   }
}

if ((file_exists($error_monitor_file)) && $hour == "00")
{
   unlink($error_monitor_file);
}
fclose($fh3);
exit();
?>

running as cron

 

Session_start() and using $_SESSION variables in a script running as a cron job don't make any sense and won't work because there is no browser to propagate the session id between invocations of the script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.