By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,666 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,666 IT Pros & Developers. It's quick & easy.

can I still send cookies after I've called session_start()

P: n/a
I was trying to set a cookie before I called session_start() and it was
giving me an error. But isn't sessions really just a cookie? Why would
it matter if I sent a cookie before session_start? Can I set them
afterwards?

Jul 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
You *can* set a cookie without calling session_start(). Do you mean a
session variable? You can't do this without calling session_start():
$_SESSION['sessionvar'] = 'value';
But you can do this:
setcookie('cookiename', 'value', time()+36000);
and then read the cookie like this:
$_COOKIE['cookiename'];

http://www.w3schools.com/php/php_cookies.asp

Jul 21 '05 #2

P: n/a
>You *can* set a cookie without calling session_start(). Do you mean a
session variable? You can't do this without calling session_start():
$_SESSION['sessionvar'] = 'value';


No, I meant, I was setting a cookie, then calling session_start(), and
I was getting an error. Can I call session_start() and then set a
cookie? I want to set a permanent cookie that will last one million
seconds. I also want sessions.

Jul 21 '05 #3

P: n/a
On 21 Jul 2005 08:29:01 -0700, lk******@geocities.com wrote:
I was trying to set a cookie before I called session_start() and it was
giving me an error.
Your post is missing a vital piece of information.
But isn't sessions really just a cookie? Why would
it matter if I sent a cookie before session_start?
It doesn't.
Can I set them afterwards?


Yes, and before.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 21 '05 #4

P: n/a
JDS
On Thu, 21 Jul 2005 11:43:55 -0700, lkrubner wrote:
No, I meant, I was setting a cookie, then calling session_start(), and
I was getting an error. Can I call session_start() and then set a
cookie? I want to set a permanent cookie that will last one million
seconds. I also want sessions.


AND WHAT IS THE ERROR????

sheesh

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 22 '05 #5

P: n/a
On Thu, 21 Jul 2005 11:43:55 -0700, lkrubner wrote:
No, I meant, I was setting a cookie, then calling session_start(), and
I was getting an error. Can I call session_start() and then set a
cookie? I want to set a permanent cookie that will last one million
seconds. I also want sessions.


AND WHAT IS THE ERROR????


This is the error I'm getting:

<b>Warning</b>: session_start(): Cannot send session cookie - headers
already sent in
<b>/home/httpd/vhosts/publicdomainsoftware.org/httpdocs/pdsIncludes/CommandStartSession.php</b>
on line <b>14</b><br />
<br />
<b>Warning</b>: session_start(): Cannot send session cache limiter -
headers already sent in
<b>/home/httpd/vhosts/publicdomainsoftware.org/httpdocs/pdsIncludes/CommandStartSession.php</b>
on line <b>14</b><br />
This is line 14 and 15 of CommandStartSession:

session_start();
session_register();
I used my text editor (RedCrimson) and did a search of all the files in
this project, looking for session_start(). I thought maybe, by
accident, I was calling it twice. But no, it only appears once.

Oddly, when I call session_start(), output buffering is on, so I'm not
sure how I get that error. Later I set a cookie, then I sent a charset
header indicating that the characters are UTF-8, then I end output
buffering.

I thought the output buffering would keep me safe from this kind of
error, but apparently not.

Aug 5 '05 #6

P: n/a
>On Thu, 21 Jul 2005 11:43:55 -0700, lkrubner wrote:
No, I meant, I was setting a cookie, then calling session_start(), and
I was getting an error. Can I call session_start() and then set a
cookie? I want to set a permanent cookie that will last one million
seconds. I also want sessions.


AND WHAT IS THE ERROR????


You can see the problem here:

http://www.publicdomainsoftware.org/

Aug 5 '05 #7

P: n/a
lk******@geocities.com wrote:
On Thu, 21 Jul 2005 11:43:55 -0700, lkrubner wrote:
No, I meant, I was setting a cookie, then calling session_start(), and
I was getting an error. Can I call session_start() and then set a
cookie? I want to set a permanent cookie that will last one million
seconds. I also want sessions.


AND WHAT IS THE ERROR????

You can see the problem here:

http://www.publicdomainsoftware.org/


<br />
<b>Warning</b>: session_start(): Cannot send session cookie - headers already
sent in

You're sending <br /> before the session_start call.

You can't send ANYTHING (including a blank line) before the session_start() call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 6 '05 #8

P: n/a
On Fri, 05 Aug 2005 21:42:15 -0500, Jerry Stuckle <js*******@attglobal.net>
wrote:
lk******@geocities.com wrote:
On Thu, 21 Jul 2005 11:43:55 -0700, lkrubner wrote:

No, I meant, I was setting a cookie, then calling session_start(), and
I was getting an error. Can I call session_start() and then set a
cookie? I want to set a permanent cookie that will last one million
seconds. I also want sessions.

AND WHAT IS THE ERROR????

You can see the problem here:

http://www.publicdomainsoftware.org/


<br />
<b>Warning</b>: session_start(): Cannot send session cookie - headers already
sent in

You're sending <br /> before the session_start call.


That's part of the PHP error message:

andyh@server:/usr/src/php-5.0.4/main$ nl -ba main.c | grep -2 '<br />'
736 char *append_string =
INI_STR("error_append_string");
737 char *error_format = PG(html_errors) ?
738 "%s<br />\n<b>%s</b>: %s in
<b>%s</b> on line <b>%d</b><br />\n%s"
739 : "%s\n%s: %s in %s on line
%d\n%s";
740 php_printf(error_format,
STR_PRINT(prepend_string), error_type_str, buffer, error_filename,
error_lineno, STR_PRINT(append_string));

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Aug 6 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.