FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » cron job and headers sent error
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: cron job and headers sent error [message #182773 is a reply to message #182767] Sun, 08 September 2013 21:09 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Sun, 08 Sep 2013 06:36:45 -0700, Richard Yates wrote:

> I have a script that runs automatically every night that shortens some
> tables. It has been working fine (Actually still is doing what it is
> supposed to). However I started getting an email warning notice when it
> runs. In says headers have already been sent in line two of the database
> Connection script that is included.


> ------- Connections/gleanslo.php
>
> <?php if(!isset($_SESSION)) session_start(); if($_SERVER["SERVER_NAME"]
> == "localhost" && $_SERVER["SERVER_ADDR"]
> == "127.0.0.1") {

The initial blank line causes headers to be output in an http context,
and then the start of the document is sent (in this case, a blank line).

The session_start() normally triggers a session cookie which is part of
the headers.

The error message is saying "I met a condition that requires me to send a
header (session_start() requiring that I send a session cookie) but I've
finished sending headers and started sending the document body already.

Easiest solution is to get rid of the blank line at the top of the file.
More complex solution would be to do the above (which may need doing
anyway) plus turn off the session code if you're not running in an http
context (ie for the cron job).

The php_sapi_name call might help, I'm not sure if it returns "cli" for a
cron job, but it should be simple enough to test.

http://www.php.net/manual/en/function.php-sapi-name.php

If it does return "cli" for cron jobs[1], then you could use something
like:

api = php_sapi_name();

if ( api != "cli"[2] ) {

// session stuff here

}

to prevent code that is only relevant in the http context being executed
in the cron job.

[1] It might return something else, but as long as it's not the same as
anything you get in an http context, it should be usable for this purpose.

[2] Obviously if your testing shows that a cron job returns an api of eg
"cron" or "cron-cli", change the comparison here.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: [Mac OS X ML] how to install MongoDB driver
Next Topic: Trouble-shooting a script that doesn't retuen HTML
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 01:38:02 GMT 2024

Total time taken to generate the page: 0.05149 seconds