Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP session variable

Member
 
Join Date: Jun 2007
Posts: 37
#1: Jun 6 '07
Hi team,

I am just a beginner programmer on PHP and I was wondering if anybody can help me with this problem.

I am trying to set-up the PHP session variable but I wasnt able to print the said session variable in the next page. Here's an excerpt from the origin page. This was placed in the start of the PHP file:

<?php
session_start();
$_SESSION['session_var'] = "testing";
?>

Then I was trying to print out the session variable in the second PHP page but nothing displayed:
<?php
session_start();
$session_var = $_SESSION['session_var'];
echo "session_var =".$session_var;
?>

I am using PHP 5.2.2 and apache 2.2.4 in winXP. More FYI, I am running on Japanese OS (If this info can help). Thank you for all responses.

ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#2: Jun 6 '07

re: PHP session variable


Is there any link from the first page to second page.

[PHP]<?php
session_start();
$_SESSION['session_var'] = "testing";
print('<a href="second_page.php">second_page.php</a>');
?>
[/PHP]

How did you call for the second page after executing first.
Member
 
Join Date: Jun 2007
Posts: 37
#3: Jun 6 '07

re: PHP session variable


I have a form going from page1 to page2. Here's the code of the first page:

<?php
session_start();
$_SESSION['session_var'] = "testing";
session_register($_SESSION['session_var'] );
?>
<html>
<body>
<p>This is a test of the sessions feature
<form action="sessionTest2.php" method = "POST">
<input type="text" name="form_var" value ="testing">
<input type="submit" value="Go to the next page">
</body>
</html>

Thanks for the reply :)
Member
 
Join Date: Jun 2007
Posts: 37
#4: Jun 6 '07

re: PHP session variable


And here's the code for the second page (might as well put it) :)


<?php
session_start();
?>
<html>
<body>
<?php
$session_var = $_REQUEST['session_var'];
echo "session_var =".$session_var."<br>\n";
?>
</body>
</html>
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#5: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by twinklyblue

And here's the code for the second page (might as well put it) :)


<?php
session_start();
?>
<html>
<body>
<?php
$session_var = $_REQUEST['session_var'];
echo "session_var =".$session_var."<br>\n";
?>
</body>
</html>

It seems like the probem is with your php configuration. In my end the script is working perfectly.
run a phpinfo() and find out the session is enabled or not.
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#6: Jun 6 '07

re: PHP session variable


page 1
u SHOULDNT use session_register(); if you use $_SESSION[]
session_register register variable $_SESSION[] and $_SESSION[] trying to register itself.... bad idea:-)

get rid the line: session_register($_SESSION['session_var']);

IT SHOULD WORK FINE
i tried it :-)
regards
jx2
Member
 
Join Date: Jun 2007
Posts: 37
#7: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by ajaxrand

It seems like the probem is with your php configuration. In my end the script is working perfectly.
run a phpinfo() and find out the session is enabled or not.

Hi ajaxrand,
Tried to check it.. but the set-up is okay. Here's what I have in my phpinfo.php:(

session
Session Support enabled
Registered save handlers files user
Registered serializer handlers php php_binary wddx
Member
 
Join Date: Jun 2007
Posts: 37
#8: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by jx2

page 1
u SHOULDNT use session_register(); if you use $_SESSION[]
session_register register variable $_SESSION[] and $_SESSION[] trying to register itself.... bad idea:-)

get rid the line: session_register($_SESSION['session_var']);

IT SHOULD WORK FINE
i tried it :-)
regards
jx2

Hi jx2,

I also did what you suggested and it still didnt work. Apparently, the one that I pasted was the old code..sorry..
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#9: Jun 6 '07

re: PHP session variable


well
after i did "my"corection to your first file i tried it and it did worked :-)

what i do in a case like that i print all variables to see values - u will see when your $session_var get into truble

try a lot of echo statments
eg:
echo $session_var;
and move it in first file line by line (after $_SESSION) TO SEE WHATS GOING ON

good luck
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#10: Jun 6 '07

re: PHP session variable


and make sure there is no white space before <?php
did you get any error?
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#11: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by twinklyblue

Hi ajaxrand,
Tried to check it.. but the set-up is okay. Here's what I have in my phpinfo.php:(

session
Session Support enabled
Registered save handlers files user
Registered serializer handlers php php_binary wddx

what about the value for session.auto_start is it on or off.
Member
 
Join Date: Jun 2007
Posts: 37
#12: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by ajaxrand

what about the value for session.auto_start is it on or off.

It is set to 0. I tried to set it to 1 but still it didnt work so I reset it to 0 again :(
Member
 
Join Date: Jun 2007
Posts: 37
#13: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by jx2

and make sure there is no white space before <?php
did you get any error?

HI jx2,

I tried what you suggested. Here's the code I inserted in start of the second php file.

<?php
session_start();
echo "test ".$session_var;
?>

I would be glad if there is an error but unfortunately there was nothing displayed. Any more suggestions? :(
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#14: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by twinklyblue

HI jx2,

I tried what you suggested. Here's the code I inserted in start of the second php file.

<?php
session_start();
echo "test ".$session_var;
?>

I would be glad if there is an error but unfortunately there was nothing displayed. Any more suggestions? :(

nothing at all? it should show at least "test" on its own
my guess iss that u doung simple mistake:-) but u need to realise what is it :-)
try to type it at the end of the first file and see if there is something in $session_var

regards
jx2
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#15: Jun 6 '07

re: PHP session variable


Why don't you post the complete scripts for both the pages here in the forum.
ronnil's Avatar
Expert
 
Join Date: Jun 2007
Posts: 132
#16: Jun 6 '07

re: PHP session variable


try to echo out session_id() just below your session_start() statement. If those two are not the same, you got a problem with your browser starting a new session for each request.

Just for eleminating that possibility
Member
 
Join Date: Jun 2007
Posts: 37
#17: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by ajaxrand

Why don't you post the complete scripts for both the pages here in the forum.

Hi ajaxrand, I did post the complete code above but will post it again for easier reference.

first page:
<?php
session_start();
$_SESSION['session_var'] = "testing";
?>
<html>
<body>
<p>This is a test of the sessions feature
<form action="sessionTest2.php" method = "POST">
<input type="text" name="form_var" value ="testing">
<input type="submit" value="Go to the next page">
</body>
</html>

second page:
<?php
session_start();
echo "test ".$session_var;
?>
<html>
<body>
<?php
$session_var = $_SESSION['session_var'];
$form_var = $_POST['form_var'];
echo "session_var =".$session_var."<br>\n";
echo "form_var =".$form_var."<br>\n";
?>
</body>
</html>

This is the output:

test session_var =
form_var =testing
Member
 
Join Date: Jun 2007
Posts: 37
#18: Jun 6 '07

re: PHP session variable


Quote:

Originally Posted by ronnil

try to echo out session_id() just below your session_start() statement. If those two are not the same, you got a problem with your browser starting a new session for each request.

Just for eleminating that possibility

I also did try that and came with a null result. Any settings that I can check with the browser?
ronnil's Avatar
Expert
 
Join Date: Jun 2007
Posts: 132
#19: Jun 6 '07

re: PHP session variable


do you mean that session_id() is null? in that case your session is not registered at all.

I actually don't know how to set that configuration, since I've never had that problem myself :D
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#20: Jun 6 '07

re: PHP session variable


Double check this parameters with your configuration settings under your php.ini.just go for a phpinfo() again.

in each and every line this is the format.
Directive - Local Value - Master Value
session.auto_start - Off - Off


Expand|Select|Wrap|Line Numbers
  1. session.auto_start    Off    Off
  2. session.bug_compat_42    On    On
  3. session.bug_compat_warn    On    On
  4. session.cache_expire    180    180
  5. session.cache_limiter    nocache    nocache
  6. session.cookie_domain    no value    no value
  7. session.cookie_lifetime    0    0
  8. session.cookie_path    /    /
  9. session.cookie_secure    Off    Off
  10. session.entropy_file    no value    no value
  11. session.entropy_length    0    0
  12. session.gc_divisor    100    100
  13. session.gc_maxlifetime    1440    1440
  14. session.gc_probability    1    1
  15. session.hash_bits_per_character    4    4
  16. session.hash_function    0    0
  17. session.name    PHPSESSID    PHPSESSID
  18. session.referer_check    no value    no value
  19. session.save_handler    files    files
  20. session.save_path    C:\DOCUME~1\PRARTH~1.PC\LOCALS~1\Temp    C:\DOCUME~1\PRARTH~1.PC\LOCALS~1\Temp
  21. session.serialize_handler    php    php
  22. session.use_cookies    On    On
  23. session.use_only_cookies    Off    Off
  24. session.use_trans_sid    0    0
Member
 
Join Date: Jun 2007
Posts: 37
#21: Jun 7 '07

re: PHP session variable


Quote:

Originally Posted by ajaxrand

Double check this parameters with your configuration settings under your php.ini.just go for a phpinfo() again.

in each and every line this is the format.
Directive - Local Value - Master Value
session.auto_start - Off - Off


Expand|Select|Wrap|Line Numbers
  1. session.auto_start    Off    Off
  2. session.bug_compat_42    On    On
  3. session.bug_compat_warn    On    On
  4. session.cache_expire    180    180
  5. session.cache_limiter    nocache    nocache
  6. session.cookie_domain    no value    no value
  7. session.cookie_lifetime    0    0
  8. session.cookie_path    /    /
  9. session.cookie_secure    Off    Off
  10. session.entropy_file    no value    no value
  11. session.entropy_length    0    0
  12. session.gc_divisor    100    100
  13. session.gc_maxlifetime    1440    1440
  14. session.gc_probability    1    1
  15. session.hash_bits_per_character    4    4
  16. session.hash_function    0    0
  17. session.name    PHPSESSID    PHPSESSID
  18. session.referer_check    no value    no value
  19. session.save_handler    files    files
  20. session.save_path    C:\DOCUME~1\PRARTH~1.PC\LOCALS~1\Temp    C:\DOCUME~1\PRARTH~1.PC\LOCALS~1\Temp
  21. session.serialize_handler    php    php
  22. session.use_cookies    On    On
  23. session.use_only_cookies    Off    Off
  24. session.use_trans_sid    0    0

I think I had all the configuration setup well. Here's what I have in my file:

[Session]
session.save_handler = files
session.save_path = "C:\Temp"
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = \
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = 0
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#22: Jun 7 '07

re: PHP session variable


Quote:

Originally Posted by twinklyblue

I think I had all the configuration setup well. Here's what I have in my file:

[Session]
session.save_handler = files
session.save_path = "C:\Temp"
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = \
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = 0
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5

Check this line under your php.ini
session.cookie_path = /
in yours its "\"
Member
 
Join Date: Jun 2007
Posts: 37
#23: Jun 7 '07

re: PHP session variable


Quote:

Originally Posted by ajaxrand

Check this line under your php.ini
session.cookie_path = /
in yours its "\"

ajaxrand.. thank you very much!!! that fixed it.. whew..
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#24: Jun 7 '07

re: PHP session variable


Quote:

Originally Posted by twinklyblue

ajaxrand.. thank you very much!!! that fixed it.. whew..

Glad to hear that twinklyblue.See you next time here @ TSDN.
Reply