Problem checking an existing browser cookie

Nik the Greek nikos.the.gr33k at gmail.com
Mon Aug 30 15:48:33 CEST 2010


On 30 Αύγ, 11:01, Nik the Greek <nikos.the.gr... at gmail.com> wrote:
> On 30 Αύγ, 06:12, MRAB <pyt... at mrabarnett.plus.com> wrote:
>
>
>
>
>
>
>
>
>
> > On 30/08/2010 03:55, Nik the Greek wrote:
>
> > > On 30 Αύγ, 05:43, MRAB<pyt... at mrabarnett.plus.com>  wrote:
> > >> On 30/08/2010 03:07, Nik the Greek wrote:
>
> > >>> On 30 Αύγ, 04:51, MRAB<pyt... at mrabarnett.plus.com>    wrote:
> > >>>> On 30/08/2010 02:14, Νίκος wrote:
>
> > >>>>> On 29 Αύγ, 21:44, MRAB<pyt... at mrabarnett.plus.com>      wrote:
> > >>>>>> On 29/08/2010 06:34, Νίκος wrote:
>
> > >>>>>>> On 28 Αύγ, 23:15, MRAB<pyt... at mrabarnett.plus.com>        wrote:
> > >>>>>>>> On 28/08/2010 20:37, Íßêïò wrote:
>
> > >>>>>>>>> On 22 Áýã, 10:27, Íßêïò<nikos.the.gr... at gmail.com>          wrote:
> > >>>>>>>>>> On 16 Áýã, 14:31, Peter Otten<__pete... at web.de>          wrote:
>
> > >>>>>>>>>>> Íßêïò wrote:
> > >>>>>>>>>>>> # initializecookie
> > >>>>>>>>>>>> cookie=Cookie.SimpleCookie()
> > >>>>>>>>>>>> cookie.load( os.environ.get('HTTP_COOKIE', '') )
> > >>>>>>>>>>>> mycookie =cookie.get('visitor')
>
> > >>>>>>>>>>>> if ( mycookie and mycookie.value != 'nikos' ) or re.search( r'(cyta|
> > >>>>>>>>>>>> yandex|13448|spider|crawl)', host ) is None:
> > >>>>>>>>>>>>          blabla...
> > >>>>>>>>>>>> ========================
>
> > >>>>>>>>>>>> I checked and Chrome has acookienames visitor with a value ofnikos
> > >>>>>>>>>>>> within.
> > >>>>>>>>>>>> So, i have to ask why the if fails?
>
> > >>>>>>>>>>> Maybe it's because != != ==
>
> > >>>>>>>>>> Iwant ti if code block to be executed only if the browsercookienames
> > >>>>>>>>>> visitor fetched doesnt cotnain the vbalue of 'nikos'
>
> > >>>>>>>>>> Is there somethign wrong with the way i wrote it?
>
> > >>>>>>>>> Please do help me with this too becaus eif i dont solve this my
> > >>>>>>>>> website keeps count my each visit like iam a guest visitor!
>
> > >>>>>>>> Print out mycookie, repr(mycookie.value) (unless mycookie is None) and
> > >>>>>>>> repr(host). Then follow the code yourself to see whether the condition
> > >>>>>>>> is True.
>
> > >>>>>>> print mycookie outputs 'None'
>
> > >>>>>>> Thts weird because i check with the browser and the cookie is there!
>
> > >>>>>> Just because you can see it doesn't mean your code can.
>
> > >>>>>>> print repr(host) outputs '78-236-176.adsl.cyta.gr'
>
> > >>>>>>> repr(mycookie.value) (unless mycookie is None)
>
> > >>>>>>> and also
>
> > >>>>>>> print mycookie.value gives an error too. Maybe there is not a value
> > >>>>>>> method?
>
> > >>>>>> If mycookie is None, then it's not surprising that doesn't have 'value'.
>
> > >>>>>> In summary, mycookie is None, so:
>
> > >>>>>>         mycookie and mycookie.value != 'nikos'
>
> > >>>>>> is false (actually None, which is treated as false).
>
> > >>>>>> host == '78-236-176.adsl.cyta.gr', so:
>
> > >>>>>>         re.search(r'(cyta|yandex|13448|spider|crawl)', host)
>
> > >>>>>> finds 'cyta' and the search returns a match.
>
> > >>>>>> false or false == false
>
> > >>>>>> blabla... isn't executed.
>
> > >>>>> Lets forget the 2nd or argument, ill put it off
>
> > >>>>> so we have this now
>
> > >>>>> if ( mycookie and mycookie.value != 'nikos' ):
> > >>>>>        #do stuff as long as there ins't a cookie names visitor with a
> > >>>>> value of nikos in the broswer
>
> > >>>>> What does it mean practically that the mycookie equals to None?
> > >>>>> That mycookie doesnt exist?
>
> > >>>>> How should i write this if block to mkake sure it checks whether or
> > >>>>> not the cookie exists?
>
> > >>>> Under what conditions do you want to execute the block?
>
> > >>>> This:
>
> > >>>>        mycookie and mycookie.value != 'nikos'
>
> > >>>> will be true if:
>
> > >>>>        there _is_ a cookie, but its value isn't 'nikos'
>
> > >>>> I think that you want is to execute the block if someone else is
> > >>>> visiting. Correct?
>
> > >>> Yes that exactyl right!
>
> > >>> To make sure a cookie is set i have a script names koukos.py
> > >>> containing this:
>
> > >>> ==============================
> > >>> #!/usr/bin/python
> > >>> # -*- coding: utf-8 -*-
>
> > >>> import cgitb; cgitb.enable()
> > >>> import cgi, os, Cookie
>
> > >>> # initialize cookie
> > >>> cookie = Cookie.SimpleCookie()
> > >>> cookie.load( os.environ.get('HTTP_COOKIE', '') )
> > >>> mycookie = cookie.get('visitor')
>
> > >>> htmlBody = []
>
> > >>> # if visitor cookie does exist
> > >>> if ( mycookie and mycookie.value == 'nikos' ):
> > >>>       htmlBody.append('ΑΠΟ ΤΗΝ ΕΠΟΜΕΝΗ ΕΠΙΣΚΕΨΗ ΣΟΥ ΘΑ ΣΕ ΥΠΟΛΟΓΙΖΩ ΩΣ
> > >>> ΕΠΙΣΚΕΠΤΗ ΑΥΞΑΝΟΝΤΑΣ ΤΟΝ ΜΕΤΡΗΤΗ!')
> > >>>       cookie['visitor'] = 'nikos'
> > >>>       cookie['visitor']['expires'] = -1      #this cookie will expire
> > >>> now
> > >>> else:
> > >>>       htmlBody.append('ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΝ ΣΕ ΕΙΔΑ, ΔΕΝ ΣΕ ΞΕΡΩ, ΔΕΝ
> > >>> ΣΕ ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΝ Ο ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!!')
> > >>>       cookie['visitor'] = 'nikos'
> > >>>       cookie['visitor']['expires'] = 60*60*24*30*12
>
> > >>> htmlBody.insert(0, 'Content-type: text/html; charset=UTF-8\n')
>
> > >>> print(cookie)
> > >>> print('\n'.join(htmlBody))
> > >>> =============================
>
> > >>> Which i seicth on and off according to my desire if i want to be
> > >>> counted or not!
>
> > >>>> How do you know when it _is_ you? There'll be a cookie which says it's
> > >>>> you?
>
> > >>>> If so, then you want to execute the block if there isn't any cookie, or
> > >>>> if there's a cookie but it doesn't say it's you:
>
> > >>>>        not mycookie or mycookie.value != 'nikos'
>
> > >>> i tried this as you suggested:
>
> > >>> if ( not mycookie or mycookie.value != 'nikos' ) or re.search( r'(msn|
> > >>> yandex|13448|spider|crawl)', host ) is None:
>
> > >>> but the counter keeps increasing although the cookie named visitor on
> > >>> my browser exist and also has the valuie of 'nikos'.
>
> > >>> Why it keeps increasing? Doesn't the if code "sees" that the cookie
> > >>> with a value of "nikos" is present!?!!
>
> > >> Previously your regex was r'(cyta|yandex|13448|spider|crawl)' and you
> > >> said that host was '78-236-176.adsl.cyta.gr', so it matched.
>
> > >> Now your regex is r'(msn|yandex|13448|spider|crawl)'. If host is still
> > >> '78-236-176.adsl.cyta.gr' then of course it doesn't match, so the
> > >> condition is true.
>
> > > Thats what i want.
>
> > > If host doesnt contain substringsa of the regex to execute if block.
> > > if it does match i dont want then if block to execute, hence the
> > > counter too.
>
> > > The reason i had 'cyta' before is because not mycookie or
> > > mycookie.value != 'nikos'  doesn't filter me out.
>
> > > The problem is what happens with the
>
> > > not mycookie or mycookie.value != 'nikos'
>
> > > Since cookie 'visitor' does contain the 'nikos' value why the if code
> > > blck gets executed?
>
> > This part:
>
> >      ( not mycookie or mycookie.value != 'nikos' )
>
> > is false but this part:
>
> >      re.search( r'(msn|yandex|13448|spider|crawl)', host ) is None
>
> > is true because host doesn't contain any of those substrings.
>
> So what is the problem here?
>
> And why mycookie when i print it it says None?
>
> Perhpas its doenst get loaded like that?
>
> # initialize cookie
> cookie = SimpleCookie()
> cookie.load( os.environ.get('HTTP_COOKIE', '') )
> mycookie = cookie.get('visitor')

Please someone else has an idea on how this to work?



More information about the Python-list mailing list