Obtener IP del visitante de forma eficiente

En muchas ocasiones necesitamos obtener la IP del visitante para habilitarle según que acciones o simplemente para reconocerlo sin necesidad de que se tenga que registrar y logear para interactuar con la Web.

La siguiente función obtiene la IP del usuario de forma eficiente:

<?php
function getIP() {
   if (isset($_SERVER)) {
      if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
         return $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
         return $_SERVER['REMOTE_ADDR'];
      }
   } else {
      if (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDER_FOR'])) {
         return $GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDED_FOR'];
      } else {
         return $GLOBALS['HTTP_SERVER_VARS']['REMOTE_ADDR'];
      }
   }
}
?>

Ver ejemplo en funcionamiento

Autor
Escrito por Jose Aguilar - Director ejecutivo y tecnológico en JA Modules. Experto programador PrestaShop y Experto programador WordPress.
Te ha servido? Valora esta entrada!
(1 voto, promedio: 5 de 5)
Comparte en las redes sociales
¿Buscas trabajo de programador?

13 respuestas a “Obtener IP del visitante de forma eficiente”

  1. Grudes dice:

    Y para aplicar este codigo a una tienda prestashop?

    Es decir para que registre la ip de quien se registra en la tienda igual que el modulo :http://www.presto-changeo.com/es/prestashop-free-modules/44-order-ip-log.html , hace con los pedidos

  2. URL dice:

    … [Trackback]…

    […] Informations on that Topic: jose-aguilar.com/blog/obtener-ip-del-visitante/ […]…

  3. refluks dice:

    I am very happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that’s at the other blogs. Appreciate your sharing this best doc.

  4. Tincho dice:

    Que tal Jose, excelente Blog. Te comento que este codigo en particular no es muy eficiente, ya que te muestra la IP privada. Por tanto, podes tener 100 personas al mismo tiempo en tu website con la misma IP y ninguna forma de diferenciarla.
    Un buen punto seria tomar la IP privada y tambien la IP Publica (de esa forma hasta podrias diferencias a 2 personas dentro de una misma LAN que salen por la misma IP).
    No es muy dificil obtener la IP Publica, asi que seria una buena opcion.

  5. Richie dice:

    I have not checked in here for a while because I thought it was getting boring, but the last several posts are great quality so I guess I’ll add you back to my daily bloglist. You deserve it friend 🙂

  6. site dice:

    Very interesting details you have mentioned, thanks for putting up.

  7. chaussure de foot dice:

    I truly enjoy studying on this internet site , it has got superb blog posts.

  8. chaussure de foot dice:

    Wonderful web site. Plenty of useful information here. I’m sending it to some buddies ans additionally sharing in delicious. And certainly, thank you for your sweat!

  9. Samual Ke dice:

    I would like to thnkx for the efforts you’ve put in writing this blog. I am hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing skills has encouraged me to get my own web site now. Really the blogging is spreading its wings rapidly. Your write up is a great example of it.

  10. Erik Minerd dice:

    I gotta bookmark this web site it seems very beneficial handy

  11. Torrenty za darmo dice:

    Hello! I realize this is kind of off-topic but I had to ask. Does managing a well-established website like yours require a massive amount work? I’m completely new to running a blog but I do write in my journal everyday. I’d like to start a blog so I will be able to share my own experience and views online. Please let me know if you have any kind of recommendations or tips for new aspiring bloggers. Appreciate it!

  12. piala eropa dice:

    I feel that is among the so much significant info for me. And i am happy studying your article. However want to commentary on some basic things, The web site style is perfect, the articles is in reality great :D. Excellent activity, cheers.

  13. Jordan Xia dice:

    You got a very superb website, Gladiolus I found it through yahoo.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.

Ver más sobre
ip