Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10734

Re: cron job and headers sent error

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: cron job and headers sent error
Date 2013-09-08 21:09 +0000
Organization A noiseless patient Spider
Message-ID <l0ip21$1vn$1@dont-email.me> (permalink)
References <tduo29dbnp2l23rjvvol64iveibjup6189@4ax.com>

Show all headers | View raw


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@gmail.com

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

cron job and headers sent error Richard Yates <richard@yatesguitar.com> - 2013-09-08 06:36 -0700
  Re: cron job and headers sent error "J.O. Aho" <user@example.net> - 2013-09-08 16:40 +0200
    Re: cron job and headers sent error Richard Yates <richard@yatesguitar.com> - 2013-09-08 08:35 -0700
      Re: cron job and headers sent error "Peter H. Coffin" <hellsop@ninehells.com> - 2013-09-08 14:34 -0500
      Re: cron job and headers sent error "J.O. Aho" <user@example.net> - 2013-09-08 22:30 +0200
        Re: cron job and headers sent error Richard Yates <richard@yatesguitar.com> - 2013-09-08 13:42 -0700
        Re: cron job and headers sent error T0xicCode <t0xic@co.de> - 2013-09-08 22:21 -0400
  Re: cron job and headers sent error Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-08 21:09 +0000
    Re: cron job and headers sent error Richard Yates <richard@yatesguitar.com> - 2013-09-09 04:29 -0700

csiph-web