HTTP_Session2
[ class tree: HTTP_Session2 ] [ index: HTTP_Session2 ] [ all elements ]

Source for file HTTP_Session2_Example.php

Documentation is available at HTTP_Session2_Example.php

  1. <?php
  2. //
  3. // +-----------------------------------------------------------------------+
  4. // | Copyright (c) 2002, Alexander Radivanovich                            |
  5. // | All rights reserved.                                                  |
  6. // |                                                                       |
  7. // | Redistribution and use in source and binary forms, with or without    |
  8. // | modification, are permitted provided that the following conditions    |
  9. // | are met:                                                              |
  10. // |                                                                       |
  11. // | o Redistributions of source code must retain the above copyright      |
  12. // |   notice, this list of conditions and the following disclaimer.       |
  13. // | o Redistributions in binary form must reproduce the above copyright   |
  14. // |   notice, this list of conditions and the following disclaimer in the |
  15. // |   documentation and/or other materials provided with the distribution.|
  16. // | o The names of the authors may not be used to endorse or promote      |
  17. // |   products derived from this software without specific prior written  |
  18. // |   permission.                                                         |
  19. // |                                                                       |
  20. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
  21. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
  22. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  23. // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
  24. // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  25. // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
  26. // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  27. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  28. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
  29. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  30. // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
  31. // |                                                                       |
  32. // +-----------------------------------------------------------------------+
  33. // | Author: Alexander Radivanovich <info@wwwlab.net>                      |
  34. // +-----------------------------------------------------------------------+
  35. //
  36.  
  37. //ob_start(); //-- For easy debugging --//
  38.  
  39. require_once ("PEAR.php");
  40. require_once ("HTTP/Session.php");
  41.  
  42. ini_set('error_reporting'E_ALL);
  43. ini_set('display_errors'1);
  44. //PEAR::setErrorHandling(PEAR_ERROR_DIE);
  45.  
  46. //HTTP_Session::setContainer('DB', array('dsn' => 'mysql://root@localhost/database', 'table' => 'sessiondata'));
  47. HTTP_Session::useCookies(true);
  48. HTTP_Session::start('SessionID'uniqid('MyID'));
  49.  
  50. ?>
  51. <html>
  52. <head>
  53. <style>
  54. body, td {
  55.     font-family: Verdana, Arial, sans-serif;
  56.     font-size: 11px;
  57. }
  58. A:link { color:#003399; text-decoration: none; }
  59. A:visited { color:#6699CC; text-decoration: none; }
  60. A:hover { text-decoration: underline; }
  61. </style>
  62. <title>HTTP Session</title>
  63. </head>
  64. <body style="margin: 5px;">
  65. <?php
  66.  
  67. /*
  68. if (!isset($variable)) {
  69.     $variable = 0;
  70.     echo("The variable wasn't previously set<br>\n");
  71. } else {
  72.     $variable++;
  73.     echo("Yes, it was set already<br>\n");
  74. }
  75. */
  76.  
  77. switch (@$_GET['action']{
  78.     case 'setvariable':
  79.         HTTP_Session::set('variable''Test string');
  80.         //HTTP_Session::register('variable');
  81.         break;
  82.     case 'unsetvariable':
  83.         HTTP_Session::set('variable'null);
  84.         //HTTP_Session::unregister('variable');
  85.         break;
  86.     case 'clearsession':
  87.         HTTP_Session::clear();
  88.         break;
  89.     case 'destroysession':
  90.         HTTP_Session::destroy();
  91.         break;
  92. }
  93.  
  94. HTTP_Session::setExpire(60);
  95. HTTP_Session::setIdle(5);
  96.  
  97. //echo("session_is_registered('variable'): <b>'" . (session_is_registered('variable') ? "<span style='color: red;'>yes</span>" : "no") . "'</b><br>\n");
  98. //echo("isset(\$GLOBALS['variable']): <b>'" . (isset($GLOBALS['variable']) ? "<span style='color: red;'>yes</span>" : "no") . "'</b><br>\n");
  99.  
  100. echo("------------------------------------------------------------------<br>\n");
  101. echo("Session name: <b>'" . HTTP_Session::name("'</b><br>\n");
  102. echo("Session id: <b>'" . HTTP_Session::id("'</b><br>\n");
  103. echo("Is new session: <b>'" (HTTP_Session::isNew("<span style='color: red;'>yes</span>" "no""'</b><br>\n");
  104. echo("Is expired: <b>'" (HTTP_Session::isExpired("<span style='color: red;'>yes</span>" "no""'</b><br>\n");
  105. echo("Is idle: <b>'" (HTTP_Session::isIdle("<span style='color: red;'>yes</span>" "no""'</b><br>\n");
  106. //echo("Variable: <b>'" . HTTP_Session::get('variable') . "'</b><br>\n");
  107. echo("Session valid thru: <b>'" (HTTP_Session::sessionValidThru(time()) "'</b><br>\n");
  108. echo("------------------------------------------------------------------<br>\n");
  109.  
  110. if (HTTP_Session::isNew()) {
  111.     //HTTP_Session::set('var', 'value');
  112.     //HTTP_Session::setLocal('localvar', 'localvalue');
  113.     //blah blah blah
  114. }
  115.  
  116. ?>
  117. <div style="background-color: #F0F0F0; padding: 15px; margin: 5px;">
  118. <pre>
  119. $_SESSION:
  120. <?php
  121. var_dump($_SESSION);
  122. ?>
  123. </pre>
  124. </div>
  125. <?php
  126.  
  127. HTTP_Session::updateIdle();
  128.  
  129. ?>
  130. <p><a href="<?php echo $_SERVER['SCRIPT_NAME'?>?action=setvariable">Set variable</a></p>
  131. <p><a href="<?php echo $_SERVER['SCRIPT_NAME'?>?action=unsetvariable">Unset variable</a></p>
  132. <p><a href="<?php echo $_SERVER['SCRIPT_NAME'?>?action=destroysession">Destroy session</a></p>
  133. <p><a href="<?php echo $_SERVER['SCRIPT_NAME'?>?action=clearsession">Clear session data</a></p>
  134. <p><a href="<?php echo $_SERVER['SCRIPT_NAME'?>">Reload page</a></p>
  135. </body>
  136. </html>

Documentation generated on Mon, 11 Mar 2019 14:18:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.