-1

I include this code on top of my php webpage

<?php
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
session_start();
?>

here is php webpage
a.php

<h1>Page AAAA</h1>
<a href="b.php?t=<? echo time(); ?>">go to page B</a>
<?php
   echo "<h2>CURRENT SESSION = ", $_SESSION['test'], "</h2>";
   $_SESSION['test'] = 'a';
   echo "<h2>AFTER CHANGE SESSION = ", $_SESSION['test'], "</h2>";
?>

b.php

<h1>Page BBBB</h1>
<a href="a.php?t=<? echo time(); ?>">go to page A</a>
<?php
   echo "<h2>CURRENT SESSION = ", $_SESSION['test'], "</h2>";
   $_SESSION['test'] = 'b';
   echo "<h2>AFTER CHANGE SESSION = ", $_SESSION['test'], "</h2>";
?>

but it not work. CURRENT SESSION is no value even if I switch between a.php and b.php

update I include this code in a.php
echo "<h1>SESSION ID = ", session_id() , "</h1>";
result
SESSION ID = fhhiilg08rl2ajtplqc9dbud43

hsgu
  • 834
  • 4
  • 9
  • 18
  • Try`!session_id() ? session_start() : null` and set error reporting on to see what error is occurring – shawndreck Jan 02 '13 at 10:01
  • 1
    What does "on top of my php webpage" mean? Are you starting the session in a.php and b.php? Try activating error reporting if you haven't already. – deceze Jan 02 '13 at 10:01
  • [**Read this**](http://www.w3schools.com/php/php_sessions.asp) – Pankit Kapadia Jan 02 '13 at 10:05
  • I recommend remove `?>` in the included PHP snippet. See http://stackoverflow.com/questions/4410704/php-closing-tag – Xiao Jia Jan 02 '13 at 10:05
  • I mean copy that code and place on top of php webpage files before html tag start – hsgu Jan 02 '13 at 10:07
  • @hsgu you have write session_start() at the beginning of both the pages i.e a.php and b.php and on every single page where youi want to use session – sandip Jan 02 '13 at 10:13
  • I want output CURRENT SESSION = a after I click go to page B from page A but now result on my server side CURRENT SESSION = NOTHING – hsgu Jan 02 '13 at 10:16
  • @hsgu have you written session_start() on both the a.php & b.php pages? – sandip Jan 02 '13 at 10:21
  • I have certainly write session_start(); in both page. It's very depress to get down vote because I can't find the cause of my strange result. Am I ask the stupid question? – hsgu Jan 02 '13 at 10:29

2 Answers2

2

Open your php.ini file and set

session.use_cookies=1
session.use_only_cookies=1

So by setting this you will not require config other files

And just start session on pages where you want to assess session variables by

session_start() 
code_fish
  • 3,381
  • 5
  • 47
  • 90
sandip
  • 3,279
  • 5
  • 31
  • 54
  • Now I edit php.ini session.use_cookies=1 already and I let session.use_only_cookies=1 out of comment but session still not work correctly. – hsgu Jan 02 '13 at 10:34
  • @hsgu I tried your code on my system I got answer as Page BBBB go to page A CURRENT SESSION = a Page AAAA go to page B CURRENT SESSION = b AFTER CHANGE SESSION = a AFTER CHANGE SESSION = b when I clicked on go to page a link I got output as – sandip Jan 02 '13 at 10:43
  • Thank so much. I try it with my friend as well my server got nothing on CURRENT SESSION but my friend got it fine what should I do at this point. – hsgu Jan 02 '13 at 10:44
  • @hsgu try by restarting your webserver! – sandip Jan 02 '13 at 10:47
  • WOWWWWWW!!!! I restart webserver and it's work fine thank!!! – hsgu Jan 02 '13 at 11:06
0

Just use session_start(); function on top of the page then assign value to $_SESSION['test'] = "value". This show session value on all the pages.

Pankit Kapadia
  • 1,579
  • 13
  • 25
prasobh
  • 95
  • 3