tags:

views:

36

answers:

1

hi

a have written this code:

<?php
require("../../config.php");
require("../php/funct.php");

try {
    $pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_TABL.';', DB_AUSER, DB_APASS);
}
catch(PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}
$idee=unique_id();
$insystem=true;

include('session_check.php');

unset($insystem);
    print($sesja);  ///// SECOND PRINT()
if($sesja!=1) {
    die("Session error");
    exit;
} else {    
       //some other code
}

session_check.php is here:

<?php
if(isset($insystem) && $insystem) {
    if(!isset($_COOKIE['seid']))    {
        setcookie('seid', $idee, time() + COOKIELIFE);
        $sesja=0;
    } else {
        setcookie('seid', $_COOKIE['seid'], time() + COOKIELIFE);
        $dane=$pdo->prepare('SELECT s.id, s.ip, s.czas, s.prawa, p.nick, p.id FROM sessions s JOIN pracownicy p ON s.Pracownicy_id=p.id WHERE s.id=:id');
        $dane->bindValue(':id',$_COOKIE['seid'], PDO::PARAM_STR);
        $dane->execute();
        $dsesji = $dane -> fetch();
        $dane->closeCursor();
        unset($dane);
        if($dsesji!==false) {
            if(isset($_GET['lo']) && ($_GET['lo']==='lo') && isset($indeks) && $indeks) {
                $usun=$pdo->prepare('DELETE FROM sessions WHERE id=:id');
                $usun->bindValue(':id',$_COOKIE['seid'], PDO::PARAM_STR);
                $usun->execute();
                unset($usun);
                setcookie('seid', 'abc', time() - 42000);
                header("Location: index.php");
            }
            $sesja=1;
            $_nick=$dsesji['nick'];
            $_Pracownicy_id=$dsesji['id'];
            $_prawa=explode('|',$dsesji['prawa']);
            unset($_prawa[count($_prawa)-1]);
            if($dsesji['ip']!=$_SERVER['REMOTE_ADDR']) {
                $usun=$pdo->prepare('DELETE FROM sessions WHERE id=:id');
                $usun->bindValue(':id',$_COOKIE['seid'], PDO::PARAM_STR);
                $usun->execute();
                unset($usun);
                setcookie('seid', 'abc', time() - 42000);
                header("Location: index.php?lo=bs");
                exit;
            }
            $teraz=time();
            $roznica=$teraz-$dsesji['czas'];
            if($roznica>(TIMEOUT*60)) {
                $usun=$pdo->prepare('DELETE FROM sessions WHERE id=:id');
                $usun->bindValue(':id',$_COOKIE['seid'], PDO::PARAM_STR);
                $usun->execute();
                unset($usun);
                setcookie('seid', 'abc', time() - 42000);
                header("Location: index.php?lo=to");
                exit;
            }
            if($sesja!=0) {
                $idee=unique_id();
                setcookie('seid', $idee, time() + COOKIELIFE);
                $dane=$pdo->prepare('UPDATE sessions SET id=:nowyid WHERE id=:id');
                $dane->bindValue(':nowyid',$idee, PDO::PARAM_STR);
                $dane->bindValue(':id',$_COOKIE['seid'], PDO::PARAM_STR);
                $dane->execute();
                unset($dane);
                $_CURR_SID=$idee;
                unset($idee);
            }
            print($sesja);  ///// FIRST PRINT()
        } else {
            $sesja=0;

        }
    }
} else {
    die('aerr1');
}
?>

Problem is: 1st print (from session_check.php) prints out 1 - what is expected value, but the second print in main script prints out 0 what is strange for me because $sesja variable is NOT changed between these both print's.

What's wrong?

+2  A: 

This happens because of variable scope within an include. $sesja was first set within your included code and lives only in there. When code execution returns to the main PHP script, $sesja is out of scope and forgotten.

To solve, you need to set $sesja = 0; somewhere before the include. Then, the included code will inherit scope and modify the correct variable.

spoulson