|
| |
| Sharonrao123 | Asp.Net User |
| Creating cookie c# | 3/7/2007 1:12:48 AM |
0/0 | |
|
Hello All,
On Default page of my website i have created a cookie to create a visitorID for users who are not logged in (please see code below). Now my problem is, In my website i have homepage, productspage, informationpage. When the default page is loaded for the first time i print the cookie value which is fine, if i click on homepage again the cookie value is changed which i dont want to happen. Can you guys please help me here how to keep the cookie value same even if i click homepage again. Cheers, Shilpa.
Default.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <table width="580" border="0" align="left" cellpadding="0" cellspacing="0"> <tr> <td colspan="2"> <asp:Label ID="lblvisitorID" runat="server" Text="Label"></asp:Label></td> </tr> </table> </asp:Content>
Default.aspx.cs
public partial class _Default : System.Web.UI.Page { _data a = new _data(); protected void Page_Load(object sender, EventArgs e) { string uniqueID = System.DateTime.Now.Ticks.ToString(); string strVisitorID = Session.SessionID.Substring(0, 8) + uniqueID;
// Create cookie object HttpCookie cookieCreate = new HttpCookie("HttpBrowserCookieCheck"); // Set the cookies value cookieCreate.Value = strVisitorID.ToString(); //Set the cookie to expire in 20 minute DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(30, 0, 20, 0); cookieCreate.Expires = dtNow + tsMinute; // Add the cookie Response.Cookies.Add(cookieCreate);
// Create cookie object HttpCookie cookieCheck = null; // Request cookie from client's browser cookieCheck = Request.Cookies["HttpBrowserCookieCheck"]; if (cookieCheck == null) { Session.Add("VisitorID", strVisitorID); string VisitorID = Session["VisitorID"].ToString(); lblvisitorID.Text = VisitorID.ToString(); } else { //strUserType = "Visitor"; lblvisitorID.Text = Response.Cookies["HttpBrowserCookieCheck"].Value.ToString(); } } public bool HttpBrowserCookieCheck(HttpRequest Request, HttpResponse Response) { try { // Create cookie object HttpCookie cookieCheck = null;
// Request cookie from client's browser cookieCheck = Request.Cookies["HttpBrowserCookieCheck"]; // Check whether the cookie exists on client's browser if (cookieCheck == null) // Client browser does not support cookies !! noooo !! { return false; } else // Client browser supports cookies { // Remove test cookie
// Response.Cookies.Remove("HttpBrowserCookieCheck"); return true; } } catch { return false; } finally { } } } |
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/7/2007 4:33:53 AM |
0/0 | |
|
Is the default.aspx is what you are calling your homepage?
If this is so then each time you load the page you create an unique id (string uniqueID = System.DateTime.Now.Ticks.ToString()), now since there will be always a time difference between two different page loads you will always have a new uniqueID and hence a new cookie value. That part of your logic needs changing.
AD |
| etws14 | Asp.Net User |
| Re: Creating cookie c# | 3/7/2007 4:35:26 AM |
0/0 | |
|
try this
this is in vb form
Response.Cookies("HttpBrowserCookieCheck")("data") = strVisitorID.ToString Response.Cookies("HttpBrowserCookieCheck").Expires = DateTime.MaxValue
i am not sure c# whether correct or not but you can try it
Response.Cookies["HttpBrowserCookieCheck"]["data"] = strVisitorID.ToString; Response.Cookies["HttpBrowserCookieCheck"].Expires = DateTime.MaxValue; |
| Sharonrao123 | Asp.Net User |
| Re: Creating cookie c# | 3/7/2007 7:12:37 AM |
0/0 | |
|
hello,
Sorry i did not understand what i need to do, can you elobrate please
Cheers, Shilpa. |
| etws14 | Asp.Net User |
| Re: Creating cookie c# | 3/7/2007 2:05:11 PM |
0/0 | |
|
etws14:
try this
this is in vb form
Response.Cookies("HttpBrowserCookieCheck")("data") = strVisitorID.ToString Response.Cookies("HttpBrowserCookieCheck").Expires = DateTime.MaxValue
i am not sure c# whether correct or not but you can try it
Response.Cookies["HttpBrowserCookieCheck"]["data"] = strVisitorID.ToString; Response.Cookies["HttpBrowserCookieCheck"].Expires = DateTime.MaxValue;
i mean you use the method directly assign the value and expires date that i given to you is more better than the one you use
// Create cookie object HttpCookie cookieCreate = new HttpCookie("HttpBrowserCookieCheck"); // Set the cookies value cookieCreate.Value = strVisitorID.ToString(); //Set the cookie to expire in 20 minute DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(30, 0, 20, 0); cookieCreate.Expires = dtNow + tsMinute; // Add the cookie Response.Cookies.Add(cookieCreate);
change to
Response.Cookies["HttpBrowserCookieCheck"]["data"] = strVisitorID.ToString; Response.Cookies["HttpBrowserCookieCheck"].Expires = DateTime.MaxValue;
I have do a lot of testing to the method you use it is not work very well, sometimes it's work sometime not work
the method I given to you i have tested already it is 100% works. |
| Sharonrao123 | Asp.Net User |
| Re: Creating cookie c# | 3/7/2007 11:41:42 PM |
0/0 | |
|
Hello,
I am new to c# so please bear with me, I have done as suggested by you. But the value of visitor id is changing when i go back to defaultpage (homepage)
551sctn5633089470832658976 - VisitorID value when Defaultpage first loaded 551sctn5633089471011414696 - VisitorID value when i click on homepage.
My question is how to retain this First visitorID value(551sctn5633089470832658976) and use it till the user leaves my website. Please help.
Cheers, Shilpa. |
| etws14 | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 1:49:49 AM |
0/0 | |
|
ok before you create a cookies to visitor first you do a check whether the cookies is exist or not if the cookies is exist then you don't need to create cookies to the visitor already.
algorithm
if ((request["cookiesName"]["datafield"]==null) || (request["cookiesName"].expires<=now)) { response["cookiesName"]["datafield"]=newid; response["cookiesName"].expires = date; }else{ DO NOTHING }
try this algorithm |
| Sharonrao123 | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 3:03:59 AM |
0/0 | |
|
Many thanks for your reply, i have done this and still i get different values, I dont understand do you have any better idea. I just need one unique visitorid for each visitor so that i can link with shopping cart.
kzyjh3ed633089592145077811 dcw0td45633089592297426436
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string uniqueID = System.DateTime.Now.Ticks.ToString();
string strVisitorID = Session.SessionID.Substring(0, 8) + uniqueID;
if ((Request.Cookies["HttpBrowserCookieCheck"]["data"] == null) || (Request.Cookies["HttpBrowserCookieCheck"].Expires <= DateTime.Now))
{
Response.Cookies[ "HttpBrowserCookieCheck"]["data"] = strVisitorID.ToString();
Response.Cookies[ "HttpBrowserCookieCheck"].Expires = DateTime.MaxValue;
}
// Create cookie object
HttpCookie cookieCheck = null;
// Request cookie from client's browser
cookieCheck = Request.Cookies[ "HttpBrowserCookieCheck"];
if (cookieCheck == null) //Create SessionID if Cookie is not present
{
Session.Add( "VisitorID", strVisitorID);
string VisitorID = Session["VisitorID"].ToString();
lblvisitorID.Text = VisitorID.ToString();
}
else
{
//Return Cookie value
lblvisitorID.Text = Response.Cookies[ "HttpBrowserCookieCheck"].Value.ToString();
}
}
} |
| etws14 | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 3:23:46 AM |
0/0 | |
|
ok i show you what i have done for my client website Private Function getIDFromCookies() As String
If IsNothing(Request.Cookies("cart")) Then
Response.Cookies("cart")("data") = System.Guid.NewGuid().ToString()
Response.Cookies("cart").Expires = DateTime.MaxValue
End If
Return Request.Cookies("cart")("data")
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
getIDFromCookies()
End If
End Sub
I just this few line codes only i have done all what you want to do. may be you can try it this code.
for what i am doing is i give the visitor one unique id and this unique id i have store into cookies, when ever i need the id i just call the function again |
| etws14 | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 3:45:26 AM |
0/0 | |
|
ok i exlaining the code that you given to me. I will put comment at there. it is because i have found the reason why you always get new id.
just read through all the comment i put within your code then you will know why
Sharonrao123:
Many thanks for your reply, i have done this and still i get different values, I dont understand do you have any better idea. I just need one unique visitorid for each visitor so that i can link with shopping cart.
kzyjh3ed633089592145077811 dcw0td45633089592297426436
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//when pageload you have assign to variable it is work..
string uniqueID = System.DateTime.Now.Ticks.ToString();
string strVisitorID = Session.SessionID.Substring(0, 8) + uniqueID;
//then you check the cookies whether it is exist or not. //if the cookies is null or expires
if ((Request.Cookies["HttpBrowserCookieCheck"]["data"] == null) || (Request.Cookies["HttpBrowserCookieCheck"].Expires <= DateTime.Now))
{ // you create new cookies the the cookies is not exist //the data of the cookies is the combination of first 8 values of the session id follow by the current datetime
Response.Cookies["HttpBrowserCookieCheck"]["data"] = strVisitorID.ToString();
// the expires datetime is Max Date value the server espect to give Response.Cookies["HttpBrowserCookieCheck"].Expires = DateTime.MaxValue;
}
//Here you create another cookies HttpCookie cookieCheck = null;
// Request cookie from client's browser
// you have assign the cookies with the existing cookies cookieCheck = Request.Cookies[ "HttpBrowserCookieCheck"];
//check again if the cookie is null if (cookieCheck == null) //Create SessionID if Cookie is not present
{
//if cookies is not exist then you create session with visitorID Session.Add( "VisitorID", strVisitorID);
//you assign the session id to the visitorID string VisitorID = Session["VisitorID"].ToString();
lblvisitorID.Text = VisitorID.ToString();
}
else
{
//Return Cookie value
// else you get the existing cookies and assign the value lblvisitorID.Text = Response.Cookies[ "HttpBrowserCookieCheck"].Value.ToString();
}
}
}
solution is you have do twice with the same job try to eliminate the redudent job, may be you can use the code that i provide to you and do some changes. i am not recommand to use HttpCookie to handle the cookies. directly request the cookies. I have done a lot of test and project i found that the HttpCookie something didn't get the value although the cookies is found and has been assign by you. therefore the code i given to you is not using httpcookie to communicate with the cookies. i just use direct method to get the value from cookies by using request method |
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 3:59:36 AM |
0/0 | |
|
Whatever way you use for generating the cookie value, if that is (the logic for generating an unique id for the cookie) is in the page_load event, which by the way is supposed to fire every time you visit the page, you are going to get a new value for the cookie.
|
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 3:59:57 AM |
0/0 | |
|
Whatever way you use for generating the cookie value, if that is (the logic for generating an unique id for the cookie) is in the page_load event, which by the way is supposed to fire every time you visit the page, you are going to get a new value for the cookie.
so |
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 4:00:04 AM |
0/0 | |
|
Whatever way you use for generating the cookie value, if that is (the logic for generating an unique id for the cookie) is in the page_load event, which by the way is supposed to fire every time you visit the page, you are going to get a new value for the cookie.
so |
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 4:00:30 AM |
0/0 | |
|
Whatever way you use for generating the cookie value, if that is (the logic for generating an unique id for the cookie) is in the page_load event, which by the way is supposed to fire every time you visit the page, you are going to get a new value for the cookie.
or |
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 4:00:33 AM |
0/0 | |
|
Whatever way you use for generating the cookie value, if that is (the logic for generating an unique id for the cookie) is in the page_load event, which by the way is supposed to fire every time you visit the page, you are going to get a new value for the cookie.
or maybe i |
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 4:00:33 AM |
0/0 | |
|
Whatever way you use for generating the cookie value, if that is (the logic for generating an unique id for the cookie) is in the page_load event, which by the way is supposed to fire every time you visit the page, you are going to get a new value for the cookie.
or maybe |
| addie | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 4:00:35 AM |
0/0 | |
|
Whatever way you use for generating the cookie value, if that is (the logic for generating an unique id for the cookie) is in the page_load event, which by the way is supposed to fire every time you visit the page, you are going to get a new value for the cookie.
or maybe i dont |
| Sharonrao123 | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 4:14:34 AM |
0/0 | |
| |
| Sharonrao123 | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 4:27:57 AM |
0/0 | |
|
Hello etws (sorry dont know your name),
This is what i have done as you suggested. But still does not solve my problem. ahhhhh why is this so hard.
protected void Page_Load(object sender, EventArgs e)
{
if (!(IsPostBack))
{
String cookiechk = getIDFromCookies();
if (cookiechk == null) //Create SessionID if Cookie is not present
{
string uniqueID = System.DateTime.Now.Ticks.ToString();
string strVisitorID = Session.SessionID.Substring(0, 8) + uniqueID;
Session.Add( "VisitorID", strVisitorID);
string VisitorID = Session["VisitorID"].ToString();
lblvisitorID.Text = VisitorID.ToString();
}
else
{
//Return Cookie value
lblvisitorID.Text = Response.Cookies[ "HttpBrowserCookieCheck"]["data"].ToString();
}
}
}
private String getIDFromCookies()
{
if ((Request.Cookies["HttpBrowserCookieCheck"]["data"] == null) || (Request.Cookies["HttpBrowserCookieCheck"].Expires <= DateTime.Now))
{
string uniqueID = System.DateTime.Now.Ticks.ToString();
string strVisitorID = Session.SessionID.Substring(0, 8) + uniqueID;
Response.Cookies[ "HttpBrowserCookieCheck"]["data"] = strVisitorID.ToString();
Response.Cookies[ "HttpBrowserCookieCheck"].Expires = DateTime.MaxValue;
}
return Response.Cookies["HttpBrowserCookieCheck"]["data"].ToString();
} |
| etws14 | Asp.Net User |
| Re: Creating cookie c# | 3/8/2007 5:39:34 AM |
0/0 | |
|
Sharonrao123:
Hello etws (sorry dont know your name),
This is what i have done as you suggested. But still does not solve my problem. ahhhhh why is this so hard.
protected void Page_Load(object sender, EventArgs e)
{
if (!(IsPostBack))
{
String cookiechk = getIDFromCookies();
if (cookiechk == null) //Create SessionID if Cookie is not present
{
string uniqueID = System.DateTime.Now.Ticks.ToString();
string strVisitorID = Session.SessionID.Substring(0, 8) + uniqueID;
// this part don't need to check the cookies again. it is because the getIDFromCookies function already check the id already
Session.Add("VisitorID", strVisitorID);
string VisitorID = Session["VisitorID"].ToString();
lblvisitorID.Text = VisitorID.ToString();
}
else
{
//Return Cookie value
lblvisitorID.Text = Response.Cookies[ "HttpBrowserCookieCheck"]["data"].ToString();
}
}
}
private String getIDFromCookies()
{
if ((Request.Cookies["HttpBrowserCookieCheck"]["data"] == null) || (Request.Cookies["HttpBrowserCookieCheck"].Expires <= DateTime.Now))
{
string uniqueID = System.DateTime.Now.Ticks.ToString();
string strVisitorID = Session.SessionID.Substring(0, 8) + uniqueID;
Response.Cookies[ "HttpBrowserCookieCheck"]["data"] = strVisitorID.ToString();
Response.Cookies[ "HttpBrowserCookieCheck"].Expires = DateTime.MaxValue;
}
return Response.Cookies["HttpBrowserCookieCheck"]["data"].ToString();
}
|
|
| |
Free Download:
Books: Java Enterprise in a Nutshell: [a practical guide] Authors: Jim Farley, William Crawford, Prakash Malani, Safari Tech Books Online, Pages: 892, Published: 2005 Java Enterprise in a Nutshell: A Desktop Quick Reference Authors: Jim Farley, William Crawford, David Flanagan, Pages: 971, Published: 2002 Active Server Pages 3.0 by Example Authors: Jeff Spotts, Bob Reselman, Pages: 592, Published: 2000 Pro VB 2005 and the .Net 2.0 Platform Authors: Andrew Troelsen, Pages: 1034, Published: 2006 JavaScript For Dummies Authors: Emily A. Vander Veer, Pages: 384, Published: 2004 Core Servlets and JavaServer Pages: Core Technologies Authors: Marty Hall, Larry Brown, Pages: 736, Published: 2003 C# 3.0 in a Nutshell: A Desktop Quick Reference Authors: Joseph Albahari, Peter Drayton, Pages: 838, Published: 2007 Python Essential Reference: Essential Reference Authors: David M. Beazley, Pages: 625, Published: 2006 Writing Apache Modules with Perl and C Authors: Lincoln D. Stein, Stein, Doug MacEachern, MacEachern, Safari Tech Books Online, Pages: 724, Published: 1999 Web:Cookie set, delete, get value and create : Cookie « Development ... Cookie set, delete, get value and create : Cookie « Development « JavaScript DHTML. ... C# / C Sharp · C# / CSharp Tutorial ... CGI::Cookie - Interface to Netscape Cookies - search.cpan.org $c->bake($r);. If you want to set the cookie yourself, Within a CGI script you can send a cookie to the browser by creating one or more Set-Cookie: fields ... Cookies in ASP.NET Creating cookies with ASP.NET is simple and straight forward. The System.Web namespace offers a class ... C:\Documents and Settings\Administrator\Cookies. ... C# And Cookies 1: <%@ language="C#" %> 2:
|
Search This Site:
|
|