Skip to main content
Inspiring
June 23, 2009
Question

Testing for client screen resolution script issue.

  • June 23, 2009
  • 2 replies
  • 2451 views

Hello;

I am working on a small script that checks for the users resolution. I know the only way to do this is by a java script sending the info to coldfusion then adding it to a db. So I wrote this script and it grabs the users resolution, but once a size is put into the db, all the other users that hit the site, are using the same resolution. I find that hard to believe.

This is my code:

These tags are set in my application.cfc file in onsessionstart

<!--- This grabs the users resolution --->

<CFSET ScreenWidth = "<script>document.write(screen.width);</script>">
<CFSET ScreenHeight = "<script>document.write(screen.height);</script>">

<!--- then add it to the DB --->

<cfquery name="tracking" datasource="#APPLICATION.dataSource#" dbtype="ODBC">
INSERT INTO tracking ( REMOTE_ADDR, HTTP_USER_AGENT, SWidth, SHight, TRACK_DATE, PATH_INFO  )
VALUES(
  <cfqueryparam value="#Trim(CGI.REMOTE_ADDR)#" cfsqltype="CF_SQL_VARCHAR">,
  <cfif Len(Trim(HTTP_USER_AGENT)) GT 1>
  <cfqueryparam value="#Trim(CGI.HTTP_USER_AGENT)#" cfsqltype="CF_SQL_VARCHAR">,
  </cfif>
  <cfqueryparam value="#Trim(ScreenWidth)#" cfsqltype="CF_SQL_VARCHAR">,
  <cfqueryparam value="#Trim(ScreenHeight)#" cfsqltype="CF_SQL_VARCHAR">,
  <cfqueryparam value="#Now()#" cfsqltype="CF_SQL_TIMESTAMP">,
  <cfqueryparam value="#Trim(PATH_INFO)#" cfsqltype="CF_SQL_LONGVARCHAR">
)
</cfquery>

then to read it I use this:

<cfquery NAME="tracking" datasource="#APPLICATION.dataSource#">
SELECT ID, REMOTE_ADDR, HTTP_USER_AGENT, SWidth, SHight, TRACK_DATE, PATH_INFO
FROM tracking
ORDER BY ID
</cfquery>

<cfoutput query="tracking">

#SWidth# x #SHight#

</cfoutput>

That is it. Anyone have any idea what I did wrong on this? Even a better more simple way would be good.

thank you.

CFmonger

This topic has been closed for replies.

2 replies

Known Participant
June 23, 2009

Do you need the info in the database?

what happens if you change the display page to just:

<cfoutput>#ScreenWidth# x #ScreenHeight#</cfoutput>

If this doesn't work then you may need to adjust the scope - hopefully someone else can correct any mistakes I make here - try either:

<cfoutput>#application.ScreenWidth# x #application.ScreenHeight#</cfoutput>

or

change the app.cfm file :

<!--- This grabs the users resolution --->

<CFSET session.ScreenWidth = "<script>document.write(screen.width);</script>">
<CFSET session.ScreenHeight = "<script>document.write(screen.height);</script>">

and the display page:

<cfoutput>#session.ScreenWidth# x #session.ScreenHeight#</cfoutput>

CFmongerAuthor
Inspiring
June 23, 2009

Yes, I am making a small tracking system, and I need to collect info on screen resolution. I did try the session.screenwidth and so on thinking it would narrow down the results by the actual session that is started. No, that didn't work either.I really think it has to do with what is being collected by the cfset and getting inserted into the DB is where the problem is.

so far those didn't work, and some I tried.BUT I did find this.

In my DB the actual resolution is not being collected, but this is being added to my db = <script>document.write(screen.width);</script>

so the cfset is not collecting the proper info. Any ideas?

CFmonger

Known Participant
June 23, 2009

You need a WHERE clause in your tracking query otherwise you will always get the latest result

Michael

CFmongerAuthor
Inspiring
June 23, 2009

how would I write a where statement for this?

Known Participant
June 23, 2009

Use the other fields to identify which user this is:

<cfquery NAME="tracking" datasource="#APPLICATION.dataSource#">
SELECT ID, REMOTE_ADDR, HTTP_USER_AGENT, SWidth, SHight, TRACK_DATE, PATH_INFO
FROM tracking

WHERE REMOTE_ADDR = '#Trim(CGI.REMOTE_ADDR)#'

AND HTTP_USER_AGENT = '#Trim(CGI.HTTP_USER_AGENT)#'


ORDER BY ID
</cfquery>

Alternatively you may be able to make use of session variables or cookies