Make a PHP session code

 Make a PHP session code

session_start

int session_start(void) 
Initializes a session.
Returns:
Always returns TRUE
Description:
Initializes a session. If a session ID is sent as a GET or in a cookie and is a valid session identifier, the session is resumed.
Version:
Existing since version 4.0
Example:
Start a session
session_start(); 
if (!$counter) session_register("counter"); 
echo $counter; 
$counter++; 



<?php
// include URL utils library
require_once ‘include/url_utils.inc.php’;
// load configuration script
require_once ‘include/config.inc.php’;
// start PHP session
session_start();
// redirect affiliate links
if (isset($_REQUEST[‘aff_id’]))
{
// save the affiliate ID
$_SESSION[‘aff_id’] = $_REQUEST[‘aff_id’];
// obtain the URL with no affiliate ID
$clean_url = SITE_DOMAIN . remove_query_param($_SERVER[‘REQUEST_URI’], ‘aff_id’);
// 301 redirect to the new URL
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: ‘ . $clean_url);
}
// display affiliate details
echo ‘You got here through affiliate: ‘;
if (!isset($_SESSION[‘aff_id’]))
{
echo ‘(no affiliate)‘;
}
else
{
echo $_SESSION[‘aff_id’];
}
?>


session_set_save_handler

bool session_set_save_handler(string open, string close, string read, 
string write, string destroy, string gc) 
open
Session init function
close
Session shutdown function
read
Session read function
write
Session write function
destroy
Destroy session function
gc
Garbage collection function
Sets handlers for custom session functions.
Returns:
TRUE on success; FALSE on error
Description:
This function takes six arguments that describe the functions used when creating your own session-handling functions. Each function can have any name, but the names must be passed in the correct order. In addition, each function must return the correct information. Using this function, it's possible to write any session handler that you want, including those that store their information in a database, text files, DBM files, or shared memory.
bool open(string save_path, string session_name )
Executed when a session is initialized; can be used for various functions such as initializing variables. The save path and session name can come from the PHP initialization file or via the session_save_path() and session_name() functions. Should return TRUE on success, FALSE on error.
bool close()
Executed on shutdown of a session. Can be used to free memory or to destroy variables. Should return TRUE on success, FALSE on error.
mixed read(string session_ID)
Called whenever a session is started. If called with a session ID, the data associated with that session ID must be read and returned as a serialized string. If no session ID is passed, an empty string is returned. Should return FALSE on error.
bool write(string session_ID , string value )
Updates or adds new session data. The data to be written must be serialized. Should return TRUE on success, FALSE on error.
bool destroy(string session_ID )
Removes any session data from the data store. Must return TRUE on success, FALSE on error.
bool gc(string max_lifetime )
Called at session startup. Designed to remove any sessions with a lifetime greater than the maximum. Should return TRUE on success, FALSE on failure.
Version:
Existing since version 4.0
Example:
Create new session handler
session_set_save_handler("user_open","user_close","user_read","user_write", 
"user_destroy","user_gc")