#1
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0

    Question Cookie gets changed when page hit comes from a referrer


    Code:
    # 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?
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    I managed to overcome it like this:


    Code:
    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:

    Code:
    # 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
  4. #3
  5. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    Someone please?

IMN logo majestic logo threadwatch logo seochat tools logo