Author Topic: Hide cart if not logged in  (Read 348 times)

0 Members and 1 Guest are viewing this topic.

Offline djlfreakTopic starter

  • Irregular
  • Posts: 27
    • View Profile
Hide cart if not logged in
« on: May 26, 2010, 01:40:25 AM »
I want to hide the shopping cart if the user is not logged in? How do I change this?

<div class="col2"><!-- Column 2 start -->

  <
div id="leftnav"
 
<?
php
require_once 'include/leftNav.php';
?>
</div>

  <div id="minicart"> 
<?php require_once 'include/miniCart.php'?>

</div>
</div>
</div>


I tried this but it didn't work.
<?php
if (isset($_SESSION['name'])) {
    echo 
'<div id="minicart">';
} else {
    echo 
' ';
}
?>

<?php
if (isset($_SESSION['name'])) {
    require_once 
'include/miniCart.php'?>
} else {
    require_once ' ';
}
?>

</div>
</div>



Offline jd307

  • Enthusiast
  • Posts: 83
    • View Profile
Re: Hide cart if not logged in
« Reply #1 on: June 09, 2010, 09:22:59 PM »
To use sessions, you need to use session_start(); before using $_SESSION.


...
session_start();
if(isset(
$_SESSION['name']))
{
     echo 
"<div id='minicart'>";
     require_once(
'include/miniCart.php');
     echo 
"</div>";
}
...


If isset($_SESSION['name'])) returns as false and you don't want anything else to happen, you don't need to set an ELSE within your IF statement.

Let me know how you get on.