Sessions doesnt work on 404 page

Hello, I got problem on my site with sessions. When I access page which doesn’t exists my error page is unable to load current session, but this only happens when I load my page as ErrorDocument. When I access page directly, then everything appear ok. I don’t think there can be anything wrong with my .htaccess, but rather posting its code:

   # HTID:xxxxxxx: DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES BELOW
    php_value display_errors 1
    # DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES ABOVE HTID:xxxxxxx:

    ErrorDocument 404 /404.php

Hi @GrowtopiaNoobs!

You had to add output_buffering flag in .htaccess. I have added it for you.

Try it now.

1 Like

This didn’t helped. Session still don’t work.

Sessions do work. If your website does not behave as expected that’s most likely coding issues…

2


3

I don’t feel like it is coding issue. Navbar should give different options when user is logged in and when he is not. When you log on at page https://trainer-core.000webhostapp.com/login.php (with account test and password test), you can notice that navbar looks different, even it should look same: https://trainer-core.000webhostapp.com/some-page-which-doesnt-exists https://trainer-core.000webhostapp.com/404.php

I have just checked it: after logging in and browsing the error file directly, the navbar displays properly: https://trainer-core.000webhostapp.com/404.php

.htaccess contains server-side directives, not PHP ones, therefore based on certain rules Apache can null sessions/headers which are coming from PHP.

Did you tried your code locally?

No, I didn’t tried my code localy, I am using 000webhost for this. So, if I understand to you right this is excepted behavior and only way to solve this is just to redirect to error page when 404 happens?

You can redirect to whatever page you want when any error code occurs, however if you are doing the redirect from .htaccess directly, sessions effect will be annulled.

1 Like