PDA

View Full Version : php Session vars passing



wjudy
9th November 2004, 12:06 AM
Hi,
Please help solve my problem about session vars passing between pages

Env: Fedora 2 with php 4.3.8 & mysql

F1.php --- page1
<?php
session_start();
$conn=mysql_connect('host','usr','pw');
if (!$conn) die(....);
mysql_select_db('mydb',$conn);
$result = mysql_query(.....); #successfully create temporary table

$_SESSION['db_conn'] = $conn;
$_SESSION['pt_code'] ='yahoo2';
?>

F2.php ---page2
<?php
session_start();
echo 'db_conn=' .$_SESSION['db_conn'] . '<br>';
echo 'pt_code=' .$_SESSION['pt_code'] . '<br>';

?>

Result from page2:
db_conn=
pt_code=
This means the session vars are not passed.

But I read session file in /tmp as following:

db_conn|i:0;pt_code|s:6:"yahoo2";

What's wrong? :confused:

IceNine
9th November 2004, 12:24 AM
try a
print_r($_SESSION); on page 2

wjudy
9th November 2004, 12:40 AM
Hi, IceNine:
Thanks for your help.
I tried. the result is "Array()"

:confused:

IceNine
9th November 2004, 12:48 AM
That confirms your Session vars are not being populated. Have you look at your Session ID info?

IceNine
9th November 2004, 12:49 AM
Also, can you show me your phpinfo()?

wjudy
9th November 2004, 01:33 AM
Hi, Icenine:
I put code at begining of page2
<? php
if(!session_id())
{ session_start();
phpinfo();
}
.......

The result: it prints phpinfo
Then I check the session dir, find the web creates a lot of session files w var ids, even I keep in one log session. That means it creats one session for each page. Why?

phpinfo:


HTTP Response Headers
X-Powered-By PHP/4.3.4
Set-Cookie PHPSESSID=00a5940a69c1791acb51dee1202ca2d2; path=/
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma no-cache
Connection close
Transfer-Encoding chunked
Content-Type text/html; charset=UTF-8

Session Support enabled
Registered save handlers files user

Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
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.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp/php/session /tmp/php/session
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off

wjudy
9th November 2004, 08:28 PM
hi,
Thanks IceNine, I followed the direction to trace. but I still can solved the problem. Even turn on client side cookie. sessionID sent by header. Where is the sessionID saved?
in my page1 code:
<?php
session_cashe_limiter('private_no_expire');
session_set_cookie_params(0,'/tmp');
session_start();
....
?>
Is it sessionID saved in client-side cookie area? If yes, why not use pure cookie instead of session?
If not, where is it saved ? Can i see it? :confused:
in my 2nd page:
<? php
if (!seesion_id())
{ session_start();
echo phpinfo();
}
....
?>
it always get in if block. creating a new sessionID.
I am very dummy. Please help!
Thanks

dlightle
9th December 2004, 03:35 PM
I don't know if you copy-pasted this or if you re-typed it, but you have a typo that would significantly effect the output:


in my 2nd page:
<? php
if (!seesion_id())
{ session_start();
echo phpinfo();
}

Also, in this code:


F1.php --- page1
<?php
session_start();
$conn=mysql_connect('host','usr','pw');
if (!$conn) die(....);
mysql_select_db('mydb',$conn);
$result = mysql_query(.....); #successfully create temporary table

$_SESSION['db_conn'] = $conn;
$_SESSION['pt_code'] ='yahoo2';
?>

You might add an echo line for the $conn just before you store it in the session. I'm definitely no pro at PHP, but I have had sessions working once. Your code looks like it should work at a glance, though. You might also try commenting out lines more and more, limiting the other factors until you end up with the most minimal lines (ie. just the session assignments). Then you can work backwards to see what is wrong.