androidnoobdev androidnoobdev - 3 months ago 25
PHP Question

isset function for li tag

My coding is like

<?php
if (!isset($_SESSION["usr"]))
{
?>
<ul id="top_nav">
<li ><a href="login.php">Login</a></li>
<li><a href="#">About us</a></li>
</ul>
<?php
}
else
{
echo '<ul id="top_nav">';
echo '<li id="logout"><a href="#">Logout</a></li>';
echo '<li><a href="#">' . $_SESSION["usr"] . '</a></li> </ul>';
}
?>


In php I want to destroy session when user click on logout(which I have used li).
so I want to check using
isset
function.
or any other method then let me know.

Answer

to unset session var use unset($_SESSION['usr'],$usr);. Both vars are needed to workaround with register_global issue. Your code have to be kind of this

<?php 
if ($_GET['logout']=='1') unset($_SESSION['usr'],$usr);
if(!isset($_SESSION["usr"])){?>
  <ul id="top_nav">
      <li><a href="login.php">Login</a></li>
      <li><a href="#">About us</a></li>
  </ul>
<?php }else{ ?>
  <ul id="top_nav">
      <li id="logout"><a href="?logout=1">Logout</a></li>
      <li><a href="#"><?=$_SESSION["usr"];?></a></li>
  </ul>
<?php } ?>    
Comments