Problem checking an existing browser cookie

N

Nik the Greek

It might be clearer if you reverse the condition and say:

     me_visiting = ...
     if not me_visiting:
         ...

I don't understand what are you trying to say

Please provide a full example.

You mean i should try it like this?

unless ( visitor and visitor.value == 'nikos' ) or re.search( r'(msn|
yandex|13448|spider|crawl)', host ) not None:

But isnt it the same thing like the if?
 
N

Nik the Greek

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

Add a print statement to verify that HTTP_COOKIE does indeed have a visitor.
Or use the stuff below as a template.

Here is a minimal script to set the visitor:

#!/usr/bin/env python
import cgitb
cgitb.enable()

import cgi
import Cookie
import os
import sys

form = cgi.FieldStorage()
name = form.getfirst("name", "Unknown")

cookie = Cookie.SimpleCookie()
cookie["visitor"] = name

sys.stdout.write(cookie.output())
sys.stdout.write("\r\nContent-type: text/plain\r\n\r\n")
print "Hello, your new name is", name

Invoke it with the equivalent of

http://localhost:8000/cgi-bin/set_visitor.py?name=Nikos

for your website. Then proceed with a small script to show the visitor's
name:

#!/usr/bin/env python
import cgitb
cgitb.enable()

import cgi
import Cookie
import os
import sys

cookie = Cookie.SimpleCookie()
cookie.load(os.environ.get("HTTP_COOKIE"))

visitor = cookie.get("visitor")
if visitor is None:
    visitor_name = "Unknown"
else:
    visitor_name = visitor.value

sys.stdout.write("Content-type: text/plain\r\n\r\n")
print "Hello,", visitor_name
print
print
print "HTTP_COOKIE=%r" % os.environ.get("HTTP_COOKIE")

which you can invoke with the equivalent of

http://localhost:8000/cgi-bin/show_visitor.py

With some luck you should see your name and can proceed to adapt your script
accordingly.

Peter

The cookie is set tyo the browser with the code i provided few posts
back

The problem is that i for soemreason cant check correctly its
existance.
 
N

Nik the Greek

It might be clearer if you reverse the condition and say:

     me_visiting = ...
     if not me_visiting:
         ...


# initialize cookie
cookie = SimpleCookie()
cookie.load( os.environ.get('HTTP_COOKIE', '') )
visitor = cookie.get('visitor')

This statement

if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448|
spider|crawl)', host ) is None:

produces the following error:

/home/webville/public_html/cgi-bin/counter.py
93 # do not increment the counter if a Cookie is set to the
visitors browser already
94 #
=================================================================================================================
95 if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448|
spider|crawl)', host ) is None:
96
97 print visitor
visitor = None, visitor.value undefined, re = <module 're' from '/usr/
lib64/python2.4/re.pyc'>, re.search = <function search>, host =
'178-128-217.dynamic.cyta.gr', builtin None = None

Why visitor.value is undefined?
 
R

Rami Chowdhury

# initialize cookie
cookie = SimpleCookie()
cookie.load( os.environ.get('HTTP_COOKIE', '') )
visitor = cookie.get('visitor')

This statement

if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448|
spider|crawl)', host ) is None:

produces the following error:

 /home/webville/public_html/cgi-bin/counter.py
  93 # do not increment the counter if a Cookie is set to the
visitors browser already
  94 #
=================================================================================================================
  95 if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448|
spider|crawl)', host ) is None:
  96
  97         print visitor
visitor = None, visitor.value undefined, re = <module 're' from '/usr/
lib64/python2.4/re.pyc'>, re.search = <function search>, host =
'178-128-217.dynamic.cyta.gr', builtin None = None

Why visitor.value is undefined?

Because, as the traceback tells you, visitor is None. As you said, the
cookie is not recognized. Try inserting the following line at the top
of your script:

print "Content-type:text/html\r\n\r\n", os.environ.get('HTTP_COOKIE',
'NO COOKIE DATA')

That should give you the value of the cookie, which might help you
debug why it is not being loaded.
 
M

MRAB

I don't understand what are you trying to say

Please provide a full example.

You mean i should try it like this?

unless ( visitor and visitor.value == 'nikos' ) or re.search( r'(msn|
yandex|13448|spider|crawl)', host ) not None:

But isnt it the same thing like the if?

My point is that the logic might be clearer to you if you think first
about how you know when you _are_ the visitor.
 
M

MRAB

# initialize cookie
cookie = SimpleCookie()
cookie.load( os.environ.get('HTTP_COOKIE', '') )
visitor = cookie.get('visitor')

This statement

if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448|
spider|crawl)', host ) is None:

produces the following error:

/home/webville/public_html/cgi-bin/counter.py
93 # do not increment the counter if a Cookie is set to the
visitors browser already
94 #
=================================================================================================================
95 if (visitor.value != 'nikos') and re.search( r'(msn|yandex|13448|
spider|crawl)', host ) is None:
96
97 print visitor
visitor = None, visitor.value undefined, re =<module 're' from '/usr/
lib64/python2.4/re.pyc'>, re.search =<function search>, host =
'178-128-217.dynamic.cyta.gr', builtin None = None

Why visitor.value is undefined?

Because visitor is None. It's not seeing any cookie.
 
N

Nik the Greek

Because, as the traceback tells you, visitor is None. As you said, the
cookie is not recognized. Try inserting the following line at the top
of your script:

print "Content-type:text/html\r\n\r\n", os.environ.get('HTTP_COOKIE',
'NO COOKIE DATA')

That should give you the value of the cookie, which might help you
debug why it is not being loaded.

Unfortunately it produces an Internal Server Error :(
 
N

Nik the Greek

My point is that the logic might be clearer to you if you think first
about how you know when you _are_ the visitor.

Well my idea was to set a cookie on my browser with the name visitor
and a value of "nikos" and then check each time that cooki. if value
is "nikos" then dont count!

I could also pass an extra url string like http://webville.gr?show=nikos
and check that but i dont like the idea very much of giving an extra
string each time i want to visit my webpage.
So form the 2 solution mentioned the 1st one is better but cant come
into action for some reason.

Aprt form those too solution i cant think of anyhting else that would
identify me and filter me out of the actual guest of my website.

I'm all ears if you can think of something else.
 
N

Nik the Greek

Well my idea was to set a cookie on my browser with the name visitor
and a value of "nikos" and then check each time that cooki. if value
is "nikos" then dont count!

I could also pass an extra url string likehttp://webville.gr?show=nikos
and check that but i dont like the idea very much of giving an extra
string each time i want to visit my webpage.
So form the 2 solution mentioned the 1st one is better but cant come
into action for some reason.

Aprt form those too solution i cant think of anyhting else that would
identify me and filter me out of the actual guest of my website.

I'm all ears if you can think of something else.

Is there any other way for the webpage to identify me and filter me
out except checking a cookie or attach an extra url string to the
address bar?
 

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,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top