View Full Version : Inserting server variables into db
Mhtml
09-21-2002, 11:54 AM
aaaggghhh!!! I have spent 3 hours trying to figure out how to insert 3 server variables into my database but it just won't work...
It seems that I've learnt nothing from my previous posts because each time I come to something very much the dame I try to do what my last reply told me to do but it just seems to always be different...
anyways here is my script
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
dim IP, Browser, Referer
IP = Request.ServerVariables("REMOTE_ADDR")
Browser = Request.ServerVariables("HTTP_USER_AGENT")
Referer = Request.ServerVariables("HTTP_REFERER")
'This is the code used to make a connection to our database
Set ConnSQL = Server.CreateObject("ADODB.Connection")
path = Server.MapPath("databases/odysseystats.mdb")
ConnSQL.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & path
sqlInsert = "INSERT INTO UniqueCounter (IP,Browser,Referer) VALUES (" & IP & "','" & Browser & "','" & Referer & "')"
ConnSql.Execute(sqlInsert)
%>
<%
ConnSQL.Close
Set ConnSQL = nothing
Set RS = nothing
%>
:(
whammy
09-21-2002, 04:42 PM
What are the datatypes for those variables in your database?
Mhtml
09-22-2002, 01:55 AM
number for IP and Text for Browser and Referer.
edit:
Ahh..... I see now Browser contains numbers as well as text, so that is why it gets stuck there... and referer has the potential to have numbers...
So how do I get around this?
whammy
09-22-2002, 02:13 AM
Ip also has NN.NN.NNN.NNN so really it's not a number. I would make them all text fields... then they would all be like:
'" & var & "'
when you insert the values in the database.
Text fields can accept numbers just fine. But integer fields won't allow alpha characters.
FYI when I use Access at work to ease in the transformation of files (importing/exporting data), I always import the data from a text file into Access as text - otherwise Access will delete leading zeroes (and you can't have that happening with account numbers, at least not in our scenario!).
Mhtml
09-22-2002, 02:22 AM
okey dokes, I'll do it now..:)
Mhtml
09-22-2002, 03:31 AM
Also, how can I just get the browser type and version out of the returned http_user_agent string?
I saw in someone elses coding that they used CASE how does this work?
A link or something would be good..
Mhtml
09-22-2002, 03:51 AM
Okay, I've made SELECT CASE work but te way I have it working would require me to know the specs of each browser, and I'd have to repeat steps of code several time with only a slight change, is there a way to do it easier then the following of which I have demonstrated how I have to code to recognise the differences in operation systems for IE6.0
<% dim strthis
strthis = Request.ServerVariables("http_user_agent")
Select CASE strthis
CASE "NS 3.0" ReturnedCase = "NS 3.0"
CASE "NS 4.0" ReturnedCase = "NS 4.0"
CASE "NS 5.0" ReturnedCase = "NS 5.0"
CASE "NS 6.0" ReturnedCase = "NS 6.0"
CASE "MSIE 3.0;" ReturnedCase = "IE 3.0"
CASE "MSIE 4.0;" ReturnedCase = "IE 4.0"
CASE "MSIE 5.0;" ReturnedCase = "IE 5.0"
CASE "MSIE 5.5;" ReturnedCase = "IE 5.5"
'If IE 6.0
CASE "Mozilla/4.0 (compatible; MSIE 6.0; Windows 3.1)" ReturnedCase = "IE 6.0"
CASE "Mozilla/4.0 (compatible; MSIE 6.0; Windows 95)" ReturnedCase = "IE 6.0"
CASE "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)" ReturnedCase = "IE 6.0"
CASE "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)" ReturnedCase = "IE 6.0"
CASE "Mozilla/4.0 (compatible; MSIE 6.0; Windows ME)" ReturnedCase = "IE 6.0"
CASE "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)" ReturnedCase = "IE 6.0"
CASE "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP Professional)" ReturnedCase = "IE 6.0"
end select
Response.Write(ReturnedCase) %>
whammy
09-22-2002, 04:40 AM
Hmm... getting the Browser type might be a bit tricky - here's what the three browsers I have installed return:
IE 5.5:
USER_AGENT: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)
Opera 6.05:
USER_AGENT: Mozilla/4.0 (compatible; MSIE 5.0; Windows ME) Opera 6.05 [en]
Netscape 7.0:
USER_AGENT: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0
So unless you just want to store the whole HTTP_USER_AGENT string, this might take some time to work out how to parse the different versions (unless someone here has already done the work for us?!?).
If you know of any scripts that parse out the browser/version reliably in javascript, you could do something like this:
<%
ID = "31"
%>
<html>
<head>
<title>Get Browser Info</title>
</head>
<body>
Image is here: --><img src="blank.gif" width="10" height="10"/><--
USER_AGENT: <% = Request.ServerVariables("HTTP_USER_AGENT") %>
<script language="JavaScript" type="text/javascript">
<!--
document.images[0].src="getbrowserinfo.asp?ID=<% = ID %>&B=" + escape(navigator.appName) + "&V=" + escape(navigator.appVersion);
//-->
</script>
</body>
</html>
Although the navigator.appName and navigator.appVersion info that I just recorded like that is nothing like the actual browsers I'm using :(
If that is a feasible solution, I just used a separate page called "getbrowserinfo.asp" to write the querystring stuff into a .txt file...
Mhtml
09-22-2002, 04:58 AM
hmm... what about this?
<%
Set MyBrow=Server.CreateObject("MSWC.BrowserType")
%>
<%BrowserValueForDb = MyBrow.Browser & " " & MyBrow.Version%>
whammy
09-22-2002, 05:03 AM
Argh. That is the worst I've tried so far. For IE 5.5 it returns IE 5.0 and for Netscape 7 and Opera it returns:
Default 0.0
?!?
From everything I've been reading so far, it doesn't look like there's really any reliable way of getting the browser info... :(
But I'll keep looking for awhile since I'm kinda working on the same deal.
Mhtml
09-22-2002, 05:04 AM
I found this which does what I want but it is way to complex for me to understand as I'm to new to this stuff, but you may be able to figure it out.
http://www.2enetworx.com/dev/projects/statcountex.asp
Mhtml
09-22-2002, 05:11 AM
I was going to say that it seemed to be unreliable but I wasn't sure if it was just me, lol... For me I used ns 3.0 and it was ok but when I used IE 6.0 it said I was running ns 4.0:rolleyes:
whammy
09-22-2002, 05:11 AM
:(
I looked at the code, and all they are using to get the browser version is javascript...:
if (navigator.appName=='Netscape'){b='NS';}
if (navigator.appName=='Microsoft Internet Explorer'){b='MSIE';}
if (navigator.appVersion.indexOf('MSIE 3')>0) {b='MSIE';}
:(
Mhtml
09-22-2002, 05:26 AM
hmm...
maybe that i the only semi-reliable way to do it, well... I'm trying to design for all browsers anyway so I guess if I have to I'll use JS..but for the moment I'm just going to stick to the IP and the Referer stats..
Mhtml
09-22-2002, 05:32 AM
Also I have another question, does session only work while the person is browsing the site? If they leave and come back is the session still active?
whammy
09-22-2002, 05:32 AM
Hmm... hold your horses... I do believe I'm getting somewhere. Of course, I don't have a BUNCH of browsers to test it on, but for the three I have installed, it's returning the correct values:
UserAgent = CStr(Request.ServerVariables("HTTP_USER_AGENT"))
If InStr(UserAgent,"MSIE") Then
If InStr(UserAgent,"Opera") Then
UserAgent = Mid(UserAgent,InStr(UserAgent,"Opera"),10)
Else
UserAgent = Mid(UserAgent,InStr(UserAgent,"MSIE"),8)
End If
ElseIf InStr(UserAgent,"Netscape") Then
UserAgent = Replace(Mid(UserAgent,InStr(UserAgent,"Netscape"),12),"/"," ")
End If
Can you go here:
http://www.solidscripts.com/getbrowserinfo/getbrowser.asp
and tell me what you get for both lines? :)
If you have others you could help me test/modify this script and we could have a kickbutt browser detection script. :)
whammy
09-22-2002, 05:33 AM
Yes, sessions only work while someone is browsing the site.
That's what cookies are for... all you have to do to make a persistent cookie in ASP is add an expiration date, i.e.:
Response.Cookies("cookiename").Expires = Date() + 30
Mhtml
09-22-2002, 05:45 AM
for ie 6.0 I got:
Browser: MSIE 6.0
User Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)
and for ns 3.1 i got:
Browser: Mozilla/3.01 (Win95; I)
User Agent: Mozilla/3.01 (Win95; I)
whammy
09-22-2002, 05:48 AM
Well, I highly doubt that 3.x or earlier browsers are going to give you any reliable information, anyway - and they have to be a minute percentage...
Glad that IE 6 worked! I'm going to test NS 4.78 right now...
Mhtml
09-22-2002, 05:49 AM
what are the numbers at the end for?
whammy
09-22-2002, 05:50 AM
Huh? Oh, do you mean +30 ?!? That's adding a month to the date, so the cookie would expire in a month...
Mhtml
09-22-2002, 05:55 AM
oops, I meant in the browser script... after IE , netscrap and opera remarks...
the start position..i think.
whammy
09-22-2002, 06:00 AM
That's just basic string manipulation - check out a tutorial on InStr() and other string manipulation functions.
Basically I told it to get everything in the string starting where "MSIE" starts, and grabbing the next 8 characters, for example.
P.S. I think I'm onto something - that javascript idea might not be bad after all - using the script above (I'm still modifying it), you can see if you come up with any results. And if not, then I write the hidden image to the page to send the javascript browser information to the server.
Can you access that link again with NS 3.0 ? I want to see what results I get.
http://www.solidscripts.com/getbrowserinfo/getbrowser.asp
It works for NS 4.78, at any rate!!!
Mhtml
09-22-2002, 06:01 AM
opera 6.01 returns the correct value.
whammy
09-22-2002, 06:02 AM
Yeah but can you try it in NS 3.something or Mozilla or something off the wall? :)
You won't see the results... but I will when I open the text file I created for testing this idea...
Mhtml
09-22-2002, 06:10 AM
just tried ns3.1 again and it came up with
Browser: Unknown
User Agent: Mozilla/3.01 (Win95; I) --> <--
whammy
09-22-2002, 06:13 AM
Yeah... but in my textfile it says:
9/22/2002 1:19:57 AM|Netscape|4.78
9/22/2002 1:19:54 AM|Netscape|4.78
9/22/2002 1:18:33 AM|Netscape|3.01
9/22/2002 1:18:27 AM|Netscape|3.01
9/22/2002 1:18:22 AM|Netscape|3.01
9/22/2002 1:18:19 AM|Netscape|3.01
9/22/2002 1:18:10 AM|Netscape|3.01
9/22/2002 1:07:44 AM|Netscape|3.01
9/22/2002 1:07:30 AM|Netscape|3.01
9/22/2002 1:03:30 AM|Netscape|4.78
9/22/2002 12:59:07 AM|Netscape|4.78
YAY!!! (I'm quite sure you're the 3.01 guy)
I'm posting in the general web design forum to see if people will help with this project.
If I can account for all the weird browsers people use in this forum, I can account for probably 99.9% of the people on the net!
And once we know this works, it will be easy to come up with a script that will update the "Unknown" browsers in your database with the information sent to the page by the hidden image using javascript... assuming everything keeps going well.
Hey, if not, we've already come a long way in getting more reliable browser versions. :)
Mhtml
09-22-2002, 06:27 AM
You've done well, I tried something similar last night using, Ltrim Rtrim but couldn't get it to work accurately.
Mhtml
09-22-2002, 06:30 AM
oh yes, I tested IE 5.0 possibly before you attahced the textfile... it worked.
whammy
09-22-2002, 06:32 AM
Cool... yeah, any that show something besides "Unknown" won't write to the text file...
If the browser is "Unknown", I write the fake image to the page that captures the javascript information in the other asp page that writes it to a text file.
Mhtml
09-22-2002, 06:40 AM
ahh...I though you may have been using JS when I saw the image with ns3.01
Mhtml
09-22-2002, 06:41 AM
So once you find a browser that doesn't work what do you do? How do you implement new code?
whammy
09-22-2002, 06:45 AM
It's already implemented... I just want to make sure that it works with other funky browsers first.
Actually if you look at the source code of the page in Netscape 3.01 you'll see what I'm doing!
Then we'll go over the results... so far, so good though - for all the browsers that we've tried that didn't show the right version in javascript, I was able to capture them with HTTP_USER_AGENT, and vice versa.
Keep your fingers crossed! And if you have any friends that use any other browsers, send them the link, and have them post here:
http://www.codingforums.com/showthread.php?s=&threadid=6586
:)
P.S. Here's the trick (not including a couple tweaks I made to the ASP code since - which I will share once I am convinced it's working ok):
<% If UserAgent = "Unknown" Then %>
<script language="JavaScript" type="text/javascript">
<!--
document.write('<img src="getbrowserinfo.asp?ID=<% = ID %>&B=' + escape(navigator.appName) + '&V=' + escape(navigator.appVersion) + '" width="10" height="10"/>');
//-->
</script>
<% End If %>
P.P.S. This thread is getting so huge i'm going to have to edit it down... if you have MSN or Yahoo or AIM, gimme your screen name or whatever so we can continue this discussion there!
Mhtml
09-22-2002, 06:57 AM
msn = ### deleted by whammy ###
P.S. ok, you're added to my list. :)
lol,
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.