Yii2 get correct client IP










-2















I use this function for get client ip:



 $client = @$_SERVER['HTTP_CLIENT_IP'];


You can see it here.
http://city.ru.xsph.ru/web/



But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/










share|improve this question
























  • do mark the correct answer that worked for you

    – Muhammad Omer Aslam
    Jan 30 at 12:52















-2















I use this function for get client ip:



 $client = @$_SERVER['HTTP_CLIENT_IP'];


You can see it here.
http://city.ru.xsph.ru/web/



But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/










share|improve this question
























  • do mark the correct answer that worked for you

    – Muhammad Omer Aslam
    Jan 30 at 12:52













-2












-2








-2








I use this function for get client ip:



 $client = @$_SERVER['HTTP_CLIENT_IP'];


You can see it here.
http://city.ru.xsph.ru/web/



But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/










share|improve this question
















I use this function for get client ip:



 $client = @$_SERVER['HTTP_CLIENT_IP'];


You can see it here.
http://city.ru.xsph.ru/web/



But this address does not match the IP that use other hosts (the correct IP). For example:
https://hidemyna.me/en/ip/







yii2 ip-address






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:32









Muhammad Omer Aslam

13.5k72548




13.5k72548










asked Nov 15 '18 at 18:18









rand_triestrand_triest

659




659












  • do mark the correct answer that worked for you

    – Muhammad Omer Aslam
    Jan 30 at 12:52

















  • do mark the correct answer that worked for you

    – Muhammad Omer Aslam
    Jan 30 at 12:52
















do mark the correct answer that worked for you

– Muhammad Omer Aslam
Jan 30 at 12:52





do mark the correct answer that worked for you

– Muhammad Omer Aslam
Jan 30 at 12:52












3 Answers
3






active

oldest

votes


















2














You can use the Yii2 default method available in YI2



Yii::$app->getRequest()->getUserIP()


or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior within the model like



public function behaviors() 
return [
...
'ip' => [
'class' => IpBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
],
]
...
];



Or like this. where value can be a string or an anonymous function that will return a string.



public function behaviors() 
return [
...
'ip' => [
'class' => IpBehavior::className(),
'createdIpAttribute' => 'created_ip',
'updatedIpAttribute' => 'updated_ip',
'value' => '127.0.0.1',
]
...
];






share|improve this answer






























    0














    I think to get client IP address you have to use this code:



     Yii::$app->request->userIP





    share|improve this answer






























      0














      The prefered way of getting client ip is with Yii::$app->request-remoteIp

      This method uses either $_SERVER['REMOTE_ADDR'] or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.



      there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example






      share|improve this answer

























      • But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?

        – rand_triest
        Nov 15 '18 at 21:17










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325630%2fyii2-get-correct-client-ip%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can use the Yii2 default method available in YI2



      Yii::$app->getRequest()->getUserIP()


      or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior within the model like



      public function behaviors() 
      return [
      ...
      'ip' => [
      'class' => IpBehavior::className(),
      'attributes' => [
      ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
      ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
      ],
      ]
      ...
      ];



      Or like this. where value can be a string or an anonymous function that will return a string.



      public function behaviors() 
      return [
      ...
      'ip' => [
      'class' => IpBehavior::className(),
      'createdIpAttribute' => 'created_ip',
      'updatedIpAttribute' => 'updated_ip',
      'value' => '127.0.0.1',
      ]
      ...
      ];






      share|improve this answer



























        2














        You can use the Yii2 default method available in YI2



        Yii::$app->getRequest()->getUserIP()


        or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior within the model like



        public function behaviors() 
        return [
        ...
        'ip' => [
        'class' => IpBehavior::className(),
        'attributes' => [
        ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
        ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
        ],
        ]
        ...
        ];



        Or like this. where value can be a string or an anonymous function that will return a string.



        public function behaviors() 
        return [
        ...
        'ip' => [
        'class' => IpBehavior::className(),
        'createdIpAttribute' => 'created_ip',
        'updatedIpAttribute' => 'updated_ip',
        'value' => '127.0.0.1',
        ]
        ...
        ];






        share|improve this answer

























          2












          2








          2







          You can use the Yii2 default method available in YI2



          Yii::$app->getRequest()->getUserIP()


          or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior within the model like



          public function behaviors() 
          return [
          ...
          'ip' => [
          'class' => IpBehavior::className(),
          'attributes' => [
          ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
          ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
          ],
          ]
          ...
          ];



          Or like this. where value can be a string or an anonymous function that will return a string.



          public function behaviors() 
          return [
          ...
          'ip' => [
          'class' => IpBehavior::className(),
          'createdIpAttribute' => 'created_ip',
          'updatedIpAttribute' => 'updated_ip',
          'value' => '127.0.0.1',
          ]
          ...
          ];






          share|improve this answer













          You can use the Yii2 default method available in YI2



          Yii::$app->getRequest()->getUserIP()


          or if you are looking to save the current user ip into the database table i would recommend you to use the IPbehavior within the model like



          public function behaviors() 
          return [
          ...
          'ip' => [
          'class' => IpBehavior::className(),
          'attributes' => [
          ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'],
          ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip',
          ],
          ]
          ...
          ];



          Or like this. where value can be a string or an anonymous function that will return a string.



          public function behaviors() 
          return [
          ...
          'ip' => [
          'class' => IpBehavior::className(),
          'createdIpAttribute' => 'created_ip',
          'updatedIpAttribute' => 'updated_ip',
          'value' => '127.0.0.1',
          ]
          ...
          ];







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 19:54









          Muhammad Omer AslamMuhammad Omer Aslam

          13.5k72548




          13.5k72548























              0














              I think to get client IP address you have to use this code:



               Yii::$app->request->userIP





              share|improve this answer



























                0














                I think to get client IP address you have to use this code:



                 Yii::$app->request->userIP





                share|improve this answer

























                  0












                  0








                  0







                  I think to get client IP address you have to use this code:



                   Yii::$app->request->userIP





                  share|improve this answer













                  I think to get client IP address you have to use this code:



                   Yii::$app->request->userIP






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 19:49









                  MehranMehran

                  1831412




                  1831412





















                      0














                      The prefered way of getting client ip is with Yii::$app->request-remoteIp

                      This method uses either $_SERVER['REMOTE_ADDR'] or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.



                      there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example






                      share|improve this answer

























                      • But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?

                        – rand_triest
                        Nov 15 '18 at 21:17















                      0














                      The prefered way of getting client ip is with Yii::$app->request-remoteIp

                      This method uses either $_SERVER['REMOTE_ADDR'] or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.



                      there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example






                      share|improve this answer

























                      • But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?

                        – rand_triest
                        Nov 15 '18 at 21:17













                      0












                      0








                      0







                      The prefered way of getting client ip is with Yii::$app->request-remoteIp

                      This method uses either $_SERVER['REMOTE_ADDR'] or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.



                      there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example






                      share|improve this answer















                      The prefered way of getting client ip is with Yii::$app->request-remoteIp

                      This method uses either $_SERVER['REMOTE_ADDR'] or ip headers (like X-Forwarded-For) that can be added by load balencers or proxy services.



                      there are however still situations where your public ip (https://hidemyna.me/en/ip/) and the ip that your appication is showing (http://city.ru.xsph.ru/web/) will be different - if you're on the same network as your app for example







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 16 '18 at 0:21

























                      answered Nov 15 '18 at 19:56









                      csminbcsminb

                      1,88211318




                      1,88211318












                      • But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?

                        – rand_triest
                        Nov 15 '18 at 21:17

















                      • But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?

                        – rand_triest
                        Nov 15 '18 at 21:17
















                      But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?

                      – rand_triest
                      Nov 15 '18 at 21:17





                      But this IP not correct ($_SERVER['REMOTE_ADDR']). How to get the same ip that show online resources?

                      – rand_triest
                      Nov 15 '18 at 21:17

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325630%2fyii2-get-correct-client-ip%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto