简体   繁体   中英

Why does session_start(); work on every file except the 404 error page?

I have recently been developing my website and I have run into a problem: On every page, I have a php script that checks if the user is logged in or not and shows the according content. This works on every page on localhost, however when I uploaded it to 000webhost, the 404 error page doesn't work and all the others do.
By "doesn't work" I mean that it shows the login button weather the user is logged in or not.
Here is my code:

<?php
session_start();
?>
<a href="/login.php" style="display:<?php 
if(isset($_SESSION['username'])){echo 'none';} ?>;">Login</a>
<a href="/logout.php" style="display:<?php 
if(!isset($_SESSION['username'])){echo 'none';} ?>;">Logout</a>

This is the same code on every single page on the website but for some reason, the 404 page doesn't work.
PS: It is not the fact that it is an error page because other errors like 403 work just fine.

Since a 404 happens when the web server cannot find a document, it uses a fallback document defined by the web server. I do not know the details of your web server (is it Apache?), but you should research how to change the document used for the 404 error. The default is probably an HTML file. If this is the case, the web server is (in a typical configuration) not going to process any PHP in the file since it only passes PHP files to the PHP parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM