skip to Main Content

Very simple bit of code below works on several apache/php servers but not on the one used by my commercial hosting service. (I will be asking them about this soon, but thought I might a quicker response here) The code increments and displays the “counter” variable every time I reload (refresh) the page – except on my commercial hosting service (which will remain nameless for the time being – big one) Heres the code:

<?php

session_start();
$_SESSION['counter']++;

   echo 'counter: '.$_SESSION['counter'].' ';
?>

The PHP version on my commercial host is 7.3.xx Nothing happens when I refresh the page. The lamp and wamp stacks I have tried this on are 7.3.xx also – and it also works on an older installation I have that runs PHP 5.4

When it works, the pages increments the number, on my commercial host, it remains “1” every time I refresh.

3

Answers


  1. Chosen as BEST ANSWER

    Turns out that there is some ghost character or some such thing in the files that I send to my host service that causes the problem. It is strange that there is no other peculiar behavior that has emerged, but I will be investigating ways to scrub the files that will alleviate the problem. Right now, "diff" shows a very cryptic response when comparing code that works and code that does not:

    diff test2.php test3.php 1d0 < 32a32


  2. PHP Sessions save data to the filesystem by default. You need to make sure the path the data is saved to (check your php.ini) is accessible and writable for you.
    Alternativly you can try setting a different path you know is writable using

    ini_set('session.save_path', '/path/to/your/folder')
    
    Login or Signup to reply.
  3. Probably the file that’s not working is saved as UTF-8 with BOM. In this case the BOM is considered to be the first part of the output and you can’t use session_start() when output is already generated.

    See here
    And here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search