Paste Code
Paste Blends
Paste Images
<?php

/* CookieCheck
*
* This script provides a function for checking whether or not a visitor
* accessing one of your PHP scripts has cookies enabled.
*
* This script is designed to be easily included as part of an existing PHP
* script. To use the script, simply include this file into your custom script
* and run the command cc_cookie_cutter(). The return value of this function is
* TRUE if cookies are enabled and FALSE if they are disabled. The script also
* sets the global value $CC_ERROR_MSG to something other than NULL if an error
* or warning was generated during the running of cc_cookie_cutter(). In the
* case where an error was generated, cc_cookie_cutter() will return FALSE. If a
* warning only was generated, cc_cookie_cutter() will return normally (i.e.
* TRUE if cookies are enabled and FALSE if cookies are disabled).
*
* All externally visible tokens are prefixed with 'cc_', 'CC_', 'cc-', 'CC-',
* '_cc_' or '_CC_'. Please be aware that if your script uses those prefixes,
* naming conflicts could potentially arise.
*
* This script sends headers as part of its logic. This means that this script
* needs to be included and the cc_cookie_cutter() function called before any
* output (including whitespace) is sent. This may necessitate the use of PHP's
* output control functions (ob_start, ob_flush, ob_end_flush, etc).
*
*
* EXAMPLE USAGE:
* ...
* include_once(cookiecheck.php);
* $cookies_enabled = cc_cookie_cutter();
* if (!$cookies_enabled) {
* // Code to display a warning that cookies are unavailable
* ...
* }
* // Execution only reaches this point if cookies are available
* ...
*
*
* Written by Jath Palasubramaniam
*
* Copyright 2008 Laden Donkey Studios. All rights reserved.
*
* This software is released under the terms of the MIT open source licence as
* specified below:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/





/* SETTINGS - May need modification */

// CC_SESSION_PATH - directory to store temporary session data
define('CC_SESSION_PATH', $_SERVER['DOCUMENT_ROOT'] . '/../tmp/cc_sessions');

// CC_COOKIE_LIFE_DAYS - how many days a test is valid for; 0 for session only
define('CC_COOKIE_LIFE_DAYS', (7));

// CC_COOKIE_PATH - path on your site that the test is valid for; / means all
define('CC_COOKIE_PATH', '/');

// CC_PROTOCOL - protocol for client-server communication; http, https, etc
define('CC_PROTOCOL', 'http');

// CC_SESSION_ID_STEM - prefix to use with session ids for this script
define('CC_SESSION_ID_STEM', 'cc-sessid-');





/* SETTINGS - Unlikely to need modification */

// CC_QUERY - variable name used in the GET query string for this script
define('CC_QUERY', 'cc_status');

// CC_COOKIE - name of the test cookie sent by this script
define('CC_COOKIE', 'cc_test');

// CC_SESSION_NAME - name of sessions used by this script
define('CC_SESSION_NAME', 'cc_session');

// CC_SESSION_TIMEOUT - time in seconds before the test is aborted
define('CC_SESSION_TIMEOUT', (60));





/* GLOBALS - Accessible after this page has been included */
// $CC_ERROR_MSG - will be modified by cc_cookie_cutter() if an error occurs
$CC_ERROR_MSG = NULL;





/* FUNCTIONS - Public */

// cc_cookie_cutter - run a test to see whether cookies are enabled or not
// Takes no arguments
// Returns TRUE if cookies are enabled, FALSE if they are disabled
function cc_cookie_cutter() {

// Declare $CC_ERROR_MSG to refer to the global variable
global $CC_ERROR_MSG;

if (isset($_COOKIE[CC_COOKIE])) {

// Cookies are enabled
if (isset($_GET[CC_QUERY])) {
// Reload the page using the initial query string
if (!_cc_initialise_session_settings()) {
$CC_ERROR_MSG =
'CookieCheck Error: Unable to initialise session settings';
return FALSE;
}
session_id(CC_SESSION_ID_STEM . $_GET[CC_QUERY]);
session_start();

// Get the initial query string and prepare it for appending
$qstring = $_SESSION['_SERVER']['QUERY_STRING'];
$qstring = ($qstring == '' ? '': '?') . $qstring;
// Get needed $_SERVER variables
$http_host = $_SESSION['_SERVER']['HTTP_HOST'];
$php_self = $_SESSION['_SERVER']['PHP_SELF'];
session_write_close();
// Reload the page, the session id will be propogated in the cookie
header('Location: ' . CC_PROTOCOL . '://' . $http_host . $php_self .
$qstring);
exit();
// Do not continue; rather exit and reload the page
}
// Restore any globals that are saved
$old_session_settings = _cc_save_session_settings();
if (!_cc_initialise_session_settings()) {
$CC_ERROR_MSG =
'CookieCheck Error: Unable to initialise session settings';
return FALSE;
}
session_id(CC_SESSION_ID_STEM . strval($_COOKIE[CC_COOKIE]));
session_start();
_cc_restore_globals();
session_destroy();
if (!_cc_restore_session_settings($old_session_settings)) {
$CC_ERROR_MSG =
'CookieCheck Warning: Unable to restore session settings';
}

return TRUE;

} else {

// Cookies are either disabled or not yet tested for
if (isset($_GET[CC_QUERY])) {
// Restore globals as they were previously sent
$old_session_settings = _cc_save_session_settings();
if (!_cc_initialise_session_settings()) {
$CC_ERROR_MSG =
'CookieCheck Error: Unable to initialise session settings';
return FALSE;
}
session_id(CC_SESSION_ID_STEM . strval($_GET[CC_QUERY]));
session_start();
_cc_restore_globals();
session_destroy();
if (!_cc_restore_session_settings($old_session_settings)) {
$CC_ERROR_MSG =
'CookieCheck Warning: Unable to restore session settings';
}
// Continue on and return FALSE as cookies are disabled
} else {
// Save globals as we are going to reload this page
if (!_cc_initialise_session_settings()) {
$CC_ERROR_MSG =
'CookieCheck Error: Unable to initialise session settings';
return FALSE;
}
$session_id = strval(mt_rand());
session_id(CC_SESSION_ID_STEM . $session_id);
session_start();
_cc_save_globals();
session_write_close();

// Append the session id to the end of the existing query string
$qstring = $_SERVER['QUERY_STRING'] .
($_SERVER['QUERY_STRING'] == '' ? '' : '&') . CC_QUERY . '=' .
$session_id;
// Send a test cookie
setcookie(CC_COOKIE, $session_id,
(time() + CC_COOKIE_LIFE_DAYS * 24 * 60 * 60), CC_COOKIE_PATH);
header('Location: ' . CC_PROTOCOL . '://' . $_SERVER['HTTP_HOST'] .
$_SERVER['PHP_SELF'] . '?' . $qstring);
exit();
// Do not continue; rather exit and reload the page
}

return FALSE;

}

}





/* FUNCTIONS - Private */

// _cc_initialise_session_settings - configure the session settings
// Takes no arguments
// Returns TRUE on success, FALSE on failure
function _cc_initialise_session_settings() {

// Initialise the session save path
$old_session_save_path = session_save_path();
session_save_path(session_save_path() . CC_SESSION_PATH);
$session_save_path = session_save_path();
if (!file_exists($session_save_path)) {
if (!mkdir($session_save_path, 0755, TRUE)) {
session_save_path($old_session_save_path);
return FALSE;
}
} else {
if (!(is_dir($session_save_path) && is_readable($session_save_path) &&
is_writeable($session_save_path))) {
session_save_path($old_session_save_path);
return FALSE;
}
}

// Initialise session garbage collection
ini_set('session.gc_maxlifetime', CC_SESSION_TIMEOUT);
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 1);

// Initialise the use of cookies for sessions
ini_set('session.use_cookies', 0);

// Initialise the session name
session_name(CC_SESSION_NAME);

return TRUE;

}


// _cc_save_session_settings - save the default/custom session settings
// Takes no arguments
// Returns an array containing the old settings
function _cc_save_session_settings() {

// Save the session save path
$old_session_settings['save_path'] = session_save_path();

// Save the session garbage collection
$old_session_settings['gc_maxlifetime'] = ini_get('session.gc_maxlifetime');
$old_session_settings['gc_probability'] = ini_get('session.gc_probability');
$old_session_settings['gc_divisor'] = ini_get('session.gc_divisor');

// Save the user of cookies for sessions
$old_session_settings['use_cookies'] = ini_get('session.use_cookies');

// Save the session name
$old_session_settings['name'] = session_name();

return $old_session_settings;

}


// _cc_restore_session_settings - restore the default/custom session settings
// Takes an array containing the old settings
// Returns TRUE on success, FALSE on error
function _cc_restore_session_settings($old_session_settings) {

// Check that argument is valid
if (!is_array($old_session_settings)) {
return FALSE;
}

// Restore the session save path
session_save_path($old_session_settings['save_path']);

// Restore session garbage collection
ini_set('session.gc_maxlifetime', $old_session_settings['gc_maxlifetime']);
ini_set('session.gc_probability', $old_session_settings['gc_probability']);
ini_set('session.gc_divisor', $old_session_settings['gc_divisor']);

// Restore the use of cookies for sessions
ini_set('session.use_cookies', $old_session_settings['use_cookies']);

// Restore the session name
session_name($old_session_settings['name']);

return TRUE;

}


// _cc_save_globals - saves the values of the PHP global arrays
// Takes no arguments
// Returns no value
function _cc_save_globals() {

// Serialize and save each of the super globals
// $GLOBALS is not saved in this manner as it seems to dynamically reference
// each of the other super globals anyway
// Explicitly saving or restoring $GLOBALS causes some sort of error, though
// I can't figure out exactly what it is
// Suffice to say, $GLOBALS works as expected without explicitly doing
// anything to save or restore it (I think)
$_SESSION['_FILES'] = $_FILES;
$_SESSION['_SERVER'] = $_SERVER;
$_SESSION['_ENV'] = $_ENV;
$_SESSION['_COOKIE'] = $_COOKIE;
$_SESSION['_GET'] = $_GET;
$_SESSION['_POST'] = $_POST;
$_SESSION['_REQUEST'] = $_REQUEST;
// Set a flag to signify that we are storing the globals to a fresh session
$_SESSION['cc_flag'] = TRUE;

return;

}


// _cc_restore_globals - restores the values of the PHP global arrays
// Takes no arguments
// Returns no value
function _cc_restore_globals() {

// Only restore the globals if we have freshly saved them
if (isset($_SESSION['cc_flag'])) {
// Unserialize and restore the super globals removing them from the
// session variable once done
// $GLOBALS is not restored in this manner as it seems to dynamically
// reference each of the other super globals anyway
// Explicitly saving or restoring $GLOBALS causes some sort of error,
// though I can't figure out exactly what it is
// Suffice to say, $GLOBALS works as expected without explicitly doing
// anything to save or restore it (I think)
$_FILES = $_SESSION['_FILES'];
unset($_SESSION['_FILES']);
$_SERVER = $_SESSION['_SERVER'];
unset($_SESSION['_SERVER']);
$_ENV = $_SESSION['_ENV'];
unset($_SESSION['_ENV']);
$_COOKIE = $_SESSION['_COOKIE'];
unset($_SESSION['_COOKIE']);
$_GET = $_SESSION['_GET'];
unset($_SESSION['_GET']);
$_POST = $_SESSION['_POST'];
unset($_SESSION['_POST']);
$_REQUEST = $_SESSION['_REQUEST'];
unset($_SESSION['_REQUEST']);
// Remove the flag that identified the session information as fresh
unset($_SESSION['cc_flag']);
}

return;

}


// No trailing whitespace after the PHP close tag to avoid sending whitespace
?>
  1. <?php
  2.  
  3. /* CookieCheck
  4.  *
  5.  * This script provides a function for checking whether or not a visitor
  6.  * accessing one of your PHP scripts has cookies enabled.
  7.  *
  8.  * This script is designed to be easily included as part of an existing PHP
  9.  * script. To use the script, simply include this file into your custom script
  10.  * and run the command cc_cookie_cutter(). The return value of this function is
  11.  * TRUE if cookies are enabled and FALSE if they are disabled. The script also
  12.  * sets the global value $CC_ERROR_MSG to something other than NULL if an error
  13.  * or warning was generated during the running of cc_cookie_cutter(). In the
  14.  * case where an error was generated, cc_cookie_cutter() will return FALSE. If a
  15.  * warning only was generated, cc_cookie_cutter() will return normally (i.e.
  16.  * TRUE if cookies are enabled and FALSE if cookies are disabled).
  17.  *
  18.  * All externally visible tokens are prefixed with 'cc_', 'CC_', 'cc-', 'CC-',
  19.  * '_cc_' or '_CC_'. Please be aware that if your script uses those prefixes,
  20.  * naming conflicts could potentially arise.
  21.  *
  22.  * This script sends headers as part of its logic. This means that this script
  23.  * needs to be included and the cc_cookie_cutter() function called before any
  24.  * output (including whitespace) is sent. This may necessitate the use of PHP's
  25.  * output control functions (ob_start, ob_flush, ob_end_flush, etc).
  26.  *
  27.  *
  28.  * EXAMPLE USAGE:
  29.  *  ...
  30.  *  include_once(cookiecheck.php);
  31.  *  $cookies_enabled = cc_cookie_cutter();
  32.  *  if (!$cookies_enabled) {
  33.  *      // Code to display a warning that cookies are unavailable
  34.  *      ...
  35.  *  }
  36.  *  // Execution only reaches this point if cookies are available
  37.  *  ...
  38.  *
  39.  *
  40.  * Written by Jath Palasubramaniam
  41.  *
  42.  * Copyright 2008 Laden Donkey Studios. All rights reserved.
  43.  *
  44.  * This software is released under the terms of the MIT open source licence as
  45.  * specified below:
  46.  *
  47.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  48.  * of this software and associated documentation files (the "Software"), to deal
  49.  * in the Software without restriction, including without limitation the rights
  50.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  51.  * copies of the Software, and to permit persons to whom the Software is
  52.  * furnished to do so, subject to the following conditions:
  53.  *
  54.  * The above copyright notice and this permission notice shall be included in
  55.  * all copies or substantial portions of the Software.
  56.  *
  57.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  58.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  59.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  60.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  61.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  62.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  63.  * THE SOFTWARE.
  64.  *
  65.  */
  66.  
  67.  
  68.  
  69.  
  70.  
  71. /* SETTINGS - May need modification */
  72.  
  73. // CC_SESSION_PATH - directory to store temporary session data
  74. define('CC_SESSION_PATH', $_SERVER['DOCUMENT_ROOT'] . '/../tmp/cc_sessions');
  75.  
  76. // CC_COOKIE_LIFE_DAYS - how many days a test is valid for; 0 for session only
  77. define('CC_COOKIE_LIFE_DAYS', (7));
  78.  
  79. // CC_COOKIE_PATH - path on your site that the test is valid for; / means all
  80. define('CC_COOKIE_PATH', '/');
  81.  
  82. // CC_PROTOCOL - protocol for client-server communication; http, https, etc
  83. define('CC_PROTOCOL', 'http');
  84.  
  85. // CC_SESSION_ID_STEM - prefix to use with session ids for this script
  86. define('CC_SESSION_ID_STEM', 'cc-sessid-');
  87.  
  88.  
  89.  
  90.  
  91.  
  92. /* SETTINGS - Unlikely to need modification */
  93.  
  94. // CC_QUERY - variable name used in the GET query string for this script
  95. define('CC_QUERY', 'cc_status');
  96.  
  97. // CC_COOKIE - name of the test cookie sent by this script
  98. define('CC_COOKIE', 'cc_test');
  99.  
  100. // CC_SESSION_NAME - name of sessions used by this script
  101. define('CC_SESSION_NAME', 'cc_session');
  102.  
  103. // CC_SESSION_TIMEOUT - time in seconds before the test is aborted
  104. define('CC_SESSION_TIMEOUT', (60));
  105.  
  106.  
  107.  
  108.  
  109.  
  110. /* GLOBALS - Accessible after this page has been included */
  111. // $CC_ERROR_MSG - will be modified by cc_cookie_cutter() if an error occurs
  112. $CC_ERROR_MSG = NULL;
  113.  
  114.  
  115.  
  116.  
  117.  
  118. /* FUNCTIONS - Public */
  119.  
  120. // cc_cookie_cutter - run a test to see whether cookies are enabled or not
  121. //      Takes no arguments
  122. //  Returns TRUE if cookies are enabled, FALSE if they are disabled
  123. function cc_cookie_cutter() {
  124.  
  125.         // Declare $CC_ERROR_MSG to refer to the global variable
  126.         global $CC_ERROR_MSG;
  127.  
  128.         if (isset($_COOKIE[CC_COOKIE])) {
  129.  
  130.                 // Cookies are enabled
  131.                 if (isset($_GET[CC_QUERY])) {
  132.                         // Reload the page using the initial query string
  133.                         if (!_cc_initialise_session_settings()) {
  134.                                 $CC_ERROR_MSG =
  135.                                         'CookieCheck Error: Unable to initialise session settings';
  136.                                 return FALSE;
  137.                         }
  138.                         session_id(CC_SESSION_ID_STEM . $_GET[CC_QUERY]);
  139.                         session_start();
  140.  
  141.                         // Get the initial query string and prepare it for appending
  142.                         $qstring = $_SESSION['_SERVER']['QUERY_STRING'];
  143.                         $qstring = ($qstring == '' ? '': '?') . $qstring;
  144.                         // Get needed $_SERVER variables
  145.                         $http_host = $_SESSION['_SERVER']['HTTP_HOST'];
  146.                         $php_self = $_SESSION['_SERVER']['PHP_SELF'];
  147.                         session_write_close();
  148.                         // Reload the page, the session id will be propogated in the cookie
  149.                         header('Location: ' . CC_PROTOCOL . '://' . $http_host . $php_self .
  150.                                 $qstring);
  151.                         exit();
  152.                         // Do not continue; rather exit and reload the page
  153.                 }
  154.                 // Restore any globals that are saved
  155.                 $old_session_settings = _cc_save_session_settings();
  156.                 if (!_cc_initialise_session_settings()) {
  157.                         $CC_ERROR_MSG =
  158.                                 'CookieCheck Error: Unable to initialise session settings';
  159.                         return FALSE;
  160.                 }
  161.                 session_id(CC_SESSION_ID_STEM . strval($_COOKIE[CC_COOKIE]));
  162.                 session_start();
  163.                 _cc_restore_globals();
  164.                 session_destroy();
  165.                 if (!_cc_restore_session_settings($old_session_settings)) {
  166.                         $CC_ERROR_MSG =
  167.                                 'CookieCheck Warning: Unable to restore session settings';
  168.                 }
  169.  
  170.                 return TRUE;
  171.  
  172.         } else {
  173.  
  174.                 // Cookies are either disabled or not yet tested for
  175.                 if (isset($_GET[CC_QUERY])) {
  176.                         // Restore globals as they were previously sent
  177.                         $old_session_settings = _cc_save_session_settings();
  178.                         if (!_cc_initialise_session_settings()) {
  179.                                 $CC_ERROR_MSG =
  180.                                         'CookieCheck Error: Unable to initialise session settings';
  181.                                 return FALSE;
  182.                         }
  183.                         session_id(CC_SESSION_ID_STEM . strval($_GET[CC_QUERY]));
  184.                         session_start();
  185.                         _cc_restore_globals();
  186.                         session_destroy();
  187.                         if (!_cc_restore_session_settings($old_session_settings)) {
  188.                                 $CC_ERROR_MSG =
  189.                                         'CookieCheck Warning: Unable to restore session settings';
  190.                         }
  191.                         // Continue on and return FALSE as cookies are disabled
  192.                 } else {
  193.                         // Save globals as we are going to reload this page
  194.                         if (!_cc_initialise_session_settings()) {
  195.                                 $CC_ERROR_MSG =
  196.                                         'CookieCheck Error: Unable to initialise session settings';
  197.                                 return FALSE;
  198.                         }
  199.                         $session_id = strval(mt_rand());
  200.                         session_id(CC_SESSION_ID_STEM . $session_id);
  201.                         session_start();
  202.                         _cc_save_globals();
  203.                         session_write_close();
  204.  
  205.                         // Append the session id to the end of the existing query string
  206.                         $qstring = $_SERVER['QUERY_STRING'] .
  207.                                 ($_SERVER['QUERY_STRING'] == '' ? '' : '&') . CC_QUERY . '=' .
  208.                                 $session_id;
  209.                         // Send a test cookie
  210.                         setcookie(CC_COOKIE, $session_id,
  211.                                 (time() + CC_COOKIE_LIFE_DAYS * 24 * 60 * 60), CC_COOKIE_PATH);
  212.                         header('Location: ' . CC_PROTOCOL . '://' . $_SERVER['HTTP_HOST'] .
  213.                                 $_SERVER['PHP_SELF'] . '?' . $qstring);
  214.                         exit();
  215.                         // Do not continue; rather exit and reload the page
  216.                 }
  217.                
  218.                 return FALSE;
  219.  
  220.         }
  221.  
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. /* FUNCTIONS - Private */
  229.  
  230. // _cc_initialise_session_settings - configure the session settings
  231. //      Takes no arguments
  232. //  Returns TRUE on success, FALSE on failure
  233. function _cc_initialise_session_settings() {
  234.  
  235.         // Initialise the session save path
  236.         $old_session_save_path = session_save_path();
  237.         session_save_path(session_save_path() . CC_SESSION_PATH);
  238.         $session_save_path = session_save_path();
  239.         if (!file_exists($session_save_path)) {
  240.                 if (!mkdir($session_save_path, 0755, TRUE)) {
  241.                         session_save_path($old_session_save_path);
  242.                         return FALSE;
  243.                 }
  244.         } else {
  245.                 if (!(is_dir($session_save_path) && is_readable($session_save_path) &&
  246.                         is_writeable($session_save_path))) {
  247.                         session_save_path($old_session_save_path);
  248.                         return FALSE;
  249.                 }
  250.         }
  251.  
  252.         // Initialise session garbage collection
  253.         ini_set('session.gc_maxlifetime', CC_SESSION_TIMEOUT);
  254.         ini_set('session.gc_probability', 1);
  255.         ini_set('session.gc_divisor', 1);
  256.  
  257.         // Initialise the use of cookies for sessions
  258.         ini_set('session.use_cookies', 0);
  259.  
  260.         // Initialise the session name
  261.         session_name(CC_SESSION_NAME);
  262.  
  263.         return TRUE;
  264.  
  265. }
  266.  
  267.  
  268. // _cc_save_session_settings - save the default/custom session settings
  269. //      Takes no arguments
  270. //  Returns an array containing the old settings
  271. function _cc_save_session_settings() {
  272.  
  273.         // Save the session save path
  274.         $old_session_settings['save_path'] = session_save_path();
  275.  
  276.         // Save the session garbage collection
  277.         $old_session_settings['gc_maxlifetime'] = ini_get('session.gc_maxlifetime');
  278.         $old_session_settings['gc_probability'] = ini_get('session.gc_probability');
  279.         $old_session_settings['gc_divisor'] = ini_get('session.gc_divisor');
  280.  
  281.         // Save the user of cookies for sessions
  282.         $old_session_settings['use_cookies'] = ini_get('session.use_cookies');
  283.  
  284.         // Save the session name
  285.         $old_session_settings['name'] = session_name();
  286.  
  287.         return $old_session_settings;
  288.  
  289. }
  290.        
  291.  
  292. // _cc_restore_session_settings - restore the default/custom session settings
  293. //      Takes an array containing the old settings
  294. //  Returns TRUE on success, FALSE on error
  295. function _cc_restore_session_settings($old_session_settings) {
  296.  
  297.         // Check that argument is valid
  298.         if (!is_array($old_session_settings)) {
  299.                 return FALSE;
  300.         }
  301.  
  302.         // Restore the session save path
  303.         session_save_path($old_session_settings['save_path']);
  304.  
  305.         // Restore session garbage collection
  306.         ini_set('session.gc_maxlifetime', $old_session_settings['gc_maxlifetime']);
  307.         ini_set('session.gc_probability', $old_session_settings['gc_probability']);
  308.         ini_set('session.gc_divisor', $old_session_settings['gc_divisor']);
  309.  
  310.         // Restore the use of cookies for sessions
  311.         ini_set('session.use_cookies', $old_session_settings['use_cookies']);
  312.  
  313.         // Restore the session name
  314.         session_name($old_session_settings['name']);
  315.  
  316.         return TRUE;
  317.  
  318. }
  319.  
  320.  
  321. // _cc_save_globals - saves the values of the PHP global arrays
  322. //      Takes no arguments
  323. //  Returns no value
  324. function _cc_save_globals() {
  325.  
  326.         // Serialize and save each of the super globals
  327.         // $GLOBALS is not saved in this manner as it seems to dynamically reference
  328.         //  each of the other super globals anyway
  329.         // Explicitly saving or restoring $GLOBALS causes some sort of error, though
  330.         //  I can't figure out exactly what it is
  331.         // Suffice to say, $GLOBALS works as expected without explicitly doing
  332.         //  anything to save or restore it (I think)
  333.         $_SESSION['_FILES'] = $_FILES;
  334.         $_SESSION['_SERVER'] = $_SERVER;
  335.         $_SESSION['_ENV'] = $_ENV;
  336.         $_SESSION['_COOKIE'] = $_COOKIE;
  337.         $_SESSION['_GET'] = $_GET;
  338.         $_SESSION['_POST'] = $_POST;
  339.         $_SESSION['_REQUEST'] = $_REQUEST;
  340.         // Set a flag to signify that we are storing the globals to a fresh session
  341.         $_SESSION['cc_flag'] = TRUE;
  342.  
  343.         return;
  344.  
  345. }
  346.  
  347.  
  348. // _cc_restore_globals - restores the values of the PHP global arrays
  349. //      Takes no arguments
  350. //  Returns no value
  351. function _cc_restore_globals() {
  352.  
  353.         // Only restore the globals if we have freshly saved them
  354.         if (isset($_SESSION['cc_flag'])) {
  355.                 // Unserialize and restore the super globals removing them from the
  356.                 //  session variable once done
  357.                 // $GLOBALS is not restored in this manner as it seems to dynamically
  358.                 //  reference each of the other super globals anyway
  359.                 // Explicitly saving or restoring $GLOBALS causes some sort of error,
  360.                 //  though I can't figure out exactly what it is
  361.                 // Suffice to say, $GLOBALS works as expected without explicitly doing
  362.                 //  anything to save or restore it (I think)
  363.                 $_FILES = $_SESSION['_FILES'];
  364.                 unset($_SESSION['_FILES']);
  365.                 $_SERVER = $_SESSION['_SERVER'];
  366.                 unset($_SESSION['_SERVER']);
  367.                 $_ENV = $_SESSION['_ENV'];
  368.                 unset($_SESSION['_ENV']);
  369.                 $_COOKIE = $_SESSION['_COOKIE'];
  370.                 unset($_SESSION['_COOKIE']);
  371.                 $_GET = $_SESSION['_GET'];
  372.                 unset($_SESSION['_GET']);
  373.                 $_POST = $_SESSION['_POST'];
  374.                 unset($_SESSION['_POST']);
  375.                 $_REQUEST = $_SESSION['_REQUEST'];
  376.                 unset($_SESSION['_REQUEST']);
  377.                 // Remove the flag that identified the session information as fresh
  378.                 unset($_SESSION['cc_flag']);
  379.         }
  380.  
  381.         return;
  382.  
  383. }
  384.  
  385.  
  386. // No trailing whitespace after the PHP close tag to avoid sending whitespace
  387. ?>
go to heaven