SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validate localhost: make sure localhost only

    Hello,

    In a form, I would like to make sure that only a "localhost" url is allowed. So what should I look for? http://127.0.1.1 and http://localhost? Is that all? I've seen numbers after "localhost" sometimes ("localhost:****) so I'm not so sure if I have to check for those or not.


  2. #2
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    127 addresses may end higher than 1, i.e., 127.0.0.14, any address 127.0.0.0/8 is a localhost IP. And IPv6 has something like ::1 for localhost.

    http://en.wikipedia.org/wiki/Localhost
    Doug G
    =====
    "If you ain't the lead dog, the view is always the same - Anon

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a regexp to validate localhost? Should I simply allow everything that doesn't end with a ".soemthing" as it seems that you can map any name to your local machine?

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Can I ask....what the purpose of this is?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    352
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yeah i don't see the point in this.. but

    you should mess with the $_SERVER enviroment


    http://www.php.net/manual/en/reserve...les.server.php
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    @logic_earth and @Shaydez,

    I test for localhost and define a LOCALHOST constant that is used to test environments.

    A single file can then be safely used to upload all files and prevent that "Oh sh!t" moment

    PHP Code:

      defined
    ('LOCALHOST'
       ? 
    NULL 
       
    define('LOCALHOST'in_array($_SERVER['SERVER_NAME'], array('localhost''127.0.0.1') ));
      ..
      ..
      
    $advert '_google_250x250'
      echo 
    "<div class='" .$advert ."'>"
        if( 
    LOCALHOST )  { include PATH_ADVERT .$advert .'.js';} else {echo $advert; }
      echo 
    '</div>';
      ..
      .. 
    Last edited by John_Betong; Apr 24, 2012 at 19:25. Reason: formatting and spelling: not my fortay

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    So you leave all that localhost stuff in the production code? My solution is better...I run the code as it will run on the production server, under a proper domain, and server configuration (not in a sub-directory).
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by logic_earth View Post
    So you leave all that localhost stuff in the production code? My solution is better...I run the code as it will run on the production server, under a proper domain, and server configuration (not in a sub-directory).
    I am always keen to learn new techniques and would be grateful if you could explain your system.

    I test for LOCALHOST and use it for MySql connect strings, passwords, do not display Google Ads locally, etc

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    With Apache I use VirtualHost to create multiple distinct web sites. IIS is the same but uses different configuration methodologies. Combined with using "hosts" to create domain names. (An actual DNS server can also be used.)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by logic_earth View Post
    With Apache I use VirtualHost to create multiple distinct web sites. IIS is the same but uses different configuration methodologies. Combined with using "hosts" to create domain names. (An actual DNS server can also be used.)
    I also use Apache VirtualHosts and a single common constants.php file that is used to set the specific environment strings.

    It appears that your system is similar to mine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •