I need to get the last non-empty value that $_GET['id_item']
had
session_start();
if(!isset($_SESSION['id_item']) || $_SESSION['id_item']==='' || ( isset($_GET['id_item']) && !$_GET['id_item'] === '' )){
$_SESSION['id_item'] = $_GET['id_item'];
}else{
/*no need to update*/
}
echo $_SESSION['id_item'] /* Allways in blank :S */
And var_dump($_GET)
outputs:
array(1) { ["id_item"]=> string(2) "50" }
Any idea why the $_SESSION
is not saved?
unless you expect a (valid) ID to be
0
you can reduce!isset($_SESSION['id_item']) || $_SESSION['id_item']===''
toempty($_SESSION['id_item'])
.!$_GET['id_item'] === ''
is always false, as this translates tofalse === ''
. You were probably looking for$_GET['id_item'] !== ''
. Again, if0
is not a valid value, you can go for!empty($_GET['id_item'])
here.That said, the whole
!isset($_SESSION['id_item']) || $_SESSION['id_item']===''
part of the condition doesn't make much sense. The second part "if _GET id_item present" is always necessary for the condition's body ($_SESSION['id_item'] = $_GET['id_item'];
) to work. So you can reduce your condition tofix this:
$_SESSION['id_item']==''
to$_SESSION['id_item']===''
or you can use:
Have you output anything before the
start_session
. If so it will be unable to send the cookie. This is probably why it is not working.