Imagine a simple html page with 3 iframes pointing to the same url:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<iframe src="http://www.mydom.com/mypage.aspx"></iframe>
<iframe src="http://www.mydom.com/mypage.aspx"></iframe>
<iframe src="http://www.mydom.com/mypage.aspx"></iframe>
</body>
</html>
My goal is to track unique visitors in mypage.aspx code behind. Sounds simple but the following:
if (Request.Cookies["myc"] == null)
{
// New visitor!
Response.Cookies["myc"].Value = myval;
Response.Cookies["myc"].Expires = DateTime.Now.AddYears(10);
}
else
{
// Returning visitor
}
has a problem. Visiting the html page with the 3 iframes I get three simultaneous hits to mypage.aspx and Request.Cookies[“myc”] is null all three times while I should understand that it is the same user (1st hit: new visitor, 2nd and 3rd hits: returning visitor for a total of one visitor/user). Any ideas how to fix this?
All you have to do is to put the same code in a [webMethod] instead of in PageLoad then call the [WebMethod] from client via js (XMLHttpRequest), you will receive the three calls in sequence. No more probs due to simultaneous hits.