Session variables in php

software development

#1

When I declare and call session variables, this error message always comes up:
Warning: Cannot send session cookie - headers already sent by (output started at /home/.decker/threerr/threeriversrambler.com/res/page1.php:5) in /home/.decker/threerr/threeriversrambler.com/res/page1.php on line 17

My code is:

<? session_start(); session_register("sess_var"); $sess_var="Hello"; echo " the value of sess_var is $sess_var
."; ?>

Does anyone know of special syntax or file permisions needed to set session vars in DH? Or a way to work around the automatic php session that causes this error?

thanks,
brad


#2

One thing to check is that every file used in this page call (i.e. every file that is included by any file) has no white space before or after the <? and ?> markers. That means no empty line and no spaces before or after those markers.

Those are enough to send output to the browser and thus make it impossible to send headers.

–David B.
“greendavid”


#3

David,
I was embedding the php code after html header and body tags. Once I put the <? on the first line, then the html, all was fine.

Many thanks for the clue!

Brad


#4

Happy to be of service!

–David B.
“greendavid”