modifiedcontent
modifiedcontent

Reputation: 59

Android browser not reading variable in PHP session

I have a PHP session script that works fine on all browsers, except Android mobile - it also works fine in Android browser if you view the desktop site. It is the same for both Chrome and MS Edge on Android.

The PHP script is a content switcher based on geo and/or slug info. I am not going to post the entire script here; it all works fine.

Here is the relevant bit from the homepage:

<?php session_start();

... bunch of stuff that produces an $n value ...

echo 'we have a content number here: ' . $n;

$_SESSION['content_number'] = $n;
}

The correct content number is echoed in all platforms, including the Android mobile browsers. The next pages should easily pick up the $n value with this:

<?php session_start();
if (isset($_SESSION['content_number'])){
    $n = $_SESSION['content_number'];
} else {
    ... cms specific homepage redirect ...; 
};

That works as expected in all browsers, but in Android mobile browser the other pages redirect to the homepage. The session content number is not recognized for some reason.

When I remove the if else condition from the next pages and just try to echo either $_SESSION['content_number'] or $n, nothing is found.

When I hardcode an $n value in the home page script - like $n = '2'; - the Android mobile browser does open the next page with the correct content:

<?php session_start();

$n = '2';

$_SESSION['content_number'] = $n;
}

Useless, but that works fine. Apparently there is nothing wrong with the sessions mechanism, cookies, etc.!

Without the if else condition in the next pages, with $n = '2'; on the homepage, echoing the session content number also works fine. The PHP session works.

What could be the reason that Android mobile can't read $n when all the other browsers can?

Are there any known issues with PHP sessions on Android mobile that I should take into consideration?

Are there other ways to set the session variable? I have tried to sanitize and process $n, but haven't found anything that makes a difference.

What else could I try to troubleshoot this? I am running out of ideas.

Edit:

var_dump of the next page on Android:

array(3) { ["Session"]=> array(0) { } ["ProcessWire1"]=> array(1) { ["SessionHandlerDB"]=> array(1) { ["ts"]=> int(1595783349) } } ["content_number"]=> NULL }

var_dump of the next page on other browsers:

array(3) { ["Session"]=> array(0) { } ["ProcessWire1"]=> array(1) { ["SessionHandlerDB"]=> array(1) { ["ts"]=> int(1595783260) } } ["content_number"]=> int(1) }

Upvotes: 0

Views: 967

Answers (1)

modifiedcontent
modifiedcontent

Reputation: 59

While trying to create an MCVE, by eliminating elements, I found "the cause" of my problem. This innocuous piece of CSS:

@media (max-width: 767px){#bg{background:url("../video/video.png") center center/cover no-repeat}#bg video{display:none}}

This is a piece from a compacted CSS file produced from SASS by Koala.

Deleting it from my style.css file fixes the problem; Android can suddenly read the correct variable from the session without issues.

The problem also disappears when I use a standard formatted version of that bit of CSS instead of this compacted version.

Why this causes Android not to read the latest variable from the session, I have no clue. It makes absolutely no sense to me. Any ideas?

The problem probably never was specifically with Android, but with mobile in general, considering the media query targets mobile. I have not tested anything on iPhone - don't have access to one at the moment.

But I don't understand how a CSS mistake can cause a problem with sessions.

Upvotes: 0

Related Questions