4

I have given up on this question as I suspect something corrupted my settings causing the problems and have since then created a new server. I figured finding the problem would take me more time than starting a new server and setting it up. However I'm still interested in finding out what caused this as it was running perfectly for 3 months and only started to show these symptoms over the weekend. **

===============================================================================

I'm having a bit of a beginner problem, probably because I've been working in other languages for the past months and I just can't seem to find out why this isn't working. I've made a very simple script to test it out, which goes as follows:

<?php session_start();
  print_r($_SESSION);
  if(isset($_SESSION['views'])){
      $_SESSION['views'] = $_SESSION['views']+ 1;
  }
  else{
      $_SESSION['views'] = 1;
  }
  echo "views = ". $_SESSION['views'];
  echo '<p><a href="">Refresh</a></p>';

  # for testing
  var_dump($_SESSION);

?>

What happens is that the var_dump shows that a session is created with a key of views and the value of 1. However when refreshing the page no active session is found, and the views key is set to 1 again.

Now the strange thing to me is that there is a session id created under PHPSESSID. I've tried to check if the path to store the sessions in was writable and it is. The headers returned by the page are as follows:

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0, public
Connection  close
Content-Encoding    gzip
Content-Length  68
Content-Type    text/html; charset=utf-8
Date    Wed, 18 Jan 2012 13:03:34 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Pragma  no-cache
Server  Apache
Vary    Accept-Encoding
X-UA-Compatible IE=Edge,chrome=1

And in the request header the session cookie is send as follows:

 Cookie PHPSESSID=0e1416r7pun3pamvc7cp8mjat3

I'm not sure if there is anything wrong in there. I am running this on an Amazon EC2 linux AMI.

I'll also include phpinfo():

PHP Version 5.3.6
System  Linux ip-**-**-**-** *.*.**.**-**.**.amzn1.x86_64 #1 SMP Sat Feb 19 23:42:04 UTC 2011 x86_64 
session
Session Support     enabled    
Registered save handlers    files user    
Registered serializer handlers  php php_binary wddx    
Directive   Local Value Master Value    
session.auto_start  Off Off
session.bug_compat_42   Off Off
session.bug_compat_warn Off Off
session.cache_expire    180 180
session.cache_limiter   nocache nocache
session.cookie_domain   no value    no value
session.cookie_httponly On  Off
session.cookie_lifetime 0   0
session.cookie_path /   /
session.cookie_secure   Off Off
session.entropy_file    no value    no value
session.entropy_length  0   0
session.gc_divisor  1000    1000
session.gc_maxlifetime  1440    1440
session.gc_probability  1   1
session.hash_bits_per_character 5   5
session.hash_function   0   0
session.name    PHPSESSID   PHPSESSID
session.referer_check   no value    no value
session.save_handler    files   files
session.save_path   /var/lib/php/session    /var/lib/php/session
session.serialize_handler   php php
session.use_cookies On  On
session.use_only_cookies    On  On
session.use_trans_sid   0   0

Any help would be greatly appreciated as I've been spending all day to solve this but can't seem to find the solution. If you need any more information, please let me know.

Marc Hoogvliet
  • 1,500
  • 1
  • 15
  • 18
  • 1
    Confirm your session directory must be writable .. – Rikesh Jan 18 '12 at 13:26
  • 1. Can you see your session file under /tmp? 2. What's your client/browser? – Teson Jan 18 '12 at 13:27
  • I can see the session file in the "/var/lib/php/session" directory in the form of "sess_"+id. I've tested this in all browsers except IE and Opera, and also on my iphone, both through WiFi and mobile connection. All with the same result PS. I've just tried to opens the session file with vim (don't know if this should be possible), but it is an empty file. – Marc Hoogvliet Jan 18 '12 at 13:35
  • Try this link: – M Ram Kumar Jan 18 '12 at 13:37
  • I've checked all answers but they don't seem to work for me, I've also used that question for the simple example shown in my question. – Marc Hoogvliet Jan 18 '12 at 14:04

3 Answers3

2

You should check session.use_cookies in your php.ini file.

Headers of the response doesn't have a COOKIE.

session.use_cookies have to be equals to 1.

Fabio Buda
  • 769
  • 2
  • 7
  • 16
  • As I can see from your edits you have session.use_cookies = 1 yet. As said by Michiel Thalen "Check if the Session id changes". – Fabio Buda Jan 18 '12 at 13:30
  • It seems like phpinfo() is showing "On" for session.use_cookies – Marc Hoogvliet Jan 18 '12 at 13:31
  • your code works for me showing $views variable always +1 for every refresh. Are you sure your browser sends the COOKIE back to the server?! – Fabio Buda Jan 18 '12 at 13:40
  • I think it does because the session id doesn't change on refreshing the page. – Marc Hoogvliet Jan 18 '12 at 13:52
  • marcus!!! session id mustn't change for the same user... otherwise how to know the user is the same??!! try to add print $_COOKIE['PHPSESSID']; and see if the sessionID is set and is the same. – Fabio Buda Jan 18 '12 at 13:58
  • Yes I know, but it doesn't change so it seems the session_id is stored/saved but all data belonging to it is not? Is that possible? – Marc Hoogvliet Jan 18 '12 at 14:02
  • [sorry I had misunderstood your previous comment] By the way your session is ok (SessID doesn't change) so the problem is, as you said, with session saved data ('views'). You said that the session Id file is empty: your problem is of course with writing data. Check if the sessID file[s] are in the Apache group and are writable by the group. – Fabio Buda Jan 18 '12 at 14:08
  • terminal shows the following: drwxrwxrwx 2 root apache 12288 Jan 18 13:55 /var/lib/php/session on a file it returns the following: -rw------- 1 apache apache 0 Jan 18 14:14 /var/lib/php/session/sess_4708fl64eebdcqtlscij367en7 – Marc Hoogvliet Jan 18 '12 at 14:13
  • 1
    it seems correct, be sure your Apache group is "apache". In some distributions (like Ubuntu) the group is www-data or httpd. – Fabio Buda Jan 18 '12 at 14:30
  • Thank you for your help, I've decided to start over with a new server as I couldn't get it to work, it sent me back some weeks but I still know what I've done so I'm able to set it up way quicker. – Marc Hoogvliet Jan 18 '12 at 16:28
1

Check if the Session id changes (with session_id() ). Further, you can abbreviate the line:

$_SESSION['views'] = $_SESSION['views']+ 1;

With

$_SESSION['views'] += 1;
Michiel Thalen
  • 300
  • 2
  • 12
0

When you create a link to the same page you're on, the entire page will not be refreshed. You will have to do this via JavaScript.

In HTML (or PHP echo):

<button type='button' onclick='refreshPage()'>Refresh</button>

Now set the function in JavaScript:

function refreshPage(){
document.location.reload(true);
}

When JavaScript executes this, the entire page will refresh and all PHP lines will be run again.

Another way to try this is to link to another page, and link back to the first page, then the entire page is loaded again too.