Hello KK Sir and All!

I am trying to create a log of login and logout or a user.

Login atleast works. but logout doesnot.
EDIT: Login works. but it creates an entry everytime the page refreshes or i visit another page. I want that:
1. Login is logged only when one user logs in.
2. Login is logged only when user logs out and then again logs in.
3. No log for login on refresh

How can I do that?

I have tried the following:

1. My Logout Link:
Code: Select all
<a class="log-out" href="<cms:logout_link redirect=k_site_link />"> Logout</a>


2. My AJAX Call:
Code: Select all
$('.log-out').click(function(event){
            event.preventDefault();
            $link = $(this);
            $.ajax({
               type: 'POST',
               url: "<cms:show k_site_link />superadmin/logout-ajax.php",
               data:  {
                  today_date = "<cms:date format='Y-m-d H:i' />",
                  user_id = "<cms:show k_user_id />",
                  action = "<cms:show 'Logout' />"
               },
               contentType: 'application/json',
               success: function(data) {
                  window.location.href = $link.attr('href');
               },
               error: function(data) {
                  alert("error");
               }
            });
         });


3. My logout-ajax.php
Code: Select all
<?php require_once( '../couch/cms.php' ); ?>
   <cms:template title="Logout AJAX" parent="_superadmin_" hidden="1" />
   
   <cms:set log_today_date = "<cms:get 'today_date' method='get' />" "global" />
   <cms:set log_user_id = "<cms:get 'user_id' method='get' />" "global" />
   <cms:set log_action = "<cms:get 'action' method='get' />" "global" />

   <cms:set my_template_name = 'history-lookup.php' />
    <cms:set my_page_title = "<cms:date format='Y-m-d' /> - <cms:show k_user_id /> - Logout" "global" />

    <cms:php>
        global $CTX, $FUNCS;
        $name = $FUNCS->get_clean_url( "<cms:show my_page_title />" );
        $CTX->set( 'my_page_name', $name );
    </cms:php>

    <cms:set my_page_id = '' 'global' />
    <cms:pages masterpage=my_template_name page_name=my_page_name limit='1' show_future_entries='1'>
        <cms:set my_page_id=k_page_id  'global' />
    </cms:pages>

   <cms:if my_page_id=''>
      <cms:db_persist
         _masterpage='history-lookup.php'
         _mode='create'
         _invalidate_cache='0'
         _auto_title='0'

         k_page_title = "<cms:date log_today_date format='Y-m-d' /> - <cms:show log_user_id /> - <cms:show log_action />"
         k_page_name = "<cms:show k_page_title />"

         today_date = "<cms:date log_today_date format='Y-m-d H:i' />"
         user_id = "<cms:show log_user_id />"
         action = "<cms:show log_action />"
      >
         
      </cms:db_persist>
   </cms:if>
<?php COUCH::invoke(); ?>


Thanks in advance!

Regards,
GenXCoders