Reputation: 1
So... here's the thing... I'm connected through sky, which have dynamic IPs and my IP changes once in a while. I'm running a small website from home, the website has, among other things, three video streams from my ip cameras. Now, I don't want to update my code every time the ip changes, so there is this bit at the top of my file:
Response.Buffer = True
Dim objXMLHTTP, xml
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", "http://myip.dnsomatic.com/", False
xml.Send
externalIP = xml.responseText
Set xml = Nothing
My ip changed a while ago, but above code was returning my old IP! When I visited http://myip.dnsomatic.com/, it shown the correct, new IP. The situation persisted as long as I didn't restart my machine. I would like to know why the code behaves like that? Why it doesn't return correct IP right away?
There is one more thing I do not understand... My website has a comments feature, where users can leave a comment. My addComment.asp file checks if the session expired first:
if Session("userID") = "" or Session("username") = "" then
Response.Write("<SCRIPT LANGUAGE=JavaScript>")
Response.Write("alert('Your session has expired...');")
Response.Write("window.location.href='index.html';")
Response.Write("</SCRIPT>")
end if
If the session hasn't expired, the comment is added to the database and the page is updated, but if the session expired, it doesn't go back to index.html, instead, the INSERT INTO statement, which I use to insert the comment into corresponding table, throws an error! That statement is way below above code, theoretically it should never be executed. Any ideas?
I should also add that if the above code is modified following way:
if Session("userID") = "" or Session("username") = "" then
Response.Redirect("index.html")
end if
everything works fine in both cases (session expired and not expired), but I would like a message telling the user about the expiration to pop up. Here's the INSERT INTO statement, which I don't think contains any errors:
conn.Open connStr
commText = "INSERT INTO Messages (ID,
userID,
messageDate,
messageText)
VALUES (" & (recordsQty + 1) & ",
" & currentUserID & ",
#" & Now & "#,
'" & commentText & "')"
conn.Open connStr
conn.Execute commText
conn.Close()
Upvotes: 0
Views: 58
Reputation: 11
This is just a theory, but I'm guessing that you're getting stale results because it's being cached somewhere. Untested code, but I believe this will solve your problem if it's caching:
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", "http://myip.dnsomatic.com/", False
xml.setRequestHeader "If-None-Match", "some-random-string"
xml.setRequestHeader "Cache-Control", "no-cache,max-age=0"
xml.setRequestHeader "Pragma", "no-cache"
xml.Send
As for continuing to get an error after redirecting, the mistake you're making is not separating server-side from client-side execution of code. Unless aborted, the server-side code will continue to execute to the end. Response.Redirect(String) aborts the execution, although not in a particularly clean way: http://msdn.microsoft.com/en-us/library/t9dwyts4%28v=vs.110%29.aspx
It's much better to modify your code so that it can continue all the way to the end. For example, this may not be pretty depending on how much code you have, but putting the non-redirect path inside of an else block of the if Session("userID") ... block would work.
Edit: I don't have the rep to post comments, but I don't believe this is a DNS caching issue at all, as the myip.dnsomatic.com is simply returning the IP of the client it's serving, which you're then, I assume, directly connecting to. If that's the case, DNS (other than resolving the myip.dnsomatic.com name) isn't involved at all.
Upvotes: 1