Bonjour je vous explique brièvement mon problème.
J'ai un petit programme de news avec une classe connexion à part et je souhaite stocker une news en session mais impossible de garder la session d'une page à l'autre.
Ca ne sert à rien de garder une news en session mais c'est surtout pour que je comprenne pourquoi je ne peux pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
 
class DatabaseConnection
{
    private static $_instance = null;
    private $_host;
    private $_user;
    private $_password;
    private $_dbname;
    private $_handle;
 
    public function __construct()
 
    {
        $this->_host     = 'xxxxx';
        $this->_user     = 'xxxxx';
        $this->_password = 'xxxxx';
        $this->_dbname   = 'xxxxx';
        $this->_handle   = null;
 
        try {
           $this->_handle = new PDO("mysql:host=$this->_host;dbname=$this->_dbname", $this->_user, $this->_password);
            $this->_handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
           die('Connection failed or database cannot be selected : ' . $e->getMessage());
        }
    }
 
    public function __destruct()
    {
        //var_dump($this->_db); // Verification
       if (!is_null($this->_handle)) {
            $this->_handle = null;
       }
       //var_dump($this->_db); // Une derniere verification pour voir si l'objet a ete detruit
    }
 
    public static function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
 
    public function handle()
    {
        return $this->_handle;
    }
 
}
?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
abstract class Objet {
 
 protected $db;
 public $output;
 
 protected function __construct($dbInit = TRUE) {
 
  if ( $dbInit ) {
      $this->db = DatabaseConnection::getInstance()->handle();
  }
 
 }
 
 public function __destruct() {
  unset($this->output);
 }
 
}
 
?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
class News extends Objet {
 
 public function __construct() {
  parent::__construct();
 }
 
 // Ajoute une news
// Retourne TRUE/FALSE en fonction de l'insertion dans la BDD
 public function AddNews($titre, $content, $categorie) {
    unset($this->output);
   $stmt=$this->db->prepare("INSERT INTO news (titre, content, categorie, date) VALUES (:titre, :content, :categorie, NOW() )");
   $stmt->bindParam(':titre', $titre, PDO::PARAM_STR);
   $stmt->bindParam(':content', $content, PDO::PARAM_STR);
   $stmt->bindParam(':categorie', $categorie, PDO::PARAM_INT);
    $stmt->execute();
   if ( $stmt->rowCount() == 1 ) {
        $this->output = $this->db->lastInsertId();
        return true;
   } else {
       return false;
   }
 }
 
// Update une news
 public function UpdateNews($titre, $content, $categorie,$id) {
  unset($this->output);
  $stmt=$this->db->prepare("UPDATE news SET titre = :titre, content = :content, categorie = :categorie  WHERE id = :id");
  $stmt->bindParam(':titre', $titre, PDO::PARAM_STR);
  $stmt->bindParam(':content', $content, PDO::PARAM_STR);
  $stmt->bindParam(':categorie', $categorie, PDO::PARAM_INT);
  $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute(); 
 
   if ( $stmt->rowCount() == 1 ) {
        $this->output = $this->db->lastInsertId();
        return true;
   } else {
        return false;
   }
 }
 
 public function DeleteNews($id) {
  $stmt=$this->db->prepare('DELETE FROM news WHERE id = :id');
  $stmt->bindParam(':id', $id, PDO::PARAM_INT);
  $stmt->execute();
  return ( $stmt->rowCount() == 1 ) ? TRUE : FALSE;
 }
 
 public function CountNewsCategorie($categorie) {
  $stmt=$this->db->prepare('SELECT * FROM news WHERE categorie = :categorie');
  $stmt->bindParam(':categorie', $categorie, PDO::PARAM_STR);
  $stmt->execute();
  return $stmt->rowCount();
 }
 
 public function GetUniqueNews($id) {
  unset($this->output);
  $stmt=$this->db->prepare('SELECT
                                new.id, new.titre, new.categorie, new.content, DATE_FORMAT(new.date,"%d-%m-%Y") as date_news ,
                                cat.categorie as categorie_news
                                FROM news as new
                                LEFT JOIN categories AS cat ON new.categorie = cat.id
                                WHERE new.id = :id
                                GROUP BY new.id');
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute();
  $this->output[] = $stmt->fetch(PDO::FETCH_ASSOC);
  array_walk_recursive($this->output, 'walk_func_bis');
 }
 
 public function GetNews($droit, $start = 0, $limit = 30) {
  unset($this->output);
                 $stmt=$this->db->prepare('SELECT
                                            new.id, new.titre, new.content, new.categorie, DATE_FORMAT(new.date,"%d-%m-%Y") as date_news ,
                                            cat.categorie as categorie_news
                                            FROM news AS new
                                            LEFT JOIN categories AS cat ON new.categorie = cat.id
                                            WHERE cat.consultation like :consultation
                                            GROUP BY new.id
                                            ORDER by new.date DESC LIMIT :start, :limit'); 
  $stmt->bindParam(':start', $start, PDO::PARAM_INT);
  $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
  $consultation_final='%'.$droit.'%';
  $stmt->bindParam(':consultation', $consultation_final, PDO::PARAM_STR);
  $stmt->execute();
   while ( $data = $stmt->fetch(PDO::FETCH_ASSOC) ) {
           $this->output[] = $data;    
   }
   ($this->output)?array_walk_recursive($this->output, 'walk_func_bis'):0;
 }
 
 
 
 public function LastIDNews() {
  return ( $this->db->query('SELECT MAX(id)+1 FROM news')->fetchColumn() ) ;
 }
 
 public function CountNews($droit) {
  return ( $this->db->query('SELECT COUNT(new.id) FROM news AS new LEFT JOIN categories AS cat ON new.categorie = cat.id  WHERE cat.consultation like '.$this->db->quote('%'.$droit.'%').'')->fetchColumn() ) ;
 }
 
 public function PaginationNews($pagination,$fonction) {
 $nb=(int)($fonction/$pagination);
 ($fonction%$pagination>0)?$nb++:1;
 return ($nb-1);
 }
 
  public function TitleNews($id) {
  return ( $this->db->query('SELECT titre FROM news where id='.$this->db->quote($id).'')->fetchColumn() ) ;
 }
 
  public function GiveMeCat($categorie) {
 return ($this->db->query('SELECT id FROM categories WHERE lien = '.$this->db->quote($categorie).'')->rowCount())?$this->db->query('SELECT id FROM categories WHERE lien = '.$this->db->quote($categorie).'')->fetchColumn():0;
 }
 
// Fin d'objet
 public function __destruct() {
  unset($this->output);
 }
 
}
?>
page test 1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
function chargerClasse ($classe){ require $classe . '.class.php';}
spl_autoload_register ('chargerClasse');
session_start();
$news = new News;
$base= new DatabaseConnection;
 
include('../Acces/connexion.php');
include('../Php/fonction.php');
 
echo "<pre>";
var_dump($news);
echo "</pre>";
 
echo "<pre>";
var_dump($base);
echo "</pre>";
 
    (!isset($_GET['archive']))?$news->GetNews(4,0,5):$news->GetNews(4,($_GET['archive']*5),5);
 
    foreach ( $news->output as $data )
        {
    ?>
        <div class="bloc_central_65">
            <h2><img src="Image/puce_titre.gif" width="13" height="18" alt=""/> <?php echo $data["titre"]; ?> </h2>
            <div  class="left demi gauche">
                Catégorie : <span class="gras bleu"><?php echo $data["categorie_news"]; ?></span>
            </div>
            <div  class="right demi droite">
                le : <?php echo $data["date_news"]; ?>
            </div>
            <div class="clear"></div>
            <div class="contenu_edito">
            <?php echo $data["content"]; ?>
            </div>
        </div>   
    <?php
        }
$_SESSION['news']=$news;
$_SESSION['base']=$base;
 
echo "<pre>";
var_dump($_SESSION['news']);
echo "</pre>";
 
echo "<pre>";
var_dump($_SESSION['base']);
echo "</pre>";
resultat
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
object(News)#3 (2) {
  ["db:protected"]=>
  object(PDO)#5 (0) {
  }
  ["output"]=>
  NULL
}
object(DatabaseConnection)#6 (5) {
  ["_host:private"]=>
  string(19) "xxxxx"
  ["_user:private"]=>
  string(14) "xxxx"
  ["_password:private"]=>
  string(8) "xxxxxx"
  ["_dbname:private"]=>
  string(14) "xxxxxx"
  ["_handle:private"]=>
  object(PDO)#7 (0) {
  }
}
 Mise en place de l'édito
 
Cat�gorie : Ensemble service
le : 12-07-2013
Bonjour,
 
 
 
A partir d'aujourd'hui les informations inter-entreprise seront disponibles directement sur  l'édito.
 
 
 
___________
 
 
 
 
 
object(News)#3 (2) {
  ["db:protected"]=>
  object(PDO)#5 (0) {
  }
  ["output"]=>
  array(1) {
    [0]=>
    array(6) {
      ["id"]=>
      string(2) "16"
      ["titre"]=>
      string(25) "Mise en place de l'édito"
      ["content"]=>
      string(287) "
Bonjour,
 
 
 
 
 
 
 
A partir d'aujourd'hui les informations inter-entreprise seront disponibles directement sur  l'édito.
 
 
 
 
 
 
 
___________
 
 
 
 
 
 
 
 
 
 
"
      ["categorie"]=>
      string(1) "5"
      ["date_news"]=>
      string(10) "12-07-2013"
      ["categorie_news"]=>
      string(16) "Ensemble service"
    }
  }
}
object(DatabaseConnection)#6 (5) {
  ["_host:private"]=>
  string(19) "xxxxxx"
  ["_user:private"]=>
  string(14) "xxxxxx"
  ["_password:private"]=>
  string(8) "xxxxx"
  ["_dbname:private"]=>
  string(14) "xxxxxx"
  ["_handle:private"]=>
  object(PDO)#7 (0) {
  }
}
 
Fatal error: Exception thrown without a stack frame in Unknown on line 0
page 2
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
<?php
function chargerClasse ($classe){ require $classe . '.class.php';}
spl_autoload_register ('chargerClasse');
session_start();
echo "news:<pre>";
var_dump($_SESSION['news']);
echo "</pre>";
 
echo "base:<pre>";
var_dump($_SESSION['base']);
echo "</pre>";
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
news:
NULL
base:
NULL