locked
Get client IP address from web application RRS feed

  • Question

  • Hi Friends

    I have a web application hosted in a server and that connects to the SQL Server running on same machine.

    Can I have client IP address (IP address of the computer from which the application is browsed) from SQL Server? The host_name() function does't work for me as application and SQL server are on the same machine.

    Regards

    Ashru

     


    MCTS, http://asharafaliptb.wordpress.com
    Tuesday, July 20, 2010 5:08 AM

Answers

  • Hi Ashru,

    I do not know how this is possible. Unless you have something like Java Script on your web page accessing SQL server directly, which is a dangerous practice, SQL server is usually accessed by code run on the server so SQL server has no idea which client is running the web app unless the code sends it the information.


    hz
    • Proposed as answer by Naomi N Wednesday, July 21, 2010 3:45 AM
    • Marked as answer by KJian_ Monday, July 26, 2010 9:59 AM
    Tuesday, July 20, 2010 10:58 AM

All replies

  • Hello,

    Try this code, to get the IP address of the SQL Server...

    DECLARE @ipLine VARCHAR(200)
    DECLARE @pos INT
    DECLARE @ip VARCHAR(40)

    SET @ip = NULL
    CREATE TABLE #temp (ipLine VARCHAR(200))
    INSERT #temp EXEC master..xp_cmdshell 'ipconfig'
    SELECT @ipLine = ipLine
    FROM #temp
    WHERE UPPER(ipLine) like '%IP ADDRESS%'
    IF (ISNULL (@ipLine,'***') != '***')
    BEGIN 
     SET @pos = CHARINDEX (':',@ipLine,1);
     SET @ip = RTRIM(LTRIM(SUBSTRING(@ipLine ,  @pos + 1 , LEN(@ipLine) - @pos)))
    END 

    DROP TABLE #temp

    SELECT @ip

    Hope its helpful....


    Pavan Kokkula Infosys Technologies Limited.
    Tuesday, July 20, 2010 5:20 AM
  • you should get this information from your Web Application and not SQL Server. SQL Server only have the IP Addr of the Web Application and not the Client.

    KH Tan
    • Proposed as answer by Naomi N Wednesday, July 21, 2010 3:44 AM
    Tuesday, July 20, 2010 5:43 AM
  • Hi Pavan

    The code does not work at my end :-(

    Regards

    Ashru


    MCTS, http://asharafaliptb.wordpress.com
    Tuesday, July 20, 2010 10:14 AM
  • Hi Atif-ullah

    Thanks you for the reply

    What I actually want is get ip form SQL Server

     

    Regards

    Ashru


    MCTS, http://asharafaliptb.wordpress.com
    Tuesday, July 20, 2010 10:16 AM
  • You want to get Client IP from SQL Server?

    Tuesday, July 20, 2010 10:48 AM
  • Hi Ashru,

    I do not know how this is possible. Unless you have something like Java Script on your web page accessing SQL server directly, which is a dangerous practice, SQL server is usually accessed by code run on the server so SQL server has no idea which client is running the web app unless the code sends it the information.


    hz
    • Proposed as answer by Naomi N Wednesday, July 21, 2010 3:45 AM
    • Marked as answer by KJian_ Monday, July 26, 2010 9:59 AM
    Tuesday, July 20, 2010 10:58 AM