On Jul 23, 6:05 pm, leo <leofante_XNOSP A...@gawab.comw rote:
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("pu blic/test.txt","w");
fwrite($hand,pr int_r($_POST,tr ue));
fclose($hand);
//
if(!empty($_POS T))
{
$error=array();
session_registe r('typep');
$_SESSION['type']=($_POST['type']?$_POST['type']:'Default');
//debug
$hand=fopen("pu blic/session.txt","w ");
fwrite($hand,pr int_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_registe r is deprecated and you do not need it at all. However,
if you use it, you first define the variable $type = ..., then say
session_registe r( "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_registe r ("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_c lose 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