I wrote a code to work as a web counter. It generates javascript output in OutPutStream so I could use the script in another website to give it a free website counter. The problem is that when I want to get the referrer, it returns the page where the script is placed not the real referrer. here is my code :
html code in body :
<script type="text/javascript" language="javascript" src="counter.aspx?siteid=2"></script>
and the counter.aspx.cs :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class counter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/javascript";
UICulture = "en-US";
Culture = "en-US";
int siteid = int.Parse(Request.QueryString["siteid"].ToString());
string pubIp = new System.Net.WebClient().DownloadString("https://api.ipify.org");
var ipResponse = GetCountryByIP(pubIp);
HttpBrowserCapabilities browse = Request.Browser;
string platform = browse.Platform;
string browsername = browse.Browser;
Counter cnt = new Counter();
var counter = cnt.GetCounter(siteid, DateTime.Now, pubIp, Request.UrlReferrer.ToString(), platform, browsername, "", ipResponse.Country);
string text = "document.write('<div id=\"ShortCounter\" style=\" margin: 0px auto;width: 100px;min-height: 100px;font-family: Tahoma;font-size: x-small;background-color:" + counter.BackColor + ";color:" + counter.color + ";border:" + counter.BorderSize + "px " + counter.BorderStyle + " " + counter.BorderColor + ";\">" +
"<div style=\"padding: 8px;\">" +
"You Are Visitor Number:<br />" +
counter.CounterNumber.ToString("N0") +
"<br />" +
"Today" +
"<br />" +
counter.Today.ToString("N0") +
"<br />" +
"This Week" +
"<br />" +
counter.ThisWeek.ToString("N0") +
"<br />" +
"This Month" +
"<br />" +
counter.ThisMounth.ToString("N0") +
"<br />" +
"</div>" +
"<div style=\"width: 100%; background-color: darkred; text-align: center; padding-top: 2px; padding-bottom: 2px;\">" +
Request.UrlReferrer.ToString() +
"</div>');";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
Response.OutputStream.Write(bytes, 0, bytes.Length);
}
the problem is: Lets assume that I am in xyz.com (other site) and then I click on a link and go to zzz.com/1.html (my site). the zzz.com/1.html page contains the script I mentioned earlier I want to know the zzz.com/1.html referrer which is xyz.com but my c# page shows me zzz.com as the referrer which makes sense. How could I get zzz.com/1.html referrer ?! I hope explanation have been enough.
Thanks
Try to pass referer in query string as below
<script language="JavaScript" type="text/javascript">
var referer = document.referrer;
$.getScript("counter.aspx?siteid=2&referedby="+referer);
</script>
Now in the code use this query string
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class counter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/javascript";
UICulture = "en-US";
Culture = "en-US";
int siteid = int.Parse(Request.QueryString["siteid"].ToString());
string referedby = Request.QueryString["referedby"];
string pubIp = new System.Net.WebClient().DownloadString("https://api.ipify.org");
var ipResponse = GetCountryByIP(pubIp);
HttpBrowserCapabilities browse = Request.Browser;
string platform = browse.Platform;
string browsername = browse.Browser;
Counter cnt = new Counter();
var counter = cnt.GetCounter(siteid, DateTime.Now, pubIp, referedby, platform, browsername, "", ipResponse.Country);
string text = "document.write('<div id=\"ShortCounter\" style=\" margin: 0px auto;width: 100px;min-height: 100px;font-family: Tahoma;font-size: x-small;background-color:" + counter.BackColor + ";color:" + counter.color + ";border:" + counter.BorderSize + "px " + counter.BorderStyle + " " + counter.BorderColor + ";\">" +
"<div style=\"padding: 8px;\">" +
"You Are Visitor Number:<br />" +
counter.CounterNumber.ToString("N0") +
"<br />" +
"Today" +
"<br />" +
counter.Today.ToString("N0") +
"<br />" +
"This Week" +
"<br />" +
counter.ThisWeek.ToString("N0") +
"<br />" +
"This Month" +
"<br />" +
counter.ThisMounth.ToString("N0") +
"<br />" +
"</div>" +
"<div style=\"width: 100%; background-color: darkred; text-align: center; padding-top: 2px; padding-bottom: 2px;\">" +
Request.UrlReferrer.ToString() +
"</div>');";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
Response.OutputStream.Write(bytes, 0, bytes.Length);
}