Results 1 to 4 of 4

Thread: Geting client ip address

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    42

    Geting client ip address

    i have managed to run the asp file in the local network however it add only my ip each time the users run the file

    how can i change the code so that i get the user's ip address not mine

    i guess it has something to do with the host.

    Code:
    <%@ Page Language="VB"%>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>
    
    <script language="VB" runat="server">
    	' Declared outside the subs so both can access it.
    	Dim objConnection As OleDbConnection
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
        Dim value As String = h.AddressList.GetValue(0).ToString()
        Dim ipfull As String = h.AddressList.GetValue(0).ToString
        Dim ipsplit() As String = ipfull.Split(".".ToCharArray())
        'IP.Text = h.AddressList.GetValue(0).ToString()
        Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
            ' Set up our connection.
            objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("ip.mdb") & ";")
    
            ' Every call to the page adds a new record and
            ' shows the data in the table.
            AddNewRecord()
            'ShowDataGrid()
        End Sub
    
    	Sub AddNewRecord()
    		Dim objCommand As OleDbCommand
    		Dim strSQLQuery As String
    
    		' Our insert command
            strSQLQuery = "INSERT INTO ip " _
       & "(pcname, username, mip) " _
       & "VALUES (@TextValue, @TextValue2, @TextValue3)"
    
    		' Create new command object passing it our SQL insert
    		' and telling it which connection to use.
    		objCommand = New OleDbCommand(strSQLQuery, objConnection)
    		
    		' Add parameters that our SQL command takes:
    		objCommand.Parameters.Add(New OleDbParameter("@TextValue", OleDbType.VarChar, 255))
            objCommand.Parameters.Add(New OleDbParameter("@TextValue2", OleDbType.VarChar, 255))
            objCommand.Parameters.Add(New OleDbParameter("@TextValue3", OleDbType.VarChar, 255))
    
    
            objCommand.Parameters("@TextValue").Value = System.Environment.MachineName
            objCommand.Parameters("@TextValue2").Value = System.Environment.UserName
            objCommand.Parameters("@TextValue3").Value = ipsplit(3)
    
    		' Open the connection, execute the command, and close the connection.
            Try
                objConnection.Open()
                objCommand.ExecuteNonQuery()
                objConnection.Close()
                status.Text = "data added"
            Catch ex As Exception
                status.Text = "you can add data only once"
            End Try
          
    	End Sub
    
    	
    
    </script>
    
    <html>
    <body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Status"></asp:Label>
        <asp:TextBox ID="status" runat="server" Width="352px"></asp:TextBox><!-- Plain old DataGrid... format it as you like. -->
    </form> 
    </body>
    </html>

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Geting client ip address

    Use Request.ServerVariables("REMOTE_ADDR"), you're using classes that work with the address of the web server.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    42

    Re: Geting client ip address

    how can i make it to get their own ips? n where to place the code

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Geting client ip address

    You'd replace it on this line

    Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)

    And I don't understand your first question, because this does get their remote address.

Posting Permissions

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



Click Here to Expand Forum to Full Width