3 Replies Latest reply on Feb 16, 2012 5:50 AM by William_1

    How to display a DIV or not, based on whether a logging session cookie exists or not?

    William_1 Level 1

      Hi everyone

       

      I have created a logging system for a simple CMS using Dreamweaver server behaviours.

       

      My problem :

      When logged in, I would like to show on my pages a DIV saying something like "You are logged in". If not logged in, I would like a different DIV with this text saying "Please log in"

       

      I have simplified the problem as much as I could. Basically, I would like to show something or to hide it and show another thing based on whether the logging session exists or not. Would someone have a simple answer to this problem?

       

      Much obliged, W

        • 1. Re: How to display a DIV or not, based on whether a logging session cookie exists or not?
          HauteJordo Level 1

          try stackoverflow or tigzag.

           

          <?php

          if(isset($_COOKIE['lastVisit']))

                    $visit = $_COOKIE['lastVisit'];

          else

                    echo "You've got some stale cookies!";

           

           

          echo "Your last visit was - ". $visit;

          ?>

          1 person found this helpful
          • 2. Re: How to display a DIV or not, based on whether a logging session cookie exists or not?
            William_1 Level 1

            Thanks HJ

            Your answer helped me find more info about cookies and session which is what I actually needed.

             

            So I can show a particular item based on whether an admin user is logged in, I proceed as follow.

             

            First I check whethet the session exists. If he has just logged in using Dreamweaver logging system, the session will exist. So put the text above the <head> and preferably at the very top of the page.

            <?php

             

            if (!isset($_SESSION)) {

              session_start();

            }

            ?>

             

            Then in the content portion of the page where I want to display, say a DIV with some content for the administrator I place the following :

             

            <?php

            $accessLevels = array('a','b');

            $validLevel = $_SESSION['MM_UserGroup'];

            if(array_search($validLevel, $accessLevels)>-1){

            ?>

            <div id="amendPage" style="margin-left:0;margin-top:0;height:20px;"><a href="/admin/changePage.php">Changez le contenu de cette page</a></div>

            <?php

            }

            ?>

             

            I am checking here whether the user is logged in based on his or her access level which I have previously set in the database. 'a' being an access level and 'b' an other one which might enable more things. So I can play with this array to show something to someone based on the access level.

             

            If I were to remove 'b' from the array, the div would only be visible when a user with an 'a' access level is logged in and vice versa.

             

            Not sure whether this is a good option, but it works for me


            • 3. Re: How to display a DIV or not, based on whether a logging session cookie exists or not?
              William_1 Level 1

              Would someone know how to create an else statement with the following script related to this thread :

               

              <?php

              $accessLevels = array('a','b');

              $validLevel = $_SESSION['MM_UserGroup'];

              if(array_search($validLevel, $accessLevels)>-1){

              ?>

              <div id="amendPage" style="margin-left:0;margin-top:0;height:20px;"><a href="/admin/changePage.php">Changez le contenu de cette page</a></div>

              <?php

              }

              ?>

               

              I would like to show the DIV #amendPage  when the session exists, and a different DIV if the condition is false. ie if a user hasn't logged in and the session does not exist

               

              Thanks