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
Switch to threaded view of this topic Create a new topic Submit Reply
cron job and headers sent error [message #182767] Sun, 08 September 2013 13:36 Go to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
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. I cannot figure out how
anything is being sent. The connection script does not output
anything. Line 2 identifies whether the script is running on the local
or production server. Any ideas where the error is coming from? Here
are the warning message, the cron script and the included connection
script:

-------- warning notice

<b>Warning</b>: session_start() [<a
href='function.session-start'>function.session-start</a>]: Cannot send
session cookie - headers already sent in
<b>/home/twpalygj/public_html/Connections/gleanslo.php</b> on line
<b>2</b><br />

------- cronjobs/cron.php

<?php
// site maintenance. shortens various database tables daily
require_once('../public_html/Connections/gleanslo.php');
require_once('../public_html/includes/dencode.inc.php');
// truncate mailarchive to last month
$prevdate= date('Y-m-d', strtotime("-30 days"));
$query="delete from mailarchive where whensent<'$prevdate'";
$rsQuery=mysqli_query($gleanslo, $query);
// truncate loginlog to last week
$prevdate= date('Y-m-d', strtotime("-7 days"));
$query="delete from loginlog where datein<'$prevdate'";
$rsQuery=mysqli_query($gleanslo, $query);
?>

------- Connections/gleanslo.php

<?php if(!isset($_SESSION)) session_start();
if($_SERVER["SERVER_NAME"] == "localhost" && $_SERVER["SERVER_ADDR"]
== "127.0.0.1") {
// on local server
$hostname_gleanslo = "localhost";
$database_gleanslo = "gleanslo";
$username_gleanslo = "root";
$password_gleanslo = "********";
} else {
// on production server
$hostname_gleanslo = "localhost";
$database_gleanslo = "twpalygj_gleanslo";
$username_gleanslo = "twpalygj_dyates";
$password_gleanslo = "********";
}
$gleanslo = mysqli_connect($hostname_gleanslo, $username_gleanslo,
$password_gleanslo, $database_gleanslo);
date_default_timezone_set('America/Los_Angeles');
$queryt="set time_zone='-8:00'";
$rsTime=mysqli_query( $gleanslo, $queryt);
// get all custom variables to insert into pages
if(!isset($_SESSION['custom']) or isset($_GET['ed'])) {
$cusquery="select * from custom";
$rsCustom=mysqli_query($gleanslo,$cusquery);
$orgrow=mysqli_fetch_assoc($rsCustom);
$_SESSION['custom']=$orgrow;
} // end of isset session custom
$orgrow=$_SESSION['custom'];
?>
Re: cron job and headers sent error [message #182768 is a reply to message #182767] Sun, 08 September 2013 14:40 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 08/09/13 15:36, 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. I cannot figure out how
> anything is being sent. The connection script does not output
> anything. Line 2 identifies whether the script is running on the local
> or production server. Any ideas where the error is coming from? Here
> are the warning message, the cron script and the included connection
> script:
>
> -------- warning notice
>
> <b>Warning</b>: session_start() [<a
> href='function.session-start'>function.session-start</a>]: Cannot send
> session cookie - headers already sent in
> <b>/home/twpalygj/public_html/Connections/gleanslo.php</b> on line
> <b>2</b><br />

This is a warning you usually get when you have white spaces or bom
sequence in your files. Check all your files that are used, that they do
not have anything before or after the PHP tages (<?php and ?>).

As you are anyway running a cron job, why do you call it over tcp/ip?

I would recommend you to rewrite the cron job to just connect to the
database and do it's job, do have error handling in the script which
gives you proper explanations why something goes wrong if something does
that. A good thing is also log to syslog so you can see the job has been
properly done and you can also forward that to splunk or similar.

--

//Aho
Re: cron job and headers sent error [message #182769 is a reply to message #182768] Sun, 08 September 2013 15:35 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Sun, 08 Sep 2013 16:40:30 +0200, "J.O. Aho" <user(at)example(dot)net>
wrote:

> On 08/09/13 15:36, 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. I cannot figure out how
>> anything is being sent. The connection script does not output
>> anything. Line 2 identifies whether the script is running on the local
>> or production server. Any ideas where the error is coming from? Here
>> are the warning message, the cron script and the included connection
>> script:
>>
>> -------- warning notice
>>
>> <b>Warning</b>: session_start() [<a
>> href='function.session-start'>function.session-start</a>]: Cannot send
>> session cookie - headers already sent in
>> <b>/home/twpalygj/public_html/Connections/gleanslo.php</b> on line
>> <b>2</b><br />
>
> This is a warning you usually get when you have white spaces or bom
> sequence in your files. Check all your files that are used, that they do
> not have anything before or after the PHP tages (<?php and ?>).

Thanks. I will check again for spaces. What is 'bom sequence'?

> As you are anyway running a cron job, why do you call it over tcp/ip?

I don't know what you mean by calling it over tcp/ip. It is set up via
cPanel and runs automatically on the domain host server.

> I would recommend you to rewrite the cron job to just connect to the
> database and do it's job,

Do you mean putting the connection script directly into the cron
script rather than using an includes()?

> do have error handling in the script which
> gives you proper explanations why something goes wrong if something does
> that.

The folder on the production server is outside of the 'public_html'
root folder so I cannot dorectly run it in my browser. I have to test
it inside another folder on my local server.

> A good thing is also log to syslog so you can see the job has been
> properly done and you can also forward that to splunk or similar.

The domain host has the cron job in a folder that I can upload to. The
error_log there has the same warning message that was sent. I do not
see any syslog. I do not know what splunk is.
Re: cron job and headers sent error [message #182770 is a reply to message #182769] Sun, 08 September 2013 19:34 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Sun, 08 Sep 2013 08:35:16 -0700, Richard Yates wrote:

> On Sun, 08 Sep 2013 16:40:30 +0200, "J.O. Aho" <user(at)example(dot)net>
> wrote:
>
>> This is a warning you usually get when you have white spaces or bom
>> sequence in your files. Check all your files that are used, that they
>> do not have anything before or after the PHP tages (<?php and ?>).
>
> Thanks. I will check again for spaces. What is 'bom sequence'?

"byte order marker", which several editors will stick on the front of
text files that contain unicode (including utf-8) data, for purposes of
identifying that file as containing unicode text. Nice for some things,
horrible for PHP because it may/will damage the output in some way.
Especially by wrecking your opportunity to set your own headers.

http://en.wikipedia.org/wiki/Byte_order_mark

--
38. If an enemy I have just killed has a younger sibling or offspring
anywhere, I will find them and have them killed immediately,
instead of waiting for them to grow up harboring feelings of
vengeance towards me in my old age. --Anspach's Evil Overlord list
Re: cron job and headers sent error [message #182771 is a reply to message #182769] Sun, 08 September 2013 20:30 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 08/09/13 17:35, Richard Yates wrote:
> On Sun, 08 Sep 2013 16:40:30 +0200, "J.O. Aho" <user(at)example(dot)net>
> wrote:
>
>> On 08/09/13 15:36, 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. I cannot figure out how
>>> anything is being sent. The connection script does not output
>>> anything. Line 2 identifies whether the script is running on the local
>>> or production server. Any ideas where the error is coming from? Here
>>> are the warning message, the cron script and the included connection
>>> script:
>>>
>>> -------- warning notice
>>>
>>> <b>Warning</b>: session_start() [<a
>>> href='function.session-start'>function.session-start</a>]: Cannot send
>>> session cookie - headers already sent in
>>> <b>/home/twpalygj/public_html/Connections/gleanslo.php</b> on line
>>> <b>2</b><br />
>>
>> This is a warning you usually get when you have white spaces or bom
>> sequence in your files. Check all your files that are used, that they do
>> not have anything before or after the PHP tages (<?php and ?>).
>
> Thanks. I will check again for spaces. What is 'bom sequence'?

I think Peter made a really good explanation, so I won't repeat it here.


>> As you are anyway running a cron job, why do you call it over tcp/ip?
>
> I don't know what you mean by calling it over tcp/ip. It is set up via
> cPanel and runs automatically on the domain host server.

Your error message contains HTML and uses session_start(), which are
something you will not use if you run the script properly as a cron job,
those I guess you made a HTTP/HTTPS request to the script in question.


>> I would recommend you to rewrite the cron job to just connect to the
>> database and do it's job,
>
> Do you mean putting the connection script directly into the cron
> script rather than using an includes()?

I mean it to use script which is just made for CLI which do not relay on
session_start() as it's not used in CLI. Sure you can use includes, but
see to only include configuration for the database, not scripts relaying
on web requests.


>> do have error handling in the script which
>> gives you proper explanations why something goes wrong if something does
>> that.
>
> The folder on the production server is outside of the 'public_html'
> root folder so I cannot dorectly run it in my browser. I have to test
> it inside another folder on my local server.

Rule number one, set up a test environment which is similar to your
production environment, those you will see that the scripts will work
when you move it from your test environment to production environment.

Today there is VirtualBox which can work really well to test things,
just remember to only have those packages installed in the test
environment as you have in live.


>> A good thing is also log to syslog so you can see the job has been
>> properly done and you can also forward that to splunk or similar.
>
> The domain host has the cron job in a folder that I can upload to. The
> error_log there has the same warning message that was sent. I do not
> see any syslog. I do not know what splunk is.

syslog is on the systems /var/log directory, you don't seem to have
access there, but your web hosting company has setup system so that you
get access to logs belonging to your site in your home directory instead.

For logging use syslog() see more about it at www.php.net/syslog

Splunk is a platform for collecting, searching, monitoring and analyzing
machine data. Many times used to centralize your logs and make it easier
to search for information. Most likely you don't need it as you only
have a hosted service.

--

//Aho
Re: cron job and headers sent error [message #182772 is a reply to message #182771] Sun, 08 September 2013 20:42 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
Thank you for the detailed info.

On Sun, 08 Sep 2013 22:30:26 +0200, "J.O. Aho" <user(at)example(dot)net>
wrote:

> On 08/09/13 17:35, Richard Yates wrote:
>> On Sun, 08 Sep 2013 16:40:30 +0200, "J.O. Aho" <user(at)example(dot)net>
>> wrote:
>>
>>> On 08/09/13 15:36, 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. I cannot figure out how
>>>> anything is being sent. The connection script does not output
>>>> anything. Line 2 identifies whether the script is running on the local
>>>> or production server. Any ideas where the error is coming from? Here
>>>> are the warning message, the cron script and the included connection
>>>> script:
>>>>
>>>> -------- warning notice
>>>>
>>>> <b>Warning</b>: session_start() [<a
>>>> href='function.session-start'>function.session-start</a>]: Cannot send
>>>> session cookie - headers already sent in
>>>> <b>/home/twpalygj/public_html/Connections/gleanslo.php</b> on line
>>>> <b>2</b><br />
>>>
>>> This is a warning you usually get when you have white spaces or bom
>>> sequence in your files. Check all your files that are used, that they do
>>> not have anything before or after the PHP tages (<?php and ?>).
>>
>> Thanks. I will check again for spaces. What is 'bom sequence'?
>
> I think Peter made a really good explanation, so I won't repeat it here.
>
>
>>> As you are anyway running a cron job, why do you call it over tcp/ip?
>>
>> I don't know what you mean by calling it over tcp/ip. It is set up via
>> cPanel and runs automatically on the domain host server.
>
> Your error message contains HTML and uses session_start(), which are
> something you will not use if you run the script properly as a cron job,
> those I guess you made a HTTP/HTTPS request to the script in question.
>
>
>>> I would recommend you to rewrite the cron job to just connect to the
>>> database and do it's job,
>>
>> Do you mean putting the connection script directly into the cron
>> script rather than using an includes()?
>
> I mean it to use script which is just made for CLI which do not relay on
> session_start() as it's not used in CLI. Sure you can use includes, but
> see to only include configuration for the database, not scripts relaying
> on web requests.
>
>
>>> do have error handling in the script which
>>> gives you proper explanations why something goes wrong if something does
>>> that.
>>
>> The folder on the production server is outside of the 'public_html'
>> root folder so I cannot dorectly run it in my browser. I have to test
>> it inside another folder on my local server.
>
> Rule number one, set up a test environment which is similar to your
> production environment, those you will see that the scripts will work
> when you move it from your test environment to production environment.
>
> Today there is VirtualBox which can work really well to test things,
> just remember to only have those packages installed in the test
> environment as you have in live.
>
>
>>> A good thing is also log to syslog so you can see the job has been
>>> properly done and you can also forward that to splunk or similar.
>>
>> The domain host has the cron job in a folder that I can upload to. The
>> error_log there has the same warning message that was sent. I do not
>> see any syslog. I do not know what splunk is.
>
> syslog is on the systems /var/log directory, you don't seem to have
> access there, but your web hosting company has setup system so that you
> get access to logs belonging to your site in your home directory instead.
>
> For logging use syslog() see more about it at www.php.net/syslog
>
> Splunk is a platform for collecting, searching, monitoring and analyzing
> machine data. Many times used to centralize your logs and make it easier
> to search for information. Most likely you don't need it as you only
> have a hosted service.
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 next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
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
Re: cron job and headers sent error [message #182774 is a reply to message #182771] Mon, 09 September 2013 02:21 Go to previous messageGo to next message
T0xicCode is currently offline  T0xicCode
Messages: 2
Registered: September 2013
Karma: 0
Junior Member
On 13-09-08 04:30 PM, J.O. Aho wrote:
> On 08/09/13 17:35, Richard Yates wrote:
>> On Sun, 08 Sep 2013 16:40:30 +0200, "J.O. Aho" <user(at)example(dot)net>
>> wrote:
>>
>>> On 08/09/13 15:36, 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. I cannot figure out how
>>>> anything is being sent. The connection script does not output
>>>> anything. Line 2 identifies whether the script is running on the local
>>>> or production server. Any ideas where the error is coming from? Here
>>>> are the warning message, the cron script and the included connection
>>>> script:
>>>>
>>>> -------- warning notice
>>>>
>>>> <b>Warning</b>: session_start() [<a
>>>> href='function.session-start'>function.session-start</a>]: Cannot send
>>>> session cookie - headers already sent in
>>>> <b>/home/twpalygj/public_html/Connections/gleanslo.php</b> on line
>>>> <b>2</b><br />
....cut...
>>> I would recommend you to rewrite the cron job to just connect to the
>>> database and do it's job,
>>
>> Do you mean putting the connection script directly into the cron
>> script rather than using an includes()?
>
> I mean it to use script which is just made for CLI which do not relay on
> session_start() as it's not used in CLI. Sure you can use includes, but
> see to only include configuration for the database, not scripts relaying
> on web requests.
I think the best idea would be to do proper separation of the various
components of the system. Have the session data in it's own world, the
database data in its own world, etc.

This way, he can continue having a single location for the database
connection, without having it do another 20 extra things.
>>> do have error handling in the script which
>>> gives you proper explanations why something goes wrong if something does
>>> that.
>>
>> The folder on the production server is outside of the 'public_html'
>> root folder so I cannot dorectly run it in my browser. I have to test
>> it inside another folder on my local server.
If you can, have your cron jobs run as scripts directly on the server,
not from pages on your website.
Re: cron job and headers sent error [message #182776 is a reply to message #182773] Mon, 09 September 2013 11:29 Go to previous message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Sun, 8 Sep 2013 21:09:22 +0000 (UTC), Denis McMahon
<denismfmcmahon(at)gmail(dot)com> wrote:

> 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).

Thanks. There is no blank line at the top of the file. The line:
'------- Connections/gleanslo.php and the space after it 'are a
separator I put in the post to the make the different code sections
clear.

I removed the if(!isset($_SESSION)) session_start(); line from the
connection script that is called by the cron job and did not get an
error warning this mornng.

Thank you to all that responded.
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Nov 27 17:12:22 GMT 2024

Total time taken to generate the page: 0.02565 seconds