为什么我在网站的每个页面上都有不同的session_id?

时间:2019-02-09 01:18:19

标签: php session session-variables php-5.6

我创建了一个PHP(MVC)网站版本5.6.30。我在index.php中添加了session_start(),但是每次更改页面时都会更改session_id(),使存储的变量变糟。

这是index.php文件,用于创建与数据库的连接并根据接收的URL创建一个控制器。

    <?php
        chdir(dirname(__DIR__));
        require_once __DIR__ .'/../core/Domanda.php';
        session_start();
        session_id();
        require_once __DIR__.'/../helpers/class.smtp.php';
        require_once __DIR__.'/../helpers/class.phpmailer.php';


        require_once __DIR__.'/../DB/DBPDO.php';

        require_once __DIR__ .'/../DB/DbFactory.php';

        require_once __DIR__.'/../config/tfpdf.php';

        require_once __DIR__.'/../config/PDF.php';

        require_once __DIR__ . '/../app/controllers/PostController.php';
    ecc..

$controller->display();

这是controller.php文件,它确定哪种方法并使用发布模型执行各种调用

     class PostController extends BaseController{
          protected $layout = 'layout/index2.tpl.php';
  public $content ='Controller';

 protected $conn;
          protected $Post ;

          public function __construct(PDO $conn) {
         $this->conn=$conn;
            $this->Post = new Post($conn);

          }

public function display(){
    require  $this->layout;
  } 


          public function getArticles(){ //home
            global $page;
            $posts = $this->Post->all($page);
            $evento = $this->Post->allPosts(1);
            $articoliTOT = $this->Post->countArticoli();

            $this->content =  viewSidebar('articoliHome', compact('posts'), $this->sidebar,$this->lingua, compact('evento'), $articoliTOT->total);
          }

这是查询数据库的模型文件

<?php
namespace App\Models;
use \PDO;
use \PDF;
use \PHPMailer;
class Post extends Base{


    public function __construct(PDO $conn) {
        parent::__construct($conn);
    }

    public function all($page){
        $result = [];
        $RECORDS_PER_PAGE=RECORDS_PER_PAGE+1;
        $start= $RECORDS_PER_PAGE * ($page -1);
          if($start< 0) {
              $start = 0;
          }

        $sql = 'SELECT * FROM articoli_'.$_SESSION['lan'].' WHERE tipo >=3 ORDER BY art_data DESC LIMIT ? , ?';
        $stm = $this->conn->prepare($sql);
        $stm->bindParam(1, $start, PDO::PARAM_INT);
        $stm->bindParam(2, $RECORDS_PER_PAGE, PDO::PARAM_INT);
        $stm->execute();

        if($stm && $stm->rowCount()){

            $result =  $stm->fetchAll(PDO::FETCH_OBJ);
        }

        return $result;

    }

functions.php

function viewSidebar($view, array $data = [], $sidebar, $lingua, array $dataSB = [], $articoliTOT){ //5
  global $page;
  extract($data);
  extract($dataSB);
  ob_start();
  require __DIR__ .'/../app/views/'.$view.'.tpl.php';
  $output =  ob_get_contents();
  ob_end_clean();
  return $output;
}

index2.php

<?php
require_once (__DIR__.'/../layout/header.tpl.php');
require_once (__DIR__.'/../layout/nav.tpl.php');
?>
     <div id="contenuto" class="row mx-3 my-3">

    <?=$this->content?>
  </div>


</div>

<?php
require_once (__DIR__.'/../layout/footer.tpl.php');?>

为什么我在网站的每个页面上都有不同的session_id?

1 个答案:

答案 0 :(得分:-1)

将session_start放置在文件的顶部,在会话初始化之前应该没有输出,并且我不知道require_once __DIR__ .'/../core/Domanda.php';包含的内容