PHP session_id never changes when updated

I am trying to use session_id () on some php pages but the id changes between each file and it changes every time I refresh the page. I have placed the following script, which should grow on reboot, but it doesn't.

session_start();
if (!isset($_SESSION['hits'])) $_SESSION['hits'] = 0;
++$_SESSION['hits'];

echo '<p>Session hits: ', $_SESSION['hits'], '</p>';
echo '<p>Refresh the page or click <a href="', $_SERVER['PHP_SELF'],
'">here</a>.';

      

In my php.ini file I have enabled cookies and also set my save_path tp '/ tmp'.

There are session files in the actual folder ... so I know this is not a file writing issue. I also ensured that every file is utf-8 with bom for consistency.

If there are any other solutions you can think of, please help me solve this problem. It drives me crazy.

Thank!!!

+1


source to share


2 answers


3 possibilities I can think of for your situation:



  • How do you call session_id()

    ? Please include this code in your question. If you call it with any arguments, it will override the session ID with whatever argument you passed.
  • Are cookies allowed in your browser? The session ID is sent to the browser as a cookie.
  • Do you call session_destroy()

    at any moment? This will delete the session data from the server and cause a new session to start on subsequent page views.
+2


source


This is because you create a new session every time you refresh the page. You must wrap the start session statement in an if file.



if(session_id() == ''){ session_start(); }

-3


source







All Articles