Cookie aint retrieving when visiting happens from a backlink

Cookie aint retrieving when visiting happens from a backlink

Postby NikosGr » Fri Oct 25, 2013 7:23 am

Hello i having the following code to try and retrieve the visitor's saved cookie form the browser.

Code: Select all
# initialize cookie and retrieve cookie from clients browser
try:
   cookie = cookies.SimpleCookie( os.environ['HTTP_COOKIE'] )
   cookieID = cookie['name'].value
except:
   cookieID = 'visitor'


It works as expected except form the fact from when the visitor enters my webpage(superhost.gr) by clicking a backlink of another webpage.

Then even if the cookie exists in his browser for some reason the try fails and except take actions.

Can somebody explain why this is happening?

You can see this action yourself by hitting:

1. superhost.gr as a direct hit
2. by clicking superhost.gr's backlink from ypsilandio.gr/mythosweb.gr

You will see than in 2nd occasion another ebtry will appear in the database here:

http://superhost.gr/?show=log&page=index.html
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Cookie aint retrieving when visiting happens from a back

Postby NikosGr » Fri Oct 25, 2013 11:44 am

Someone have an idea?
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Cookie aint retrieving when visiting happens from a back

Postby micseydel » Fri Oct 25, 2013 6:04 pm

The link you have got me a 404.

As for your problem, you have a blanket except block which is catching unexpected exceptions. Just catch NameError for the one you expect, and then let it raise otherwise, so that you can find and if necessary report to us the unexpected exception.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Cookie aint retrieving when visiting happens from a back

Postby NikosGr » Fri Oct 25, 2013 9:13 pm

Code: Select all
# initialize cookie and retrieve cookie from clients browser
try:
   cookie = cookies.SimpleCookie( os.environ['HTTP_COOKIE'] )
   cookieID = cookie['name'].value
except( NameError):
   cookieID = random.randrange(0, 9999)
   cookie['ID'] = cookieID
   cookie['ID']['path'] = '/'
   print( cookie )


Is this what you mean? I'm ot usre i havce understood you correctly.n
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Cookie aint retrieving when visiting happens from a back

Postby micseydel » Fri Oct 25, 2013 9:35 pm

The except line is correct, but the block will always result in a TypeError: 'int' object has no attribute '__getitem__'. I'm not really sure what you intended for it to be.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Cookie aint retrieving when visiting happens from a back

Postby NikosGr » Sat Oct 26, 2013 1:00 am

Sorry i meant this.

Code: Select all
# initialize cookie and retrieve cookie from clients browser
try:
   cookie = cookies.SimpleCookie( os.environ['HTTP_COOKIE'] )
   cookieID = cookie['name'].value
except( NameError ):
   cookieID = random.randrange(0, 9999)
   cookie['name'] = cookieID
   cookie['name']['path'] = '/'
   print( cookie )


But if visit comes from a referrer backlink then the cookie cannot be retrived and is set again, if it comes as a direct hot it doesn't.
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Cookie aint retrieving when visiting happens from a back

Postby micseydel » Sat Oct 26, 2013 1:22 am

NikosGr wrote:
Code: Select all
   cookieID = random.randrange(0, 9999)
   cookie['name'] = cookieID
   cookie['name']['path'] = '/'

That code is inherently incorrect (even assuming that cookie is defined somewhere before).
Code: Select all
cookie['name']

is a number, specifically an integer. So the line
Code: Select all
   cookie['name']['path'] = '/'

is equivalent to
Code: Select all
0['path'] = '/'

Which causes
Code: Select all
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object does not support item assignment

(I'm using a different computer from before, so the exception is a little bit different, but it will always be a TypeError in any reasonable circumstances.)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Cookie aint retrieving when visiting happens from a back

Postby NikosGr » Sat Oct 26, 2013 1:36 am

Yes, NameError si compkainign sayign that:

keyerror 'name' is not defined.

So thne, how should i suppose to write it?
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki


Return to Web Development

Who is online

Users browsing this forum: No registered users and 2 guests