MidOhioIT Posted July 30, 2010 Share Posted July 30, 2010 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209305-cannot-send-session-cookie-headers-already-sent/ Share on other sites More sharing options...
Alex Posted July 30, 2010 Share Posted July 30, 2010 Make sure you have no whitespace on the page before <?php. Quote Link to comment https://forums.phpfreaks.com/topic/209305-cannot-send-session-cookie-headers-already-sent/#findComment-1092912 Share on other sites More sharing options...
MidOhioIT Posted July 30, 2010 Author Share Posted July 30, 2010 Alex, Thanks but that was not it. It also looks like it only has the issue running as cron Quote Link to comment https://forums.phpfreaks.com/topic/209305-cannot-send-session-cookie-headers-already-sent/#findComment-1093077 Share on other sites More sharing options...
Adam Posted July 30, 2010 Share Posted July 30, 2010 Odd. What does the crontab look like? Quote Link to comment https://forums.phpfreaks.com/topic/209305-cannot-send-session-cookie-headers-already-sent/#findComment-1093082 Share on other sites More sharing options...
MidOhioIT Posted July 30, 2010 Author Share Posted July 30, 2010 php -q full_path_to_script>>/dev/null Quote Link to comment https://forums.phpfreaks.com/topic/209305-cannot-send-session-cookie-headers-already-sent/#findComment-1093094 Share on other sites More sharing options...
Alex Posted July 30, 2010 Share Posted July 30, 2010 The problem when running it as a cron might be the paths; try making them absolute. Quote Link to comment https://forums.phpfreaks.com/topic/209305-cannot-send-session-cookie-headers-already-sent/#findComment-1093170 Share on other sites More sharing options...
PFMaBiSmAd Posted July 30, 2010 Share Posted July 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209305-cannot-send-session-cookie-headers-already-sent/#findComment-1093186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.