473,508 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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;
}
?>
===========================================

Jul 17 '05 #1
3 3924
PS: I forgot to mention I have an "archaic" version of PHP installed
(4.0.2). ....

Pavel

"Pavel Vetesnik" <Pa*******@volny.cz> píše v diskusním příspěvku
news:bt**********@ns.felk.cvut.cz...
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;
}
?>
===========================================

Jul 17 '05 #2
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 wrote:
PS: I forgot to mention I have an "archaic" version of PHP installed
(4.0.2). ....

Pavel

"Pavel Vetesnik" <Pa*******@volny.cz> píše v diskusním příspěvku
news:bt**********@ns.felk.cvut.cz...
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;
}
?>
===========================================



Jul 17 '05 #3
It works now!!!!

Pavel

"Agelmar" <if**********@comcast.net> píse v diskusním príspevku
news:bt************@ID-30799.news.uni-berlin.de...
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 wrote:
PS: I forgot to mention I have an "archaic" version of PHP installed
(4.0.2). ....

Pavel

"Pavel Vetesnik" <Pa*******@volny.cz> píše v diskusním příspěvku
news:bt**********@ns.felk.cvut.cz...
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 onlyuntil 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 isimmediately 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;
}
?>
===========================================


Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
14641
by: Jeff | last post by:
I've searched the web for hours trying to figure out this problem and can't seem to find any pertinent answers. I have a website where the user starts on a login page, puts in their credentials and...
8
3958
by: ndsoumah | last post by:
hello guys I'm trying to get access to variables I put in a session variable from another page and it fails... here's the exact situation main file page1.php
2
3323
by: Amit D.Shinde | last post by:
Hello Experts.. I need some help regarding cookies and session objects and also global.asa file I am creating one cookie when a user logs in on my website. The cookie stores the login name of...
4
1660
by: Dean | last post by:
I finally got class session variables to work by putting the following in global.asax in the session_start: dim myDBComp as DBComp = new DBComp........ session("myDBComp") = myDBComp In each...
0
1321
by: Michael | last post by:
I have a problem here that only seems to happen on the one new server that we are migrating our ASP.Net application to. The server is a Windows 2000 SP4 box. It initially had the .Net Framework...
2
1790
by: Paul W | last post by:
Using default session handling (ie. inproc and with cookies). I have a 'logout' button that returns the user to the login screen and does a session.abandon. HOWEVER, if they then log back in...
8
1734
by: ari | last post by:
hey all, i'm trying to make my app as stateless as possible. is it ok to create a dataset and store in viewstate and whenever the user decides to select a from that dataset, to move from...
4
4450
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use...
10
313
by: sheldonlg | last post by:
Something weird is happening here. I inherited some code that looks like what is shown below. The problem is that when menu1.php is included, the session variables are not known in homepage.php. ...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.