Log in

View Full Version : sessions and doctype declaration problem



pjunod
11-04-2007, 01:47 AM
I have a webpage coded for strict xhtml 1.0. I have a php require_once statment for an authentication page, and the first thing on it is a session start function, which needs to be output first.

The problem is that both the doctype and the session headers need to go first in order for them to both be happy. What is the solution to this? It can't be that uncommon of a problem, but i haven't been able to find any other references to this specific problem. (if i leave the require once statement first, it works as needed, but the homepage doesn't validate as valid xhtml. if i put the require once after the doctype, then i get session errors because the doctype has already been sent)

I know i could leave the require_once statement first and it would be fine, but the homepage does not validate as xhtml like that because the w3c parser is expecting a doctype declaration first. I don't want to leave it like this because it bugs me knowing there is probably another way to do it correctly and i would like to find this way.

My homepage is a php page because it has alot of dynamically-generated content. All of the webpage is output for strict xhtml 1.0 though.

Start of homepage (index.php)


<?php require_once 'checklogin.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
....


Start of checklogin.php page:


<?php
session_start();
....

Twey
11-04-2007, 02:11 AM
I have a webpage coded for strict xhtml 1.0.You are using the wrong content type. The content type for XHTML is application/xhtml+xml. You need to specify it in the HTTP headers, not as a <meta> tag:
header('Content-Type: application/xhtml+xml; charset=utf-8');If you don't, it won't be parsed as XHTML, and you'll be essentially serving invalid HTML. If you do, the page will no longer work in Internet Explorer, which doesn't support XHTML yet. If you want support for Internet Explorer, you must use HTML.

Put the PHP include first. Since the PHP is evaluated server-side, the first thing the user agent sees will be the DOCTYPE, unless the PHP produces output.

papartiska
09-21-2010, 01:44 PM
The same problem doctype declaration and session_start must be first, it is on top of the script to make everything work properly. The problem exists only with IE. Twey your word are too smart can you give more detailed example how to solve this problem, can you give the first script code lines, how it should look? English in not my mothertongue language so I am not sure if I understand your explanation. So far your solution doesnt work. thanks in advance I hope I'll find the solution in this forum.

papartiska
09-21-2010, 02:38 PM
in my case solution is this:

create php file for doctype declaration (doctype.php):

<?php
echo'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
';
?>

include this file to your main web site script:

<?php
session_start();
require_once ('doctype.php');
....

I'll be glad if I'll help someone with this problem.

jscheuer1
09-21-2010, 03:10 PM
Twey's not around anymore - I hope he comes back soon though, he is very smart.

However, here I don't think he would mind too much if I said that his answer is not to the point.

The specific DOCTYPE used really isn't the issue, except to purists. Technically speaking he's right, you should be using the HTML 5 DOCTYPE (unavailable or too experimental when he wrote the above). But any valid URL DOCTYPE will also do, as long as the page validates to it, or does so nearly enough to be parsed as desired by the browser.

Sending the header he recommends (as he notes) will make the page unusable in IE 8 and less in all cases except if imported to another page with some very fancy javascript.

What you need to do is ensure that the DOCTYPE is the very first thing the browser sees and that the session start is the very first thing the server sees.

What you have done satisfies those two conditions, so of course it works.