0

This is my code:

session_start();

if(isset($_SESSION['name'])) { 
  echo $_SESSION["name"] ;
}else{
   $_SESSION["name"] = 'Test';
   echo 'Just inserted: '.$_SESSION["name"] ;
}

I expect that only the first time it shows "Just inserted: Test" and then it should shows only "Test".

But on my server it always display "Just inserted: Test".

Can someone tell me why ?

EDIT:

New code:

var_dump(session_start());
var_dump(session_name()); 
var_dump(session_id());


if(isset($_SESSION['name'])) { 
  echo $_SESSION["name"] ;
}else{
   $_SESSION["name"] = 'Test';
   echo 'Just inserted: '.$_SESSION["name"] ;
}

phpinfo();
xRobot
  • 25,579
  • 69
  • 184
  • 304
  • On your server or on your local machine? – Yang Aug 19 '15 at 04:04
  • Then try to check if session can be started. by doing `var_dump(session_start());` – Yang Aug 19 '15 at 04:07
  • I don't understand why this code is not working for you. Its working for me. – Muntashir Akon Aug 19 '15 at 04:12
  • @MuntashirAkon This works for everyone. He has issues with php.ini on server machine – Yang Aug 19 '15 at 04:15
  • Try `phpinfo();` and find `session` – Muntashir Akon Aug 19 '15 at 04:21
  • Everything seems to be perfect. When we call `session_start()` php automatically set a cookie to the browser (called `PHPSESSID`). But in your case, there wasn't any cookie at all. Try `var_dump(session_start());` instead of just `session_start()`. It should return `bool(true)`. – Muntashir Akon Aug 19 '15 at 04:50
  • But it still didn't save any cookies. Try this `var_dump(session_name());` & this `var_dump(session_id());`. The first one should return session name and the second one should return session id. – Muntashir Akon Aug 19 '15 at 05:13
  • Ok, this is the last comment. Try this `setcookie(session_name(),session_id(),time()+3600);` after `session_start()`. This should set the cookie which is for some peculiar reasons not working. – Muntashir Akon Aug 19 '15 at 05:19
  • It's working on our machine as you expected. Could you please tell me the session information from your phpinfo? – Domain Aug 19 '15 at 05:31
  • So, the problem is not with `session` at all but with cookie. Try one of these: [PHP - setcookie(); not working](http://stackoverflow.com/questions/20316870/php-setcookie-not-working) Or [PHP – setcookie() not working](http://stackoverflow.com/questions/24662580/php-setcookie-not-working) or [PHP Cookies not working](http://stackoverflow.com/questions/19260793/php-cookies-not-working) – Muntashir Akon Aug 19 '15 at 05:48
  • @MuntashirAkon ok thanks :) – xRobot Aug 19 '15 at 05:52
  • check out the value of session.gc_maxlifetime() in your php.ini – SpongePablo Aug 19 '15 at 06:20
  • Try following line into your php.ini file: ini_set('session.gc_maxlifetime', 24*60*60); I think this might work for you, hope so... – Domain Aug 20 '15 at 06:03

0 Answers0