直接上代码,好用不解释:
<?php /** osCommerce中关于session的三个好用方法 */ header("Content-type:text/html;charset=utf8"); //配置 if ( (PHP_VERSION >= 4.3) && ((bool)ini_get('register_globals') == false) ) { @ini_set('session.bug_compat_42', 1); @ini_set('session.bug_compat_warn', 0); } session_start(); //注册一个变量到session中去 function tep_session_register($variable) { if (isset($_SESSION)) { if (PHP_VERSION < 4.3) { return session_register($variable); } else { if (isset($GLOBALS[$variable])) { $_SESSION[$variable] =&$GLOBALS[$variable]; } else { $_SESSION[$variable] = null; } } } return false; } //检测某变量是否注册到session中 function tep_session_is_registered($variable) { if (PHP_VERSION < 4.3) { return session_is_registered($variable); } else { return isset($_SESSION) && array_key_exists($variable, $_SESSION); } } //注销一个session变量 function tep_session_unregister($variable) { if (PHP_VERSION < 4.3) { return session_unregister($variable); } else { unset($_SESSION[$variable]); } } $user=array( 'name'=>'jtk', 'age'=>18, 'groupid'=>3 ); tep_session_register('user'); echo "<hr/>"; if(tep_session_is_registered('user')){ echo "OK, user 已经存在session中!"; }else{ echo "Oops, user 不存在于session中"; } echo "<hr/>"; echo 'Name=='.$_SESSION['user']['name']."<br/>"; echo 'AGE=='.$_SESSION['user']['age']."<br/>"; echo 'GroupID=='.$_SESSION['user']['groupid']."<br/>"; $user['name']='Namejtk'; echo "<hr/>"; echo 'New Name=='.$_SESSION['user']['name']."<br/>"; ?>