Asked  7 Months ago    Answers:  5   Viewed   35 times

I have some basic session handling in my application. In each page I check if the user is logged in. If they are then they're already identified by $_SESSION['user_id'], and their login/logout is recorded in a MySQL table.

I would also like to record visits by guests (not logged in) based on a unique id. I had assumed that once session_start() is called that an internal session_id is automatically generated and that calling session_id() I could retrieve this. But this just gives an "undefined variable" error, so I guess I have to set it manually..? If so then what's the best way so that it will be a unique ID, or what is the usual method?

Thanks for any help...

 Answers

84

There are 2 ways to use sessions and session id's in PHP:

1 - Auto generate the session ID and get it:

session_start();
$id = session_id();

2 - Set the session ID manually and then start it:

session_id( 'mySessionId' );
session_start();

If you intend to set the session ID, you must set it before calling session_start(); If you intend to generate a random session_id (or continue one already started in a previous page request) and then get that id for use elsewhere, you must call session_start() before attempting to use session_id() to retrieve the session ID.

Wednesday, March 31, 2021
 
astaykov
answered 7 Months ago
15

the php-memcached extension supports session locking

http://us3.php.net/memcached

http://us1.php.net/manual/en/memcached.sessions.php

the memcache and memcached extensions look syntactically similar so it may not be too much of a headache to give it a try. (memcached has a stable version 2.1.0 released 2012-08-07).


if you are set on using memcache 2.2.7 you will most likely have to implement the lock yourself by setting some "session_is_locked" variable in your session and then releasing/unsetting it when the script is done writing to the session. Then you'd always need to check if that variable is set before continuing with any scripts which write to the session.

Wednesday, March 31, 2021
 
penpen
answered 7 Months ago
87

session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.

Thursday, July 29, 2021
 
jab
answered 3 Months ago
jab
62

That was seen only with older versions of docker (issue 22343), but also more recently with 16.04.1 LTS, Docker version 1.12.1, build

It depends on which docker you are using, and on which host OS.

If you are on Ubuntu for instance, try removing docker completely (if you don't have any local work that you would want to keep)

sudo apt-get purge docker-engine
sudo apt-get autoremove --purge docker-engine
rm -rf /var/lib/docker # This deletes all images, containers, and volumes

Then install the latest docker 13.1 and see if the issue persists.

Again, this is only if you are ready to start again from a clean state, reinstalling Docker entirely.
Do not execute rm -rf /var/lib/docker without considering the consequences, or in a production environment(!).

rakeshz's answer using an apt-get install --reinstall is a safer first step.

Saturday, October 9, 2021
 
NoSenseEtAl
answered 2 Weeks ago
61

Django currently does not support composite primary key by default

What you can do instead is keep the auto generated id as the (surrogate) primary key and then define a unique_together relationship in the through table.

class Meta:
    unique_together = (course, person)

This way, you can guarantee unique entries in the through table, and when you reference the id it is the equivalent of referencing the unique (course, person) which is what we want anyways.

There are some third party apps that implement this feature if you want. However, unless an absolute necessity (like a legacy system support), I would just keep it simple and implement unique_together.

Saturday, October 16, 2021
 
mariaoialvarez
answered 5 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :