Forum Moderators: coopster

Message Too Old, No Replies

session start();

Works on my server, not on host server

         

wkpride

1:42 pm on May 8, 2009 (gmt 0)

10+ Year Member



Hey,
Had a large program ready & loaded it to host server. Got an error. Tried removing blank lines & white spaces, no luck.

So, I wrote program that only starts a session and I can't get session started. Here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN"
"http://www.w3.org/TR/xhtm11/DTD/xhtm11-strict.dtd">
<html xmlns="http://www.w3org/1999/xhtml" lang="en" xml:lang="en" >
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<html>
<head>
<title>2nd</title>
</head>
<body>
<?php session_start();
echo "go figure";?>
</body>
</html>

Here are the results:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at .../2nd.php:10) in .../2nd.php on line 10

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at .../2nd.php:10) in .../2nd.php on line 10
go figure

As I said, it works on my Windows (PC) server, but gives error on the Linux - Host server.

Anything stick out as a mistake?

Thanks so much!
KP

wkpride

2:15 pm on May 8, 2009 (gmt 0)

10+ Year Member



This takes care of the problem:

<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html;charset=iso-8859-1">

So, is there any reason you wouldn't want to make the very first line of every page start with:
<?php session_start();?> ?

coopster

2:41 pm on May 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you are going to be invoking sessions and need to send headers (cookies) then nothing else can be sent to the browser first, as you have now experienced. If you need to process code and/or some form of logic first then you can do so, just don't send output to the browser first. Use buffering if you have to.

brodyh

1:45 am on May 11, 2009 (gmt 0)

10+ Year Member



It can also happen if your files are encoded as UTF-8 but you're sending them as ASCI. I think it's because what UTF-8 sees as the start of the document other charsets see as strange characters. I had this issue in the past.

enigma1

8:36 am on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




So, is there any reason you wouldn't want to make the very first line of every page start with:
<?php session_start();?> ?

Yes there are. For instance If you are referencing/storing object info to the session then their classes must be included before session_start. Another example is with spiders. You do not want to start sessions if a spider makes the request.