Location: PHPKode > projects > PhpProjectMaster > www/inc/sess.php
<?php
/* 
 Miscellaneous Session functions
 (c) 2004-2007 by "Oleg Savchuk" <hide@address.com>

 part of phpProjectMaster project
 http://phpprojmaster.sourceforge.net

 The contents of this file are subject to the GNU GENERAL PUBLIC LICENSE
 http://www.gnu.org/copyleft/gpl.html
*/

 require_once "user.php";
 require_once dirname(__FILE__)."/site_value.php";

$PERM_COOKIE_NAME='ppm';
$PERM_COOKIE_DAYS=365;

function set_def_session($u_id){
  $hUSER=get_user($u_id);
  foreach($hUSER as $key => $value){
    $_SESSION[$key]=$value;
  }
#  $_SESSION=array_merge($hUSER,$_SESSION); # don't work in PHP5
  $_SESSION['access_name']=accesslevel2name($hUSER['access_level']);

 $_SESSION['external_wiki_link']=get_site_value('site.external_wiki_link');

  #set company info
#  my $hCOMP=get_company($c_id);
#  while( my ($k,$v)= each %$hCOMP ){
#     $SESSION{"company_$k"}=$v;
#  }

}

//*****************
function create_perm_cookie($u_id){
 global $PERM_COOKIE_NAME, $PERM_COOKIE_DAYS;

 $cookie_id=substr(get_rand_str(16).time(),0,32);

 db_query("replace into user_cookie (cookie_id, u_id, add_time) VALUES (".dbq($cookie_id).", ".dbq($u_id).", now() )");

 setcookie($PERM_COOKIE_NAME, $cookie_id, time()+60*60*24*$PERM_COOKIE_DAYS, "/");

# rwe("$PERM_COOKIE_NAME, $cookie_id, ".(time()+60*60*24*$PERM_COOKIE_DAYS));

 return $cookie_id;
}

//*****************
function check_permanent_login(){
 global $PERM_COOKIE_NAME, $PERM_COOKIE_DAYS;

 $cookie_id=$_COOKIE[ $PERM_COOKIE_NAME ];
#rw("cookies: $cookie_id");
#print_r($_COOKIE);
#exit;

 if ($cookie_id) {
    $u_id=db_value("select u_id 
          from user_cookie 
         where cookie_id=".dbq($cookie_id)." 
           and add_time>=FROM_DAYS(TO_DAYS(now())-$PERM_COOKIE_DAYS)
    ");

    if ($u_id>0){
       do_permanent_login($u_id);
    }else{ //cookie is not found in DB - clean it (so it will not put load on DB during next pages)
       setcookie($PERM_COOKIE_NAME, FALSE, -1, "/", ".ualife.org");
    }

 }

}

//*****************
function do_permanent_login($u_id){

 session_destroy();
 session_start();

 #fill up session data
 set_def_session($u_id);
 session_write_close();

 #update login vars
 $ip=getenv("REMOTE_ADDR");

 $sql="update users 
       set login_time=now(),
           login_ip=".dbq($ip)."
      where u_id=$u_id";
# logger($sql);
 db_query($sql);

}

//*************
function remove_perm_cookie(){
 global $PERM_COOKIE_NAME, $PERM_COOKIE_DAYS;

 $cookie_id=$_COOKIE[ $PERM_COOKIE_NAME ];

 setcookie($PERM_COOKIE_NAME, FALSE, -1, "/");

 #cleanup in DB (user's cookie and ALL old cookies)
 db_query("delete from user_cookie
    where cookie_id=".dbq($cookie_id)."
       or add_time<FROM_DAYS(TO_DAYS(now())-$PERM_COOKIE_DAYS)
 ");

}

?>
Return current item: PhpProjectMaster