[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Php file run by cron doesn't work. Get session_start error. No blanks in file. Is server working properly?

Posted on 2009-02-22
19
Medium Priority
?
564 Views
Last Modified: 2013-12-13
I've a script that works perfect, except a part of it relying on cron jobs running 3 different php files. For every run I get an error.

I've also tested with a "blank" php file run by the cron, and it produces the same error.

The php file I call test.php has only this code inside:

<?php
session_start();
echo "test";
?>

There is NO blanks inside...

I run the cron from Cpanel, with this command:
php -q /home/user/public_html/campaign/test.php

Here's the error I get in the email when running the crons: Why?
(the first error message under)

The second error message I get if I do NOT have the -q in the cron.

And WHY is the last "Expires" date 1981 ???  That I really don't understand :-)


Thanks for helping :-)


0
Comment
Question by:Maxrespons
  • 9
  • 7
  • 2
18 Comments
 

Author Comment

by:Maxrespons
ID: 23706017
I thought I did attach the error messages, but I can't see them, so I try again :-)


// This is the first error message:
 
<br /> <b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent in <b>/home/smartnco/public_html/campaign/test.php</b> on line <b>2</b><br /> <br /> <b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/smartnco/public_html/campaign/test.php:2) in <b>/home/smartnco/public_html/campaign/test.php</b> on line <b>2</b><br /> test 
 
 
// This is the second error message
 
X-Powered-By: PHP/5.2.6 Set-Cookie: PHPSESSID=2a0971a487482674bf3ba7da37bdbfc9; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-type: text/html test 

Open in new window

0
 
LVL 10

Expert Comment

by:Tobias
ID: 23706037
Dear Maxrespons,

I think you have forgot to add the error, please add it.


One question, have you changer in this line user by your username ?

php -q /home/user/public_html/campaign/test.php



Best Regards
0
 

Author Comment

by:Maxrespons
ID: 23706041
I just added the errors :-)

Yes, the crons have my correct user id in it. So the crons finds the file
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23706055
You can not use sessions with cron jobs. Sessions rely on http headers, cron is not a browser, and does not understand http.
0
 

Author Comment

by:Maxrespons
ID: 23706071
May be.

The script I bought has session_start( in the beginnig of the cron jobs file. And calls for a parameter called:
a.merchant_id = ".$_SESSION["sess_ecommerce_merchant_id"]."

I'm not much of a php coder, so I don't actuall yknow how these files are working... Too advanced for me :-)

Should I just remove the session_start() in all the three files?


PS: And I can't find the writer of the code... Therefore I come her efor help :-)
0
 
LVL 10

Expert Comment

by:Tobias
ID: 23706089
Dear,

I don't understand why you mean that in the second error message is one error. Because you get the return of the page.

X-Powered-By: PHP/5.2.6 Set-Cookie: PHPSESSID=2a0971a487482674bf3ba7da37bdbfc9; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-type: text/html for the session_start(); and the test for the echo test.

This is normal that the expire date is to 1981 is set by default that when you close the browser the session expire, then is for that, that you get one date in the past.

You could edit the php.ini to change the session.cookie_lifetime.

Best Regards
0
 

Author Comment

by:Maxrespons
ID: 23706110
@ MadShiva:
You see, I didn't know that.

The last message (that you refer to) comes when I do NOT have the -q in the cron path. Is this the correct path? (My host told me to have the -q inside, but they might be wrong...)

However the cript doesn't send any emails, even though that's the mission of the files run by crons.

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 1000 total points
ID: 23706116
>> Should I just remove the session_start() in all the three files?

Yes. And you can not use $_SESSION in the scripts.
0
 

Author Comment

by:Maxrespons
ID: 23706129
Ok. Then the coder must have done an error.

In the instructions for the install it tells specific to run these files with cron. Since one of the files has the $_SESSION inside, I guess I should just call it as an image url inside the merchants interfence then... (?)

I'll try to remove the session_start() from the other files and run them, then try to run the last file from a browser...
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23706203
A session implies more than one request to the scripts, one to set one or more session variables, the next to read and use these variables. A cron job is just one "request". If you need persistence, i.e. to remember something from one job run to the next, use the database or files to store the "session data".

>> call it as an image url inside the merchants interfence

I do not understand this...

A typical php request will return a html page, but a cron job can not output anything, it is run in the background and does not have a screen. You can output to a file, though. And you can acces the database and send email.
0
 

Author Comment

by:Maxrespons
ID: 23706213
I ment like this:
<img src="http://mydomain.com/campaign/campaign.launched.php" width="0" height="0" border="0">
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23706267
I don't know what your cron job is supposed to do. Like I said, it can not output a html page. It has no screen to write to, and no browser to send the result to. You can however write html to a file.

If you explain what the cron job is supposed to do, it would be easier for me to give usefull advice. :)
0
 

Author Comment

by:Maxrespons
ID: 23706352
:-)

The cron should trigger an email sending of emails that has passed a specific date. It's a system putting emails in sequences for following up customers.

The very php file runs a sql, and finds those emails that should be sent that has not yet been sent.

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23706407
Ok. Then there is no need for a session. I suppose the <img> goes into the email.
0
 

Author Closing Comment

by:Maxrespons
ID: 31549822
Hi! Thanks for helping me. :-)
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23709383
Why do you give me a B grade?  I told you what the problem was and how to fix it.

http://www.experts-exchange.com/help.jsp#hi403
http://www.experts-exchange.com/help.jsp#hi404
0
 

Author Comment

by:Maxrespons
ID: 23709447
I thought that was the appropiate answer. I may have misunderstood how this works. I'm sorry... :-(

I am satisfied with the answer, so I'll ask the mods to change it to an A.


0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23709454
Thanks! :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question