top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Python: Cookie gets changed when hit comes from a referrer

+1 vote
85 views
# 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 by it.

All work well except the situation where the user visits my webpage by clicking a backlink on another webpage.

Then for some reason the cookieID changes to another value thus a new entry appears into the database when insert happens.

What can I do about that?

posted Oct 8, 2013 by Satish Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
You are aware that using cookies to track a user who doesn't want to be  tracked won't work, because he'll just tell his browser to not use cookies, aren't you.

If a user doesn't want to be tracked, you can't track them. The user controls all the data their machine sends to you. This means that they can manipulate it. Nothing you can do  will prevent this.

1 Answer

+1 vote

The most likely possibility is that the domain doesn't match. For example, the cookie is set for the domain www.foo.com, and the other webpage is linking to foo.com. Another possibility is that the cookie is expiring because the browser session was terminated, not because of anything to do with the other webpage.

Or it could simply be a bug or unusual setting in whatever browser you're using to test it.

answer Oct 8, 2013 by anonymous
I think this is the problem but i am not sure entirely how you mean. Can you please explain it a bit more?

Shall i change cookie['ID']['path'] = '/' to something else so that never happens?
Similar Questions
+1 vote

I use apache 2.4 on centos.

I want to protect the access to a page (/my_folder/secure) with a cookie (in my exemple the cookie name is : my_cookie_name)

I would like that some machine with IP 192.1.1.10 and 192.1.1.11 can access the server without the cookie. This two IP address don't need to have the cookie for access to /my_folder/secure)

And all other IP address are redirect to address https://my_register_site.com if they don't have the cookie.

How to write this ? Some example, and of course it does not work.

Session On 
RewriteEngine on 
RewriteCond %{HTTP_COOKIE} !my_cookie_name 
RewriteCond %{REMOTE_ADDR} !^192.1.1.10$ 
RewriteCond %{REMOTE_ADDR} !^192.1.1.11$ 
RewriteRule ^(.*)$ https://my_register_site.com [NC,L,R=301] 
+1 vote

I'm trying to figure out how I can create custom session cookies. I've found the Manager interface for creating the sessions, but there's nothing about the actual session cookie. I don't see anything in the Valve interface that will let me do this either. Is this possible in Tomcat 7 (or 8?).

+3 votes

I just updated this morning my Python from a 3.3rc to 3.4 (Windows) and I noticed that the 'Green' color in tkinter GUI is not the same at all.

'Green' in 3.4 is very dark. I had to replace it with 'Lime' to get back a nice 'Green'.

+2 votes

I have a python web application running on apache2 deployed with mod_wsgi. The application has a thread continuously running. This thread is a ZeroMQ thread and listening to a port in loop. The application is not maintaining session. Now if I open the browser and sends a request to the apache server the data is accepted for the first time. Now when second time I send the request It shows Internal server error. When I checked the error log file for traceback, It shows the ZMQError:- The address already in use.

Does apache reloads the application on each request sent from the browser since so that the ZeroMQ thread is being created everytime and being assigned the port but since the port has already been assigned it shows error....

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...