By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

Session problem

P: n/a
leo
Hi , i have a problem with session variables.
The web site was already working correctly using sessions, i've had to
add a page which use a new variable session but this new one doesn't
get registered, so when the page redirect to another one the new
variable is vanished.
Here a snippet of the code:

<?php

unset($_SESSION['tipopreventivo']);

//debug
$hand=fopen("public/test.txt","w");
fwrite($hand,print_r($_POST,true));
fclose($hand);
//

if(!empty($_POST))
{
$error=array();
session_register('typep');
$_SESSION['type']=($_POST['type']?$_POST['type']:'Default');

//debug
$hand=fopen("public/session.txt","w");
fwrite($hand,print_r($_SESSION,true));
fclose($hand);
//

if($_POST['info']==1)
{
$url="Location: prev.php";
header($url);
}

}
after submit text.txt e session.txt are right, in particular in
session.txt the variable $_SESSION['type'] is set ad has a value but
after the redirect to prev.php $_SESSION['type'] is not set
The sessions are working becouse other variables are passed correctly.
Any idea of what is wrong?

Jul 23 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Jul 23, 6:05 pm, leo <leofante_XNOSPA...@gawab.comwrote:
Hi , i have a problem with session variables.
The web site was already working correctly using sessions, i've had to
add a page which use a new variable session but this new one doesn't
get registered, so when the page redirect to another one the new
variable is vanished.
Here a snippet of the code:

<?php

unset($_SESSION['tipopreventivo']);

//debug
$hand=fopen("public/test.txt","w");
fwrite($hand,print_r($_POST,true));
fclose($hand);
//

if(!empty($_POST))
{
$error=array();
session_register('typep');
$_SESSION['type']=($_POST['type']?$_POST['type']:'Default');

//debug
$hand=fopen("public/session.txt","w");
fwrite($hand,print_r($_SESSION,true));
fclose($hand);
//

if($_POST['info']==1)
{
$url="Location: prev.php";
header($url);
}

}

after submit text.txt e session.txt are right, in particular in
session.txt the variable $_SESSION['type'] is set ad has a value but
after the redirect to prev.php $_SESSION['type'] is not set
The sessions are working becouse other variables are passed correctly.
Any idea of what is wrong?
session_register is deprecated and you do not need it at all. However,
if you use it, you first define the variable $type = ..., then say
session_register( "type" ). You don't need it since in the line below
you use the right way of setting session variables ($_SESSION["type"]
= ...). Also, you seem to have misspelled the "type" in
session_register ("typep"). All of these are just comments. As for why
it doesn't work, you should check whether you called session_start in
the right place (before any output has been made) and whether you
called it at all in this new file. You are probably aware that you
need it? Also, if you actually require-d this code into another file,
you might have called session_write_close before you require-d it.
That might be a problem as well. I don't know how this http header
works, but maybe the client doesn't get the generated session id (if
it's a new session), so it might not be recognized on the next file
request. Maybe you rethink the redirection method to redirect it by
either javascript or meta tags? Or simply require-ing the file you
need to redirect the client to? Without any further knowledge of your
code, I can't really guess anything else...

Cheers,
Darko

Jul 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.