Forum Discussion

NehaRana's avatar
12 years ago

IP adress

Hello everyone!!

 

I am new to Lithium.

 

I want to put some conditions in javascript on the basis of IP Adress.I have to put that condition into my custom compnent ,so that i can use jquery in it.Please suggest me HOW can i do that.

 

I got ${http.request.remoteAddr}  is used to get the IP adress in Lithim.But I am new to Lithum i don't how it can help me tracking an IP and assigning this to a java script variable??

 

Please help me

 

 

Thanks,

Neha

  • Hi Neha and welcome to Lithosphere,

     

    Usually it's preferrable to handle IP information in Freemarker code. Can you share some more details on what you want to achieve based on the IP address?

     

    If you really really need it in JavaScript it should be possible to have your custom component populate a JavaScript variable with the freemarker one like this:

    <script type="text/javascript">
       var RemoteIP = ${http.request.remoteAddr};
    </script>
    

    ...or if you need this info on every page instead of creating a custom component you can add this code to your Lithium skin's header wrapper.

  • Hi Neha and welcome to Lithosphere,

     

    Usually it's preferrable to handle IP information in Freemarker code. Can you share some more details on what you want to achieve based on the IP address?

     

    If you really really need it in JavaScript it should be possible to have your custom component populate a JavaScript variable with the freemarker one like this:

    <script type="text/javascript">
       var RemoteIP = ${http.request.remoteAddr};
    </script>
    

    ...or if you need this info on every page instead of creating a custom component you can add this code to your Lithium skin's header wrapper.

    • NehaRana's avatar
      NehaRana
      Adept
      Actually i want to hide register snippet if the user comes from some particular IP Address so want to add some link to Login on the basis of IP .I was thinking that i would add jqyery code on the basis of IP check condition.If you can suggest a better way.It would be really appreciable..

      Thank You
      • Claudius's avatar
        Claudius
        Boss

        Basically if you just need to hide the UI element you just need to identify the CSS class or ID and then add the following code to your custom component:

        <#if ${http.request.remoteAddr} == "127.0.0.1">
        <style>
            a.lia-component-users-action-login {display : none !important;}
        </style>
        </#if>

         Alternatively using jQuery:

         

        <#if ${http.request.remoteAddr} == "127.0.0.1">
        <@liaAddScript>
        ;(function($) {
        $(document).ready(function() {
            $('.lia-component-users-action-login').hide();
         });
        })(LITHIUM.jQuery);
        </@liaAddScript>
        </#if>

         Whatever seems easier to you ;)

  • Just to back up what Claudius has already said, you need to bear in mind that any JavaScript variables can be easily changed by the user within their browser. Therefore it's important that you don't rely on the IP address being correct for anything important (e.g. authentication, security etc.).

  • Are you just trying to hide the link, or do you need to prevent those users from logging in?

     

    Technically, it may be possible to hide all of the registration links (but not prevent registration altogether) using jQuery. However, there are so many different places in the community that link to the registration page that I suspect it would be difficult to hide them all.

     

    Lithium may have an option to disable registration for users with a certain IP address, which may be a more sensible option.