I am doing a PHP script check.php
that checks if a user is logged in. There is only one user, so the password is written directly in the PHP code. check.php
enters the top line (line 1) of each corresponding page with a line <? include "check.php"; ?>
<? include "check.php"; ?>
<? include "check.php"; ?>
.
The code
I deleted the password and domain name. Other than that, my code. The point here is that you enter the password on the login page, and then send it a POST to this script.
If the password is valid xxx, the login
session will keep true
.
If the password is incorrect , but set, that is, the user typed something incorrectly, any existing session ends with session_destroy()
, that is, it is logged out.
If it reaches the page but is not logged in , the login
session must be false or not installed, which means it will be used } elseif(!($_SESSION['login'])) {
.
Finally, if he clicks the logout button, he is sent to this script with the URL: check.php?logout=true
. logout=true
should be caught in $_GET
in the final elseif statement, and the session should end there.
<? ob_start(); session_start(); if($_POST['password'] == 'xxx') { // Correct password $_SESSION['login'] = true; header("Location: http://www.url.com/administration/index.php"); } elseif (isset($_POST['password'])) { // Wrong password session_destroy(); header("Location: http://www.url.com/administration/login.php?true"); } elseif(!($_SESSION['login'])) { // Check at every page header("Location: http://www.url.com/administration/login.php"); } elseif($_GET['logout']) { // Log out session_destroy(); header("Location: http://www.url.com/"); } ob_flush(); ?>
Problem
In every if statement, I'm trying to redirect. I use header("Location:...)
, but it does not work in any of the cases. Since the header
command should be the first request to be sent to the browser according to the specifications, I used ob_start();
and ob_flush();
as described here . This does not work with or without them.
There is also a problem with a session that will not store content. I cannot keep true
in the session for some reason. Is there a problem with my code that causes it to crash?
As a test, I tried to write the echo
command in each if
/ ifelse
expression. From this, I found that the script always enters the third statement - the one that has it !($_SESSION['login'])
.
Question
So far so good. This tells me that the script may detect that the session is not established.
Two problems remain:
- WHY redirection in the instruction does not work, since there is no redirection, but
- WHY a session cannot be set in the first place.
Any advice would be appreciated.
Update 1
To make it clear what is happening (and what does not happen), I added several echo
to different places. This code snippet above with extra echo
s:
... echo "Input: " . $_POST['password']; echo "<br>Session before: " . $_SESSION['login']; if($_POST['password'] == 'xxxx') { // Correct password $_SESSION['login'] = true; header("Location: http://www.url.com/administration/index.php"); echo "<br>Session after: " . $_SESSION['login']; echo "<br>The first if works"; } ...
returns the following output:
Input: xxxx Session before: Session after: 1 The first if works
(xxxx is the password and it is correct.)
This is the situation when you log in. You just wrote a password and were sent to check.php
.
So, I see here that it refers to the first if
, as expected. And the session is correctly set to true
(or 1
). When I refresh the page, the session is no longer established. Must not be?
And header
redirection obviously does nothing.
Update 2
So, thanks to the answer from @EmilF below, I found that my session ID, which I can print to the screen using echo session_id();
, changes each time the page is changed or the page is updated to some new random number. It seems to me that the data stored in the session is then forgotten because the new session identifier indicates somewhere else.
Using:
<? session_id('cutckilc16fm3h66k1amrrls96'); session_start(); ...
where cutckilc16fm3h66k1amrrls96
is just a random number, the session id is fixed, and the saved data can now be restored again after the page is refreshed. This works well; although it’s still a little strange that this is necessary.
Now I only need the header redirection to work ...
Well, it smells of something that has been disabled. Session and header settings changed. Perhaps these are some PHP settings from the host. Something that blocks the header request.
To be continued...
Update 3 - Solution
See my answer below.
Some strange characters are created at the beginning of the file when I change the file to a different encoding format, for example. from ANSI to UTF-8. Created characters 
, and I do not see them in my own editor. Since they are in front of the PHP script, they impede the operation of header
and session_start()
. Why they are created is still a mystery to me.