scache_connect and scache_pconnect prepares and opens socket level connection to scached backend but doesn't query anything. These functions should be considered as default connection methods to be used. Since these functions doesn't query anything, actual result for session availability occurs delayed when first round trip query is invoked. scache_pconnect reuses existing connections even they might be opened from completely different application. scache_connect instead opens new connection every time.
id is in most cases the cookie or whatever you send to browsers to distinguish them between. secret is meant to be web application specific check to be matched on a case where same scached is used between not so trustworthy apps and servers. If secret is set it must match on further opens. On a simple local-only scacheds secret has no sensible uses.
- scache_connect initiates tcp connection to scached, but doesn't query anything. It merely prepares connection to use values provided. scache_connect operates only on existing and valid sessions and never create a new one.
- scache_open initiates tcp connection and queries server. If session with provided id exists and optional secret matches, scache_open returns that session. If session doesn't exist, it will be created and scache_open acts like scache_reset. If return value is not FALSE, session is verified to be valid unlike with scache_connect where validness is not resolved out until on first actual operation.
- scache_reset also initiates tcp connection and queries server, but it always resets and creates session whether it exists or not, as long as secret match if set on possibly existing session. As like with scache_open non-false result is actually verified.
On class interface there is no connect-method. Constructing SCache object is equivalent to scache_(p)connect -functions.
Session resource on success, FALSE on failure. All other functions operate on this returned resource as their first parameter.
On a normal life-cycle you should separate user login from other activities and create session on that login phase with either scache_open or scache_reset. With scache_open you can detect already existing session and choose to select another session identifier while scache_reset always resets session leaving undetectable possibility there is another user using same identifier. It's a case how much you trust you id's uniqueness.
On the later page loads you should acquire session resource with scache_connect to avoid unnecessary backend queries. Good practice is to separate session initialization from it's further use.
Finally you log out the user by destroying session with scache_drop
In most cases session conversation with scached backend begins in page processing by connecting to scached backend with scache_pconnect and querying certain general session values from backend for example with scache_iov. In this default construction you should check result value from that first query wheter it fails and result value returned by scache_lasterr is SCERR_NO_SESSION to detect failed or unavailable session.
<?php $my_session_identifier = $_COOKIE['MySessionIdentifier']; $session = scache_pconnect($my_session_identifier); ?>