Problem checking an existing browser cookie

MRAB python at mrabarnett.plus.com
Mon Aug 30 03:51:08 CEST 2010


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?

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'



More information about the Python-list mailing list