Results 1 to 5 of 5

Thread: sessions and doctype declaration problem

  1. #1
    Join Date
    Nov 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sessions and doctype declaration problem

    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)
    Code:
    <?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:
    Code:
    <?php
    session_start();
    ....

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default twey your words seems to be smart

    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.

  4. #4
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default found a solution

    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.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •