【问题标题】:Why my PHP session's variable doesn't work anymore?为什么我的 PHP 会话变量不再起作用?
【发布时间】:2020-09-07 01:51:05
【问题描述】:

我有一个用 PHP 编写的网络平台。在这个平台上,我们有一个登录页面和一个支付页面,它们以前都可以正常工作,但现在 PHP 会话的变量不再工作了。代码库是一样的。 /tmp 文件夹存在,我已经写入权限并且会话正确启动,但是每次我设置会话变量并切换到下一页时,超全局数组都是空的。

这是登录脚本。注意第49行

<?php 
include_once('database.php');
include_once('const.php');
include_once('functions.php');

if( isset($_POST['email']) && isset($_POST['password']) ){
    $email = htmlspecialchars(strip_tags($_POST['email']));
    $password = htmlspecialchars(strip_tags($_POST['password']));

    $db = new Database();
    $conn = $db->getConnection();

    if( accountAlreadyExists($email, "ANAGRAFICA_SOCI") ){
        $query = "SELECT * FROM `wcksdc5_DWH`.ANAGRAFICA_SOCI WHERE EMAIL = :email ;";
        $stmt = $conn->prepare($query);
        $stmt->bindValue(":email", $email, PDO::PARAM_STR);
        try{
            $stmt->execute();
        }catch(PDOException $e){
            echo ' Error: ' . $e->getMessage();   
            echo ' Line: ' . $e->getLine();   
            echo ' File: ' . $e->getFile();
        }
        if($stmt->rowCount() > 0){
            $res = $stmt->fetch(PDO::FETCH_ASSOC);
            $dbPass = $res['PASSWORD'];
            if(password_verify($password, $dbPass)){
                if(!session_start()){
                    die('Impossibile creare sessione..');
                }
                $_SESSION['userLogged'] = true;
                $_SESSION['nome'] = $res['NOME'];
                $_SESSION['email'] = $email;
                header('Location: ../dashboard.php');
            }else{
                die('Password errata, riprovare.');
            }
        }else{
            die("Errore, se il problema persiste contattare l'amministratore di sistema..");
        }
    }else if( accountAlreadyExists($email, "UTENTI_DA_APPROVARE") ){
        $var = checkTmpPaymentStatus($email);
        if( $var ){
            echo "L'account è in attesa di approvazione da parte del maestro. Si prega di riprovare più tardi";
        }else if( !$var ){
            if(!session_start()){
                die('Impossibile creare sessione di pagamento..');
            }
            $_SESSION['userLogged'] = true;
            $_SESSION['email'] = $email;
            header('Location: ../membership-fee.php');
            exit();
        }else{
            "Trovato valore non conosciuto";
        }
    }else{
        die("L'account non esiste. Si prega di ricontrollare le credenziali");
    }
    
}else{
    die("Non sono presenti tutti i dati richiesti per performare l'operazione");
}
?>

这是下一页

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
include_once('./php/database.php');
include_once('./php/const.php');
include_once('./php/functions.php');
include_once('./php/json-fn.php');

if(!isset($_SESSION['userLogged']) || $_SESSION['userLogged'] != true){
  die("Non possiedi i privilegi per visitare questa sezione");
}
?>

【问题讨论】:

  • 尝试将session_start(); 放在页面顶部&lt;?php 标记之后和任何其他代码或文本之前。以后可以省去很多麻烦。此外,您要访问的所有页面session 都应该在顶部

标签: php session session-variables


【解决方案1】:

与我编写的代码类似,您必须将其包含在所有页面中

<?php
   session_start();
   $title=$_SESSION['username'];
   $pic=$_SESSION['pic'];
   if(!risset($_SESSION['username']))
      header('Location:index.php');
   ?>

您可以在此之后立即开始您的 html 代码

要创建会话,您可以使用下面的代码

$_SESSION['username'] = $row['username'];
$_SESSION['pic']=$row['propic'];

销毁会话(注销)

<?php
   session_start();
   session_destroy();
   header('Location:index.php');
?>

【讨论】:

  • 感谢您的回复,但我看不出这段代码对我有什么帮助
  • 总是在提问时发布您尝试过的内容和代码 sn-p
猜你喜欢
  • 1970-01-01
  • 2012-09-23
  • 2017-12-12
  • 2013-11-10
  • 2015-09-30
  • 2012-08-17
  • 2014-05-16
相关资源
最近更新 更多