Obtain the Client IP Address

Source: Internet
Author: User
Tags servervariables

Today's interview asked me how to obtain the Client IP address. I couldn't answer it for a while, sorry.

Get server computer name: page. server. manchinename

Get user information: page. User

Obtain the client computer name: page. Request. userhostname

Obtain the Client IP Address: page. Request. userhostaddress

Obtain the previous IP Address: Document. referrer

C # obtain the IP Address:Using system; <br/> using system. data; <br/> using system. configuration; <br/> using system. collections; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. web. UI. htmlcontrols; </P> <p> using system. collections. specialized; </P> <p> Public partial class test_getip: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> response. write (request. userhostaddress); <br/> response. write (request. userhostname); <br/> response. write (server. machinename); <br/> response. write (page. user. identity. name); <br/> // C # method for obtaining the previous IP address <br/> If (request. servervariables ["http_referer"]! = NULL) <br/> response. write (request. servervariables ["http_referer"]. tostring (); <br/> // csdn example <br/> int loop1, loop2; <br/> namevaluecollection Coll; <br/> coll = request. servervariables; <br/> string [] arr1 = Coll. allkeys; <br/> for (loop1 = 0; loop1 <arr1.length; loop1 ++) <br/>{< br/> response. write ("key:" + arr1 [loop1] + "<br>"); <br/> string [] arr2 = Coll. getvalues (arr1 [loop1]); <br/> for (loop2 = 0; loop2 <arr2.length; loop2 ++) <br/>{ <br/> response. write ("value" + loop2 + ":" + server. htmlencode (arr2 [loop2]) + "<br> "); <br/>}</P> <p >}< br/>JS get IP:<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <title> method for obtaining IP addresses using JavaScript </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> function f () {<br/> alert (1); <br/>}< br/> // delegate similar to C, use the function name as the parameter <br/> function F1 (m) {<br/> M (); <br/>}< br/> document. onclick = function () {<br/> F1 (f); <br/> // obtain the previous IP address <br/> alert (getlocalipaddress ()); <br/> alert (document. referrer ); <br/>}< br/> // choose security> ActiveX Control and plug-in from Internet Options> <br/> // no mark initialize ActiveX controls for security and run scripts. set to enable. <br/> // This method is not successful. <br/> function getlocalipaddress () {<br/> var OBJ = NULL; <br/> var rslt = ""; <br/> try <br/> {<br/> OBJ = new activexobject ("rcbdyctl. setting "); <br/> rslt = obj. getipaddress; <br/> OBJ = NULL; <br/>}< br/> catch (E) <br/>{< br/> // exception <br/>}</P> <p> return rslt; <br/>}< br/> </SCRIPT> <br/> </pead> <br/> <body> <br/> <a href = "getip. aspx "> getip </a> <br/> </body> <br/> </ptml>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.