Bigcommerce - How to redirect users to login page

2019-08-09 15:42发布

问题:

How do I have customers see the login page instead of the homepage if they are not logged in? I tried using the following code but it redirects to the login page even if the customer is logged in.

<script language="javascript" type="text/javascript">//<![CDATA[
if (window.location.href.toString() == "http://www.mywebsite.com/")
{
    window.location.assign("http://www.mywebsite.com/login.php");
}
//]]></script>

回答1:

You can detect whether or not the user is logged in by looking at the welcome message that comes from the %%GLOBAL_LoginOrLogoutText%% code.

So if that welcome text is not displayed redirect them to the login.php page.

Also note that you need a caveat that if they are already at that page you don't need to redirect them again (and again and again).



回答2:

I built this the other day and I was able to check if a customer was logged in from any page in the store.

    //this will allow you to mention the cookie by index
    function getCookie(name) {
      var value = "; " + document.cookie;
      var parts = value.split("; " + name + "=");
      if (parts.length == 2) return parts.pop().split(";").shift();
    }
    //set variable that will check if login email exists
    var loggedIn = getCookie('LoginEmail');

    //logic that will output different content based on the loggedIn Status
    if(typeof loggedIn === 'undefined'){
        console.log("They are not logged in!");
        var notLoggedIn = '<ul><li><a href="/membership/">Become a Member</a></li></ul><p>Already a member? <a href="/login.php">Sign In >></a></p>';
        $(notLoggedIn).appendTo(".LandingInnerContent");
    }
    else{
        console.log("They are logged in! ");
        var isLoggedIn = '<ul><li><a href="/membership/">Shop Now</a></li></ul><p>Access <a href="/account.php">Account Page >></a></p>';
        $(isLoggedIn).appendTo(".LandingInnerContent");
    }

link to repo

This will allow you to check it based on the cookie and you can display different content or redirect to a different page.



回答3:

Unfortunately, the '%%GLOBAL_LoginOrLogoutText%%' variable doesn't usually work, so I've come up with a JavaScript solution.

First, I check the value of %%GLOBAL_CurrentCustomerFirstName%%, then fill the login or register, or logout 'li' accordingly using jQuery.

<li class="login-register">
     <script type="text/javascript">
           var GlobalFname="%%GLOBAL_CurrentCustomerFirstName%%";

           if (GlobalFname !="" && GlobalFname!="Guest"){
               $('li.login-register').html('<a href="%%GLOBAL_ShopPath%%/login.php?action=logout">LOG OUT</a>');
           } else {
               $('li.login-register').html('<a href="%%GLOBAL_ShopPath%%/login.php">LOGIN</a> OR <a href="%%GLOBAL_ShopPath%%/login.php?action=create_account">REGISTER</a>');

          };
     </script>
</li>


回答4:

You could simply look for the presence of the "Sign out" text, like this. This code would go on the "Page Content" of "Web Page Details" for any web page in Storefront - Web Pages. You must use the HTML button on the editor and paste it as raw HTML.

<script type="text/javascript">
$(document).ready(function(){

if( $(".header").text().indexOf('Sign out') >= 0){
  //alert("Customer is logged in");
  $("#div_logged_in").show();
  $("#div_not_logged_in").hide();
} else {
  //alert("Customer is NOT logged in");
  $("#div_logged_in").hide();
  $("#div_not_logged_in").show();
}

});
</script>

<div id="div_logged_in">
    <p>The customer IS logged in.</p>
</div>
<div id="div_not_logged_in">
    <p>The customer IS NOT logged in</p>
</div>


回答5:

if you're looking to do this in php, and you're using sessions to store the login information, you could do something simple like this at the top of the homepage:

<?php
session_start();
//check if the user is already logged in.
if (!isset($_SESSION['user'])) {
    header('Location: login.php');
}

Hope that helps.