简体   繁体   中英

Opencart - check if user is logged in and redirect to accounts page

My Opencart store is in a subdirectory '/login'. In the root folder I have an index.php file that displays a Login user and password interface. The site works by not allowing access until a user is logged in. Once logged in, it gets redirected to the subdirectory store.

All works fine, but when the user is finished with the site, closes the browser, and decides to come back to the .com store, they are greeted with the login interface again.

The problem lies here. If they didn't log out last time, they will be stuck at the log in screen, but because they already logged in earlier, it won't redirect if they reenter their credentials. It just errors out and fails to redirect.

So my question is: How do you check if the user is already logged in, and redirect to the site? I tried islogged() but it is undefined since all of the core files are in the subdirectory along with the rest of the store. I tried to use the php include and include the customer.php file that defines "islogged()" but that seems to not work.

Here is the code inside my index.php file.

<script type="text/javascript"><!--
    $('#button-cart').on('click', function() {
        $.ajax({
            url: 'login/index.php?route=account/loginajax',
            type: 'post',
            data: $('input[type=\'text\'], input[type=\'password\']'),
            dataType: 'json',
            beforeSend: function() {
                $('#button-cart').button('loading');
            },
            complete: function() {
                $('#button-cart').button('reset');
            },
            success: function(json) {
                $('.alert, .text-danger').remove();
                $('.form-group').removeClass('has-error');
                if (json['error']) {
                    $('.error').after('<div class="alert alert-danger has-error">' + json['error'] + '</div>');
                }
                if (json['success']) {
                    $('.error').after('<div class="alert alert-success">' + json['success'] + '</div>');
                    window.location = "http://www.mbdtesting.com/login/index.php?     route=account/account";
                }
            }
        });
    });
</script>

You can check is session for customer is set or not. In your index file put this :-

if (isset($session->data['customer_id'])) {
   /*continue to your site*/
  /**Also verify customer is valid or not then continue*/
} else {
   /*go to login page*/
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM