Jump to content

Hide cart if not logged in


djlfreak

Recommended Posts

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/202922-hide-cart-if-not-logged-in/
Share on other sites

  • 2 weeks later...

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.