470,848 Members | 1,784 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Problem with session variables

This is a excerpt from the code where I collect data with a form.

<FORM action="index15dq.php" method="post" name="stegscf_pwd_edit"
id="stegscf_pwd_edit">

I am setting three session variables to already declared variables. I then
echo them and can see on the resulting form that they are set correctly.

<?
session_start();
$_SESSION['scf_upd_pwd'] = $scfmpwd1;
$_SESSION['scf_upd_num'] = $scfchknum;
$_SESSION['scf_upd_nam'] = $scfmfor;
echo $_SESSION['scf_upd_pwd'];
echo $_SESSION['scf_upd_num'];
echo $_SESSION['scf_upd_nam'];
?>

On the processing page - index15dq.php - I have this code.

<?
error_reporting(E_ALL);
session_start();
$scfmpwd1 = $_SESSION['scf_upd_pwd'];
$scfchknum = $_SESSION['scf_upd_num'];
$scfmfor = $_SESSION['scf_upd_nam'];
print "values";
echo $scfmpwd1;
echo $scfchknum;
echo $scfmfor;
?>

Its echoing blank values. No errors reported.

Shouldn't this work? How can I force session varables to work. Does it
matter where session_start is declared?

My pages are complex. First I open an index which looks at certain key
values, the page is then dynamicaly put together using segments of php code,
certain segments are omited or included according to which page is being
loaded. Much of these form the surrounding information, the headers, the
pictures and banners and the meny bar on the left of the screen.). All of
which can vary accordning to which page is being loaded.

Eventually the nitty-gritty of the page is loaded in the centre.

It is here that I am a) declaring loading the session variables.
and b) trying to use the loaded session variables.

Somewhere along the way they are being unloaded.

Garry Jones
Sweden
Oct 18 '06 #1
5 2071
Rik
Garry Jones wrote:
This is a excerpt from the code where I collect data with a form.

<FORM action="index15dq.php" method="post" name="stegscf_pwd_edit"
id="stegscf_pwd_edit">

I am setting three session variables to already declared variables. I
then echo them and can see on the resulting form that they are set
correctly.

<?
session_start();
$_SESSION['scf_upd_pwd'] = $scfmpwd1;
$_SESSION['scf_upd_num'] = $scfchknum;
$_SESSION['scf_upd_nam'] = $scfmfor;
echo $_SESSION['scf_upd_pwd'];
echo $_SESSION['scf_upd_num'];
echo $_SESSION['scf_upd_nam'];
>>

On the processing page - index15dq.php - I have this code.

<?
error_reporting(E_ALL);
session_start();
$scfmpwd1 = $_SESSION['scf_upd_pwd'];
$scfchknum = $_SESSION['scf_upd_num'];
$scfmfor = $_SESSION['scf_upd_nam'];
print "values";
echo $scfmpwd1;
echo $scfchknum;
echo $scfmfor;
>>

Its echoing blank values. No errors reported.

Shouldn't this work? How can I force session varables to work. Does it
matter where session_start is declared?

Yes. print_r($_SESSION); to check what your session actually holds.
Be warned that session_start() has to be declared BEFORE any output, or it
won't work. So not in 'the centre' of your page, but as one of the first
things 'on top'. Even sending out a simple space or line-break before
session_start() will render it useless.

Another word of advice: don't rely on shorttags: always use <?php instead
of <?
--
Grtz,

Rik Wasmus
Oct 18 '06 #2
"Rik" <lu************@hotmail.comskrev i meddelandet
news:a0***************************@news2.tudelft.n l...
Yes. print_r($_SESSION); to check what your session actually holds.
Be warned that session_start() has to be declared BEFORE any output, or it
won't work. So not in 'the centre' of your page, but as one of the first
things 'on top'. Even sending out a simple space or line-break before
session_start() will render it useless.
Done that and I am getting this. (Since I posted I turned error reporting on
in a setting my web hotel service allows users to change).

Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at
/customers/scfmotion.se/scfmotion.se/httpd.www/sitedefs.php:67) in
/customers/scfmotion.se/scfmotion.se/httpd.www/scfma_edt_pwd.php on line 34

Warning: session_start() [function.session-start]: Cannot send session cache
limiter - headers already sent (output started at
/customers/scfmotion.se/scfmotion.se/httpd.www/sitedefs.php:67) in
/customers/scfmotion.se/scfmotion.se/httpd.www/scfma_edt_pwd.php on line 34

So what does
"session cookie" and "session cache limiter" - mean?

Okay, it does seem like I have to move session_start somewhere else in the
code. Thats a bit of a nightmare because the code itself should only have
session_start based on a couple of conditions. These conditions are met when
that php code segment is called for. I am going to have to make a copy of
the very same if statements higher up and start the session earlier. That
should do it if I have understood you correctly.

Garry Jones
Oct 18 '06 #3
Rik
Garry Jones wrote:
Okay, it does seem like I have to move session_start somewhere else
in the code. Thats a bit of a nightmare because the code itself
should only have session_start based on a couple of conditions. These
conditions are met when that php code segment is called for. I am
going to have to make a copy of the very same if statements higher up
and start the session earlier. That should do it if I have understood
you correctly.
Indeed.
Things like headers, actions to take, etc, I usually do before any HTML
output at all.
--
Grtz,

Rik Wasmus
Oct 18 '06 #4
Rik" <lu************@hotmail.comskrev i meddelandet
news:76***************************@news2.tudelft.n l...
Garry Jones wrote:
Indeed.
Things like headers, actions to take, etc, I usually do before any HTML
output at all.
Hmmm I got one of my forms working. Did a similar thing to another one but
am still having problems

On the form page these two variables are echoed correctly.
<?= $_SESSION['scf_upd_num'] ?>
<?= $_SESSION['scf_upd_nam'] ?>

On the form page and the processing page I load the session start write at
the begining, first line of code.

But on the processing page these rows

error_reporting(E_ALL);
print_r($_SESSION);
echo $_SESSION['scf_upd_num'];
echo $_SESSION['scf_upd_nam'];

produce this output

Notice: Undefined variable: _SESSION in
/customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 5
Notice: Undefined variable: _SESSION in
/customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 6
Notice: Undefined variable: _SESSION in
/customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 7

Note that the form is processed by - FORM action="index15dt.php" - a file
which uses a segment called cent15dt.php which can be seen in the above
example. I am surprised to see the name of the segment in the code because
the index15dt.php is the start of the code that puts the entire page
together.

Any ideas?

Garry Jones
Sweden
Oct 18 '06 #5
Rik
Garry Jones wrote:
Notice: Undefined variable: _SESSION in
/customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 5

This is an error you'll get when you fail to give a correct
session_start(). This is mandatory on EVERY page. Be sure to:

- include session_start() in your script as one of the first command in
every file offcourse.
- enable error_reporting before the session_start();
- beware of type-errors :-)
--
Rik Wasmus
Oct 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by ZoombyWoof | last post: by
1 post views Thread by Jonathan Chong | last post: by
5 posts views Thread by Newton | last post: by
6 posts views Thread by Scott Zabolotzky | last post: by
8 posts views Thread by Ashish | last post: by
reply views Thread by Alexander Widera | last post: by
3 posts views Thread by stclaus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.