ho creato una struttura ad oggetti che mi permette di fare accessi con il login...
l'unico difetto è che fintanto che sto nella pagina del login, la sessione la tiene... se vado a navigare nelle altre pagine, la sessione non me la trova più...
è come se si cancellasse...
vi posto il codice di home e login
Codice PHP:
<?php
if(function_exists('ob_gzhandler')){
ob_start('ob_gzhandler');
}
class Index
{
//<!--inclusioni--!>
//<!--do i valori alle classi--!>
var $p;
var $smarty="";
var $config="";
var $sql="";
function Index(){
error_reporting(E_ALL);
include_once 'config.php';
$this->config=new config;
include_once $this->config->sitedir.'/includes/php/libs/Smarty.class.php';
include_once $this->config->sitedir.'/error.php';
$this->smarty=new Smarty;
$this->sql=new Sql($this->config->dbname,$this->config->pass,$this->config->user,$this->config->host);
$this->smarty->template_dir=$this->config->themedir;
$this->smarty->compile_dir=$this->config->themedir.'/templates_c';
$this->smarty->config_dir=$this->config->themedir.'/config';
$this->smarty->compile_check = true;
$this->smarty->debugging = false;
}
function path(){
$separators=array("/","=") ;
$collection=!isset($_SERVER["PATH_INFO"]) ? "/page=main/ahah=false" : $_SERVER["PATH_INFO"] ;
/* Togliamo il primo slash da PATH_INFO */
$collection=substr($collection,1) ;
/*
Nel caso in cui l'ultima variabile resti vuota e si abbia una PATH_INFO di questo tipo,
pagina.php/var1=val1/var2=val2/var3=/, l'ultimo separatore crea errori quindi va tolto
*/
if(substr($collection,-1)==$separators[0]){
$collection=substr($collection,0,-1) ;
}
/*
Riduciamo i separatori nell'URL ad uno soltanto (gli "=" erano stati utilizzati solo per somiglianza con i normali URL dinamici (pagina.php?var=val): poi utilizzeremo un explode() per separare le variabili
*/
$collection = str_replace($separators[1], $separators[0], $collection);
/*
Ecco l'explode
*/
$varArray=explode($separators[0],$collection) ;
$_PINFO=array() ;
foreach ($varArray as $key=>$value){
if(!($key%2)){
$_PINFO["$value"]=$varArray[$key+1] ;
}
}//END FOREACH
$_GET = $_PINFO;
}
function view(){
$pa=$this->path();
$this->p = $_GET['page'];
include_once($this->config->sitedir.'/page/'.$this->p.'.php');
$class='page_'.$this->p;
$var=array();
$var['themedir'] = $this->config->themedir;
$var['basedir'] = $this->config->sitedir;
$var['title'] = $this->config->title;
$var['forumurl'] = $this->config->forumurl;
$var['siteurl'] = $this->config->siteurl;
$var['p'] = $this->p;
$page=new $class();
$page->variable = $var;
$page->page();
}
}
$index=new Index();
$index->view();
?>
Codice PHP:
<?php
class page_login extends Index
{
var $variable = array();
function page(){
require_once($this->config->sitedir.'/includes/php/captcha.class.php');
$captcha=new captcha();
$act=isset($_GET['act']) ? $_GET['act'] : '';
if(!isset($_COOKIE['log'])&&$_GET['action']=='register'){
$stringa=$captcha->stringa_casuale();
$this->smarty->assign("stringa", $stringa);
if(isset($_GET['stringa'])){
$captcha->offuscamento=5;
$captcha->genera($_GET['stringa']);
}
$_SESSION['stringa']=$stringa;
if(isset($_GET['action'])&&$_GET['action']=='register'&&isset($_POST['username'])){
$q=$this->sql->query("SELECT username FROM ".$this->config->prefix."user ORDER BY id DESC LIMIT 0,30",__LINE__,__FILE__);
while($d=mysql_fetch_array($q)){
if($d['username']==$_POST['username']){
$dbu=1;
} else {
$dbu=0;
}
}
if($_POST['password']!=$_POST['confirm']){
$this->smarty->assign("message","le password non coincidono");
} elseif($dbu==1){
$this->smarty->assign("message","C'è gia un utente registrato con questo nome");
} elseif($_SESSION['stringa'] == $_POST['codice']){
$this->smarty->assign('message','Il codice non corrisponde all\'immagine');
} else {
$data = addslashes(fread(fopen($_FILES["image"]["tmp_name"], "rb"), $_FILES["image"]["size"]));
$this->sql->query('INSERT INTO `'.$this->config->prefix.'user` ( `id` , `username` , `password` , `e-mail` , `datereg` , `state`, `image`)
VALUES (
\'\', \''.$_POST['username'].'\', MD5( \''.$_POST['password'].'\' ) , \''.$_POST['email'].'\', UNIX_TIMESTAMP( NOW() ) , \'1\', \''.$data.'\'
);');
$this->smarty->assign("message",1);
}
}
} elseif(!isset($_COOKIE['log'])&&$_GET['action']=="login") {
if(isset($_GET['action'])&&$_GET['action']=='login'&&isset($_POST['username'])){
$q=$this->sql->query('SELECT * FROM '.$this->config->prefix.'user WHERE username = \''.$_POST['username'].'\' AND password = \''.md5($_POST['password']).'\' LIMIT 0,1',__FILE__,__LINE__);
if(!$q) $this->smarty->assign('message',"Nome utente, o password non riconosciuti... ritenta :-(");
setcookie('log',array('username'=>$_POST['username'],'password'=>md5($_POST['password'])),320000,'/','');
$this->smarty->assign('message',1);
}
}
foreach($this->variable as $name => $value){
$this->smarty->assign($name, $value);
}
$this->smarty->display($this->config->themedir.'/index.body.tpl');
return;
}
}
?>
che devo fare...
help please
PS: uso smarty template engine