How do I disable a session variable?

I have the following code, but it doesn’t work, I don’t know what error I’m going to make that is why this code doesn’t work:

Here is the code:

<?php session_start();
if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo']) && !empty($_GET['indexNo'])) 
{
   $indx = $_GET['indexNo'];
   foreach($_SESSION['itemsOrder'] as $key => $val)
{
 echo "$key => $val <br> " ;    
  if($indx == $val)
  {
    unset($_SESSION['itemsOrder'][$val]);

  }
  else
  {

    echo "indexNo was not unset <br>";
  }
 }
}
else 
{
    echo "indexNo not received!";
}
?>

      

+3


source to share


3 answers


Should $key

n't be $val

. Try with

if($indx == $key)
{
    unset($_SESSION['itemsOrder'][$key]);

}

      



Do not need to use isset

both empty

together.

if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo']))

      

+2


source


You need to override the value of the session array than use its key instead of the value. replace:

unset($_SESSION['itemsOrder'][$val]); 

      



from:

unset($_SESSION['itemsOrder'][$key]);

      

+2


source


**Try This Code**
<?php session_start();
if (isset($_GET['indexNo']) && is_numeric($_GET['indexNo'])) 
{
$indx = $_GET['indexNo'];
foreach($_SESSION['itemsOrder'] as $key => $val)
{
echo "$key => $val <br> " ; 
if($indx == $val)
{
    unset($_SESSION['itemsOrder'][$key]);

}
else
{

    echo "indexNo was not unset <br>";
}
}
}
else 
{
    echo "indexNo not received!";
}

      

0


source







All Articles