【问题标题】:PHP SESSION undefined variablePHP SESSION 未定义变量
【发布时间】:2020-10-22 13:31:03
【问题描述】:

enter image description here 晚上好,我正在做一个小型的大学项目。我正在创建一个网站,人们可以使用 PHP 发布他们使用过的物品以出售它们或与另一个对象交换它们(一种 ebay,但简单 100000 倍),这对我来说是新的。为简单起见,在填写连接表单 (connexion.php) 后,用户将被重定向到他自己的个人资料 (profil.php),其中出现了他的用户名和电子邮件(从数据库中获取它们之后)。如下图:

//////////connexion.php : //////////

<?php
session_start(); 
try
{
    $bdd = new PDO('mysql:host=localhost;dbname=espace_membre;charset=utf8', 'root', '');
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage()); //message d'erreur au cas où la connexion échoue
}

if(isset($_POST['formconnect']))  //vérifie que le bouton pour se connecter est enclenché 
{
  $mailconnect = htmlspecialchars($_POST['mailconnect']); 
  $mdpconnect = sha1($_POST['mdpconnect']);
  if(!empty($mailconnect) AND !empty($mdpconnect))
  {
     $requser=$bdd->prepare("SELECT * FROM membres WHERE email = ? AND mdp= ?");  
     $requser->execute(array($mailconnect,$mdpconnect));
     $userexist=$requser->rowcount(); 
     if($userexist==1)
     {
          $userinfo=$requser->fetch();
          $_SESSION['id']=$userinfo['id'];
          $_SESSION['pseudo']=$userinfo['pseudo'];
          $_SESSION['email']=$userinfo['email'];
          header("Location: profil.php?id=".$_SESSION['id']);
     }
     else
     {
      $erreur="password or mail not valid" ; 
     }
  }
  else
  {
    $erreur =" please complete all inputs " ; 
  }
}
 ?>

连接后,用户被重定向到他的个人资料,名为profil.php,如下所示:

<?php
session_start();

 
//tentative de connexion à la base de donnée 
try
{
    $bdd = new PDO('mysql:host=localhost;dbname=espace_membre;charset=utf8', 'root', '');
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage()); //message d'erreur au cas où la connexion échoue
}


if(isset($_GET['id']) AND $_GET['id'] > 0)
{
  $getid=intval($_GET['id']);
  $requser= $bdd -> prepare('SELECT * FROM membres WHERE id= ?');
  $requser->execute(array($getid));
  $userinfo=$requser->fetch();


[the table where my users infos are stored. membres means members in french][1]

<html>

<head>
    <title>Profil de <?php echo $userinfo['pseudo']?></title>    
    <meta charset='utf-8'>
</head>

  <div align="center">
         <h3>profil TROKI</h3>
        <br/>
        
        
      <h3>les annonces de <?php echo $userinfo['pseudo']?> </h3>
          pseudo = <?php echo $userinfo['pseudo']?> <br/> //this one works
           mail = <?php echo $userinfo['email']?> <br/>  //this one works too

           <?php 
            if (isset($_SESSION['id']) AND $userinfo['id']==$_SESSION['id']) 
            {
             ?>
              //everything here is visible only when the user is logged in 
             <h>Bienvenue dans ton profil <?php echo $userinfo['pseudo']?> </h2> 
             <a href="editionprofil.php"> éditer mon profil</a> 
             <a href="modifiermdp.php">modifier mon mot de passe</a>
             <a href="deconnexion.php"> se déconnecter</a>
             <a href="formulaireajout.php"> ajouter une annonce</a>
            
             <?php 
            }
           ?>
    </div>

 <?php    
  }
   else
   ?>
</html>

每当我想打印类似的东西时

  <h2>welcome to your profile,<?php echo $userinfo['pseudo']?> </h2>

变量 $userinfo['pseudo'] 变成了我的用户昵称,这正是我想要的。

但是,由于我不知道,我无法让这些变量在任何其他页面上工作并且我不断收到此错误:

** 注意:未定义变量:第 64 行 C:\wamp\www\projet2\formulaireajout.php 中的用户信息**

我创建了另一个页面供用户填写表单以发布内容

 <?php
session_start();
try
{
    $bdd = new PDO('mysql:host=localhost;dbname=espace_membre;charset=utf8', 'root', '');
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage()); //message d'erreur au cas où la connexion échoue
}

// ////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($_SESSION['id']))
{   echo "ok"; // I'm getting "ok" so this condition is verified
 }

else
{
  //echo "lol";
  header('location:connexion.php');
} 
if(isset($_GET['id']) AND $_GET['id'] > 0)
{
  $getid=intval($_GET['id']);
  $requser= $bdd -> prepare('SELECT * FROM membres WHERE id= ?');
  $requser->execute(array($getid));
  $userinfo=$requser->fetch();
}

<!DOCTYPE html>
<html>
<head>
    <title>Ajouter une annonce</title>
</head>
<body>
    <h3>You can post your things here,<?php echo $userinfo['pseudo']?></h3> // here, $userinfo generates an undefined variable error ! 
    <div align="center">
    'some extra code deleted'

</body>
</html>

好吧,我的问题是,如果我的 $userinfo 是在 profile.php 页面中定义的,为什么它会在我正在创建的所有其他页面中生成错误?我究竟做错了什么 ? (这个项目大约是我学期分数的 60%,所以我遇到了麻烦,我无法解决它)

感谢您的阅读,祝您有美好的一天!

【问题讨论】:

    标签: php mysql session phpmyadmin


    【解决方案1】:

    在连接页面上,您将 URL 中的 id 传递给配置文件页面,如下所示:header("Location: profil.php?id=".$_SESSION['id']); 配置文件页面上正在寻找要设置的 $_GET['id'] 的函数得到满足,因为 id 是在 URL 中设置。

    但是,如果我是用户并单击这些链接:

    • editionprofil.php
    • 修饰符mdp.php
    • deconnexion.php
    • formulaireajout.php

    ID 没有在 URL 或页面请求中传递,但您的代码仍在寻找要在这些页面上设置的值,以及下面的代码 sn-p:

    if(isset($_GET['id']) AND $_GET['id'] > 0)
    {
      $getid=intval($_GET['id']);
      $requser= $bdd -> prepare('SELECT * FROM membres WHERE id= ?');
      $requser->execute(array($getid));
      $userinfo=$requser->fetch();
    }
    

    因为没有设置 $_GET['id'] 这个变量$userinfo 没有被定义为以后在你的代码中使用。仅当 $_GET['id'] 已设置且大于 0 时才会设置。

    我的建议是,由于您已经在会话中设置了用户 ID,因此请在其他页面上调用 $_SESSION 变量。因此,只要您现在在文件顶部继续启动会话并且不结束或销毁会话,您应该能够访问其他页面上 $_SESSION 数组的值:

          $_SESSION['id']=$userinfo['id'];
          $_SESSION['pseudo']=$userinfo['pseudo'];
          $_SESSION['email']=$userinfo['email'];
    

    对于您已经在会话中设置的值,只需稍后在其他页面上的代码中回显这些值即可。所以调用它应该可以在其他页面上工作:

    &lt;?php echo $_SESSION['pseudo']?&gt;&lt;/h3&gt;

    如果您想在用户“注销”时销毁会话和/或取消设置值。此外,由于这些值在会话中,因此您无需在每个页面上为相同的值进行数据库查找,除非它们要更改或您正在查找尚未存储在会话中的新数据。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的准确回答,Shakima !我已经在我的所有页面中添加了此代码:if(isset($_SESSION['id'])) { echo "ok"; } else { //echo "lol"; header('location:connexion.php'); 作为(基本)安全性,所以每当我尝试直接访问页面(如 formulaireajout.php)而没有正确登录时,我都会被重定向到 connexion.php 页面这意味着 $_SESSION['id'] 完成了它的工作。但是即使在将会话变量传递给其他页面之后,仍然没有定义 $userinfo 所以我用 $_SESSION 变量替换了所有变量并且它可以工作。谢谢!!!
    猜你喜欢
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2012-05-24
    • 2019-09-18
    • 2012-08-08
    • 1970-01-01
    相关资源
    最近更新 更多