2

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 Answers3

0

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')
restlessmodem
  • 81
  • 1
  • 6
  • Thanks, Christopher. Doesn't seem to have an effect. My hosting provider information seems to indicate that is not a configurable option. I don't get an error when running your suggestion, but I also don;'t see anything created in the directory I specify, and phpinfo doesn't reflect that the ini_set took. The Hopefully I can talk to them tomorrow and get a resolution - I will post it if I do. – Keith Simpson Oct 07 '19 at 01:23
0

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

0

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

Florian Schwalm
  • 166
  • 1
  • 3