Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Session problem: session exists, but session variables don't lasts

0 views
Skip to first unread message

Pavel Vetesnik

unread,
Jan 12, 2004, 6:43:26 AM1/12/04
to
Greetings!

I have some troubles with session variables. I can easily create a session,
but the variable I append to global session array ( $_SESSION) lasts only
until the end of current function.

I prepared an example that can demonstrate the problem (see code bellow):
The program flow is this:
(1) It starts with "session_start()" and displays "login_form()".
(2) When user enters his username and password, it goes to "login()"
function which sets $_SESSION variables.
(3) This function also "Echoes" session_id and user name to the screen.
(4) Then it goes to "MyProgram()" function. It only "Echoes" the same
variables to the screen.

Strange is, that the session_id is the same in (3) and (4), but the user
name ($_SESSION["username"]) is only in (3), but not in (4). By other
words - $_SESSION["username"] variable filled up by "login()" function is
immediately cleared.

I don't know why.

There must be something missing, but after 3 days of watching it, I can not
find a problem here.

I will really appreciate any help!
Pavel

===========================================
<?
Function login($uzivatel, $heslo)
{
$_SESSION["username"] = $uzivatel;
$_SESSION["pass"] = $heslo;
$U_Name=$_SESSION['username'];
echo "Login username: $U_Name<BR>";
echo "Login session ID: ";
echo session_id();
echo "<BR>";
}

Function login_form()
{
echo ("<DIV ALIGN='center'>
<H1>Test</H1>
<P>Please enter your username and password:</P>
<form action='test.php' method='get'>
<INPUT TYPE='HIDDEN' NAME='login' VALUE=TRUE>
<TABLE BORDER='0'>
<TR>
<TD ALIGN='right'><U>U</U>ser name:</TD>
<TD><INPUT NAME='uzivatel' SIZE=25 ACCESSKEY='U' TYPE='TEXT'></TD>
</TR>
<TR>
<TD align='right'><U>P</U>assword:</TD>
<TD><INPUT NAME='heslo' SIZE=25 ACCESSKEY='P' TYPE='PASSWORD'></TD>
</TR>
<TR>
<TD COLSPAN=2 ALIGN='RIGHT'><INPUT NAME='OK' ACCESSKEY='O' TYPE='SUBMIT'
VALUE='OK' TABINDEX=3></TD>
</TR>
</TABLE>
</FORM></DIV></HTML>");
}

Function MyProgram()
{
$U_Name=$_SESSION['username'];
echo "Program username: $U_Name<BR>";
echo "Program session ID: ";
echo session_id();
echo "<BR>";
}

{
session_start();
if ($_SESSION['username']):
MyProgram();
else:
{
if (!$login):
login_form(0);
else:
login($uzivatel, $heslo);
MyProgram();
endif;
}
endif;
}
?>
===========================================

Pavel Vetesnik

unread,
Jan 12, 2004, 10:49:00 AM1/12/04
to
PS: I forgot to mention I have an "archaic" version of PHP installed
(4.0.2). ....

Pavel

"Pavel Vetesnik" <Pave...@volny.cz> píąe v diskusním příspěvku
news:btu0v6$6hi$1...@ns.felk.cvut.cz...

Agelmar

unread,
Jan 12, 2004, 11:19:43 AM1/12/04
to
Uhm... yeah... upgrade your version of PHP. Asking for help on archaic
versions is often futile. New versions are released to correct bugs,
etc! Upgrade!!!

P.S. $_POST, $_SESSION, $_GET, $_FILES, $_REQUEST, $_ENV weren't added
until 4.1.0.

Upgrade to 4.3.4 dude...

Pavel Vetesnik

unread,
Jan 12, 2004, 12:18:27 PM1/12/04
to
It works now!!!!

Pavel

"Agelmar" <ifette...@comcast.net> píse v diskusním príspevku
news:btuhf0$blo4g$1...@ID-30799.news.uni-berlin.de...

0 new messages