0

I am having problems getting PHP sessions to work correctly. I have the following two pages as a test. I would expect sess2.php to output 'bing' but it isn't doing.

sess1.php

<?php
session_start();
$_SESSION['usr'] = 'bing';
echo '<a href="sess2.php">go</a>';

sess2.php

<?php
session_start();
echo $_SESSION['usr'];

Am I missing something?

Peter Astbury
  • 115
  • 1
  • 1
  • 6
  • Try var_dump($_SESSION['usr']); instead echo. So you can see even if the value is null. – Subin Thomas Sep 22 '15 at 11:50
  • You have cookies enabled in your browser? – Chris Sep 22 '15 at 11:50
  • I don't think cookies must be enabled to manipulate server-side session variables – al'ein Sep 22 '15 at 11:54
  • @SubinThomas yes the value is null when I do a var_dump – Peter Astbury Sep 22 '15 at 11:56
  • @Chris yes, cookies are enabled – Peter Astbury Sep 22 '15 at 11:57
  • Please, check [all these configs](http://php.net/manual/en/session.configuration.php) on your **php.ini** file. – al'ein Sep 22 '15 at 11:58
  • var_dump($_SESSION['usr']); put this in the sess1.php file and see session is getting set or not @PeterAstbury – Subin Thomas Sep 22 '15 at 12:02
  • @SubinThomas he already said it returns null. – al'ein Sep 22 '15 at 12:02
  • @AlanMachado Not in forwarded page. In the same page session set done.. in sess1.php file.. – Subin Thomas Sep 22 '15 at 12:03
  • I think maybe can be a session store path , maybe it can be bad writte permisions or not defined , try to do echo session_save_path() , if not null then check if the the folder have write permissions – Joaquin Javi Sep 22 '15 at 12:10
  • @AlanMachado session variables all look fairly standard to me... https://www.dropbox.com/s/74i7ccd4pq340j5/phpsession.PNG?dl=0 – Peter Astbury Sep 22 '15 at 12:12
  • http://stackoverflow.com/questions/6200612/session-variables-are-not-persisting-between-page-loads describes all the possibilities in session @PeterAstbury – Subin Thomas Sep 22 '15 at 12:14
  • has the var/lib/php/session folder writte permisions? – Joaquin Javi Sep 22 '15 at 12:17
  • @JoaquinJavi I thought it did - apache has full permissions on the folder - however when, just to test, I gave full 777 permissions to that folder everything worked as expected - so apparently 'apache' isn't the user that needs the permissions. As I'm running Plesk php is run by psacln so that was the user that needed permissions - NOT apache as I had thought. Thanks! If you want to post this as an answer rather than a comment then I'll mark it as the accepted answer – Peter Astbury Sep 24 '15 at 12:45
  • Ok i will now , and firstly apologize me i didn't read deeply in you question and using mobile while i'm in transit – Joaquin Javi Sep 24 '15 at 13:11

2 Answers2

1

has the var/lib/php/session folder writte permisions?

Sometimes the user / group executing apache are not the same as the root or your owner user/group, that's depending on specific built in configuration.

Try to chmod 777 to your session storage path , you can perform it doing session_save_path()  to see the specific folder or look in your phpinfo()

Joaquin Javi
  • 880
  • 7
  • 12
  • can you reword this an an answer rather than as a followup query? That would be much more helpful to this and other people reading this question on SO. Cheers. – Martin Sep 24 '15 at 13:19
  • Thanks @JoaquinJavi this helped! I had permissions set on that folder but for the wrong user - I had assumed that the permissions were needed by apache but php wasn't being run by apache - I changed ownership of the folder to the user which php runs as and all worked as expected. – Peter Astbury Sep 24 '15 at 17:08
0

Try to modify files like this to see where the problem is:

sess1.php

<?php
session_start();
$_SESSION['usr'] = 'bing';
echo '<a href="sess2.php">go</a>';
echo '<pre>' . print_r($_SESSION, true) . '</pre>'

sess2.php

<?php
session_start();
echo '<pre>' . print_r($_SESSION, true) . '</pre>'

If you see an empty $_SESSION at sess1.php then the problem with opening and/or setting session itself; if sess1.php is fine, but sess2.php has empty $_SESSION then something wrong with passing session across requests (e.g. disabled cookies). Either way this will give you a clue on where the problem is and figure out solution from there.

Evgeniy Chekan
  • 2,615
  • 1
  • 15
  • 23
  • Thanks - the session variables weren't being stored as the folder that stores them on the server had the permissions set incorrectly. All sorted now. – Peter Astbury Sep 24 '15 at 12:47