Cookie gets changed when hit comes from a referrer

  • Thread starter Îίκος Αλεξόπουλος
  • Start date
Î

Îίκος Αλεξόπουλος

# initialize cookie and retrieve cookie from clients browser
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )

if cookie.get('ID') is not None:
cookieID = cookie['ID'].value
else:
cookieID = random.randrange(0, 9999)
cookie['ID'] = cookieID
cookie['ID']['path'] = '/'
print( cookie )

===========
===========

I use this code to retrive or set a cookie to the visitor's browser if
present and identify him bu it.

All work well except the situation where the user visits my webpage by
clicking a backlink on another wbpage.

Then for some reason the cookieID changes to another value thus a new
entry appears into the database when insert happens.

What cna i do about that?
 
I

Ian Kelly

I use this code to retrive or set a cookie to the visitor's browser if
present and identify him bu it.

All work well except the situation where the user visits my webpage by
clicking a backlink on another wbpage.

Then for some reason the cookieID changes to another value thus a new entry
appears into the database when insert happens.

What cna i do about that?

This question is really about HTTP, not Python, so you'd have better
luck asking elsewhere. The most likely possibility is that the domain
doesn't match. For example, the cookie is set for the domain
www.foo.com, and the other webpage is linking to foo.com. Another
possibility is that the cookie is expiring because the browser session
was terminated, not because of anything to do with the other webpage.
Or it could simply be a bug or unusual setting in whatever browser
you're using to test it.
 
Î

Îίκος Αλεξόπουλος

Στις 8/10/2013 2:08 μμ, ο/η Ian Kelly έγÏαψε:
This question is really about HTTP, not Python, so you'd have better
luck asking elsewhere. The most likely possibility is that the domain
doesn't match. For example, the cookie is set for the domain
www.foo.com, and the other webpage is linking to foo.com. Another
possibility is that the cookie is expiring because the browser session
was terminated, not because of anything to do with the other webpage.
Or it could simply be a bug or unusual setting in whatever browser
you're using to test it.

When i direct hit http://superhost.gr the cookie remains the same it is
not lost.

Also i have set:
ookie['ID']['expires'] = 60*60*24*365 #this cookie will expire in a year

but that didnt also help much because the cookie is also changing when
the hit comes through a referrer.

So, i cannot se the cookie down to its feet my whole insert or update
procedure breaks and i have duplicate entried for the same hostnames.

Where shoudl i rely to identify a visitor?
I was relying on tis hostname(although i know that after a router reset)
it changes, and then couple days ago i was thiking of relying to a
cookie that i would/set retrive from the vistirs browser, but if it
changes all the time i cannot evan rely to that.
 
Î

Îίκος Αλεξόπουλος

Στις 8/10/2013 2:08 μμ, ο/η Ian Kelly έγÏαψε:
This question is really about HTTP, not Python, so you'd have better
luck asking elsewhere. The most likely possibility is that the domain
doesn't match. For example, the cookie is set for the domain
www.foo.com, and the other webpage is linking to foo.com.

I think this is the problem but iam not sure entirely how you mean.
Can you please explain it a bit more?

Shall i change cookie['ID']['path'] = '/' to something else so that
never happens?
 
D

Denis McMahon

I use this code to retrive or set a cookie to the visitor's browser if
present and identify him bu it.

You are aware that using cookies to track a user who doesn't want to be
tracked won't work, because he'll just tell his browser to not use
cookies, aren't you.

Nick, if a user doesn't want to be tracked, you can't track them. The
user controls all the data their machine sends to you. This means that
they can manipulate it. Nothing you can do will prevent this.
 
Î

Îίκος Αλεξόπουλος

Στις 8/10/2013 6:55 μμ, ο/η Denis McMahon έγÏαψε:
You are aware that using cookies to track a user who doesn't want to be
tracked won't work, because he'll just tell his browser to not use
cookies, aren't you.

Nick, if a user doesn't want to be tracked, you can't track them. The
user controls all the data their machine sends to you. This means that
they can manipulate it. Nothing you can do will prevent this.

Yes iam aware of that, but its the best trcking method i can think of.
Tracking just the hostname is not accurate since with every router
restart, that info is changing.

Tracking the visitor by settign a cookie to its browser is not
perfect/accurate since he can manipulate its broswer data or flush the
cookies but this is the best one can do after having people register on
the webiste.

Or perhaps trying to identify the cookie + hostname is even better.

Can you help me with this particuler problem please?
 
J

Joel Goldstick

Στις 8/10/2013 6:55 μμ, ο/η DenisMcMahon έγÏαψε:
Browser cookies have been defined and around for a very long time. If
you google "browser cookie tutorial" you can learn how they work --
probably within an hour!. This will help you find your solution
The first poster pointed out that www.example.com and example.com can
be considered different domains. You can make a cookie work for both
but you need to understand cookies to learn how.

This is off topic, ... again!
 
Î

Îίκος Αλεξόπουλος

Is there any better way to identif a previous visitor? i tried cookies
which failed for me for the reason i opened this thread and host like
follows:

# try to locate the visitor
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and host =
%s''', (cID, host) )
data = cur.fetchone()

if not data:
# if first time visitor on this page, create new record
cur.execute('''INSERT INTO visitors (counterID, host, city, useros,
browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s)''',
(cID, host, city, useros, browser, ref, lastvisit) )
else:
# since visitor exists just update his record
cur.execute('''UPDATE visitors SET city = %s, useros = %s, browser =
%s, ref = %s, hits = hits + 1, lastvisit = %s''', (city, useros,
browser, ref, lastvisit) )
=======

Please tell me if you can think fo something else.
 
J

Joel Goldstick

Is there any better way to identif a previous visitor? i tried cookies which
failed for me for the reason i opened this thread and host like follows:

# try to locate the visitor
cur.execute('''SELECT * FROM visitors WHERE counterID =%s
and host = %s''', (cID, host) )
data = cur.fetchone()

if not data:
# if first time visitor on this page, create new
record
cur.execute('''INSERT INTO visitors (counterID,
host, city, useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s,
%s)''',
(cID, host, city, useros,
browser, ref, lastvisit) )
else:
# since visitor exists just update his record
cur.execute('''UPDATE visitors SET city = %s, useros
= %s, browser = %s, ref = %s, hits = hits + 1, lastvisit = %s''', (city,
useros, browser, ref, lastvisit) )
=======

Please tell me if you can think fo something else.

Yes! there is a very simple and comprehensive way to learn about your
visitors. Use Google Analytics. Its free, you only need a google
account to open an analytics account. They give you a small bit of
javascript that you copy and past to your pages. If you are using a
template to create your pages, this is easy, since you just add google
code to the template.
 
D

Denis McMahon

Can you help me with this particuler problem please?

Unfortunately I can't, because I am unable to reproduce the problem you
describe.

When I load my test page in the browser, then replace it with something
else by entering an address in the address bar and pressing return, then
use the back link followed by the reload one, I am back at my test page
with the original cookie value.

Of course, this is using my cookie etc code and mechanisms, and not
yours ....

Now, either it's an issue in your python implementation of cookie
handling which isn't happening in my implementation, or it's something to
do with the way that your system passes data around (cgi) that doesn't
happen in mine (mod_wsgi), or it's happening in the browser you're
testing in, but not in my browser.

Have you checked the cookie jar in the browser to see what value the
cookie has? Is that the value you think it should have? Note that
checking the cookie jar is a browser topic, not a python topic, so if you
don't know how to do that you're going to have to find the right place to
ask, WHICH IS NOT HERE!

Ideally you need to check what the server thinks it's setting the cooking
to, what the browser thinks it received as the cookie, and what the
server gets back afterwards to work out where the error is happening.
 
Î

Îίκος Αλεξόπουλος

Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγÏαψε:
Unfortunately I can't, because I am unable to reproduce the problem you
describe.

When I load my test page in the browser, then replace it with something
else by entering an address in the address bar and pressing return, then
use the back link followed by the reload one, I am back at my test page
with the original cookie value.

Of course, this is using my cookie etc code and mechanisms, and not
yours ....

Now, either it's an issue in your python implementation of cookie
handling which isn't happening in my implementation, or it's something to
do with the way that your system passes data around (cgi) that doesn't
happen in mine (mod_wsgi), or it's happening in the browser you're
testing in, but not in my browser.

Have you checked the cookie jar in the browser to see what value the
cookie has? Is that the value you think it should have? Note that
checking the cookie jar is a browser topic, not a python topic, so if you
don't know how to do that you're going to have to find the right place to
ask, WHICH IS NOT HERE!

Ideally you need to check what the server thinks it's setting the cooking
to, what the browser thinks it received as the cookie, and what the
server gets back afterwards to work out where the error is happening.

Is there something i can try to isolate the problem and make it work?
By whole counters project is based on cookie handling now....
 
N

Ned Batchelder

Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγÏαψε:

Is there something i can try to isolate the problem and make it work?
By whole counters project is based on cookie handling now....

Nikos, as a few people have mentioned already, this is no longer a
Python question. You need to find other avenues of support.

--Ned.
 
S

Steven D'Aprano

Is there something i can try to isolate the problem and make it work?

Of course there is. That is part of the job of the developer: hard work
trying dozens, maybe hundreds of different things until you isolate the
problem. There are no shortcuts, no magic button you can push to
immediately identify the source of the problem.

If you are not willing to spend hours, maybe days or weeks, working on
this problem, then you should hire a programmer who is, and stop fooling
yourself that you are a professional developer. An amateur who programs
for fun can just give up when a problem becomes too difficult and isn't
fun any more. A professional has to keep going.

Start by identifying which browsers this occurs on. You should test using
at least Firefox, Internet Explorer, Safari, Chrome and Opera, for as
many different versions as you can find. You should also test with less
common browsers such as Konqueror, Epiphany, lynx, links and others. See
if there is a pattern in which ones behave as you expect and which ones
don't.

You should also test with and without cookies enabled, ad-blockers, and
similar. Maybe you can replicate the problem if (say) the user accepts
the first cookie, then rejects it when they click Back.

If this only occurs with a single version of a single browser with
cookies enabled and no ad blocker, you should report it as a bug to the
browser developers. Make sure you give them enough detail to replicate
the problem. If it's an old version, they'll probably say Won't Fix, and
you'll just have to accept that your cookie handling code won't work for
some percentage of visitors.

Have you checked that the server is setting the cookie values you expect?
Have you checked the value of the cookie in the browser? If you don't
know how to do these things, this site will teach you everything you need:

https://duckduckgo.com/

Follow the links until you reach enlightenment. There are *thousands* of
pages on debugging programming problems.

If you find it is broken with *all* of the above browsers, then you
should suspect a bug in your Python code. In that case, since other
people have failed to reproduce the reported problem, you are obviously
doing something different than what you are telling us you are doing.
Only in this case should you come back here to ask for help with your
Python code. Before you do, read this, and follow the instructions:

http://www.sscce.org/

If you are not willing to do these things, then stop pretending to be a
professional developer, and admit that you are only programming for fun.
There is no shame in this -- not everyone is cut out to be a professional
programmer, just as not everybody makes a good doctor or taxi driver or
carpenter.

By whole counters project is based on cookie handling now....

If you cannot solve the cookie problem, maybe you should reconsider the
decision to rely on cookies.
 
I

Ian Kelly

Also i have set:
ookie['ID']['expires'] = 60*60*24*365 #this cookie will expire in
a year

The Expires attribute takes a date. If you're passing an interval in
seconds then you should use the Max-Age attribute instead.

That said, I think I misunderstood the problem initially. You are
saying that when the user is on another site, and they press the
browser's Back button to return to your page, your host is not
recording a visit from the cookie you've given them? This is probably
happening because the browser is not actually sending a request to
your web server when it navigates back, unless the user specifically
requests a refresh. Otherwise, most browsers will just use the cached
page already in memory in this situation. As far as the server is
concerned, nothing has happened.
 
Î

Îίκος Αλεξόπουλος

Στις 9/10/2013 7:53 πμ, ο/η Ian Kelly έγÏαψε:
Also i have set:
ookie['ID']['expires'] = 60*60*24*365 #this cookie will expire in
a year

The Expires attribute takes a date. If you're passing an interval in
seconds then you should use the Max-Age attribute instead.

That said, I think I misunderstood the problem initially. You are
saying that when the user is on another site, and they press the
browser's Back button to return to your page, your host is not
recording a visit from the cookie you've given them? This is probably
happening because the browser is not actually sending a request to
your web server when it navigates back, unless the user specifically
requests a refresh. Otherwise, most browsers will just use the cached
page already in memory in this situation. As far as the server is
concerned, nothing has happened.

No i dont mean that.

When a user hits my link on another website, for exmaple they are on
ypsilandio.gr and they hit the link of superhost.gr then a new entry
with a new cookie is appearing into my visitors table!

Where is the old cookie that was saved in my browser so it will get
retrieved? I use chrome and i notice that when a visitor comes to my
webpage form a referrer link the cookie's ID is always set to a new
random value.

I have no idea why.
Why would it metter from where you sre coming from?
The cookie ust have beeen present in the visitor's browser, shouldnt it?
 
M

Mark Lawrence

Is there something i can try to isolate the problem and make it work?

As you are the problem why not try solitary confinement? :)

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
Î

Îίκος Αλεξόπουλος

Στις 9/10/2013 4:33 πμ, ο/η Steven D'Aprano έγÏαψε:
Of course there is. That is part of the job of the developer: hard work
trying dozens, maybe hundreds of different things until you isolate the
problem. There are no shortcuts, no magic button you can push to
immediately identify the source of the problem.

If you are not willing to spend hours, maybe days or weeks, working on
this problem, then you should hire a programmer who is, and stop fooling
yourself that you are a professional developer. An amateur who programs
for fun can just give up when a problem becomes too difficult and isn't
fun any more. A professional has to keep going.

Start by identifying which browsers this occurs on. You should test using
at least Firefox, Internet Explorer, Safari, Chrome and Opera, for as
many different versions as you can find. You should also test with less
common browsers such as Konqueror, Epiphany, lynx, links and others. See
if there is a pattern in which ones behave as you expect and which ones
don't.

You should also test with and without cookies enabled, ad-blockers, and
similar. Maybe you can replicate the problem if (say) the user accepts
the first cookie, then rejects it when they click Back.

If this only occurs with a single version of a single browser with
cookies enabled and no ad blocker, you should report it as a bug to the
browser developers. Make sure you give them enough detail to replicate
the problem. If it's an old version, they'll probably say Won't Fix, and
you'll just have to accept that your cookie handling code won't work for
some percentage of visitors.

Have you checked that the server is setting the cookie values you expect?
Have you checked the value of the cookie in the browser? If you don't
know how to do these things, this site will teach you everything you need:

https://duckduckgo.com/

Follow the links until you reach enlightenment. There are *thousands* of
pages on debugging programming problems.

If you find it is broken with *all* of the above browsers, then you
should suspect a bug in your Python code. In that case, since other
people have failed to reproduce the reported problem, you are obviously
doing something different than what you are telling us you are doing.
Only in this case should you come back here to ask for help with your
Python code. Before you do, read this, and follow the instructions:

http://www.sscce.org/

If you are not willing to do these things, then stop pretending to be a
professional developer, and admit that you are only programming for fun.
There is no shame in this -- not everyone is cut out to be a professional
programmer, just as not everybody makes a good doctor or taxi driver or
carpenter.



If you cannot solve the cookie problem, maybe you should reconsider the
decision to rely on cookies.
I managed t overcome it like this:

cur.execute('''UPDATE visitors SET cookieID = %s, host = %s, city = %s,
useros = %s, browser = %s, ref = %s, hits = hits + 1, lastvisit = %s
WHERE counterID = %s and host = %s''',
(cookieID, host, city, useros, browser, ref, lastvisit, cID, host) )

if not cur.rowcount:
# if first time visitor on this page, create new record, if visitor
exists then update record
cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE host = %s, city = %s, useros = %s, browser = %s,
ref = %s, hits = hits + 1, lastvisit = %s''',
(cID, cookieID, host, city, useros, browser, ref, lastvisit, host, city,
useros, browser, ref, lastvisit) )


But thats a not clear way to handle the cookie because i involve host to
help me identify its recorde since when my website hit comes from areferrer.

i also tried adding the domain when i set the cookie but this didnt
helped me at all:

# initialize cookie and retrieve cookie from clients browser
cookie = cookies.SimpleCookie( os.environ['HTTP_COOKIE'] )

if cookie.get('ID') is not None:
cookieID = cookie['ID'].value
else:
cookieID = random.randrange(0, 9999)
cookie['ID'] = cookieID
cookie['ID']['domain'] = ".superhost.gr"
cookie['ID']['path'] = '/'
cookie["ID"]["expires"] = 60*60*24*365 # this cookie will expire in a year

i read some links from duckduckgo but that didnt help me solve this.
Please someone esle try to reproduce the problem by just using cgi and
not mod_wsgi.

ps. Really why duckduckgo and not google.com ?
 
M

Mark Lawrence

On 09/10/2013 09:24, Îίκος Αλεξόπουλος wrote:

You have been told repeatedly that your questions have nothing to do
with Python, e.g. Ben Finney just over an hour ago "None of this has to
do with Python. Please do not ask this Python doscussion forum to
educate you on how HTTP operates."

Please be courteous enough to take your questions to an appropriate forum.

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
D

Denis McMahon

Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγÏαψε:
Is there something i can try to isolate the problem and make it work?
By whole counters project is based on cookie handling now....

See those last two paragraphs there that you quoted. You should have read
them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top