简体   繁体   English

由于会话user_id而退出时出现php错误

[英]php errors when logged out because of session user_id

I'm making a product details page, and on the product details page I'm having if functions to see if the $_SESSION user_id is the same as the $product user_id to echo something or not for example. 我正在创建一个产品详细信息页面,并且在产品详细信息页面上,我正在查看是否有函数来查看$ _SESSION user_id是否与$ product user_id相同以回显某些内容,例如。 When no user is logged in, I get alot of errors that it can't find the $session user_id. 当没有用户登录时,我收到很多错误,无法找到$ session user_id。 How can I solve this problem so when there is no user logged in it does the same as if the session user_id is wrong to the product user_id? 我怎样才能解决这个问题,所以当没有用户登录时,它会像产品user_id的会话user_id错误一样吗?

for example: 例如:

<?php if ( $_SESSION['user_id'] != $product["user_id"]): ?>
 <h3>Email van de eigenaar:</h4>
<?php echo $userarray['email'];?>
       <?php endif; ?>
 </div>

So if the session user_id is wrong to the product user_id i want the same thing to happen when there is no $_SESSION user_id found which is echoing this: <?php echo $userarray['email'];?> 因此,如果会话user_id对于产品user_id是错误的,我希望在没有找到$ _SESSION user_id时发生同样的事情,这就是回应这个: <?php echo $userarray['email'];?>

This is my whole details page: 这是我的整个详细信息页面:

<?php include_once ('templates/header.php'); ?>

<div class="all-content">
       <div class="row">
        <div class="col-lg-12 bg-warning" style="font-size:25px">

            <!-- Maak variables voor gele balkje ingelogt of niet --> 
            <?php $overditcadeau = '<center>Over dit cadeau</center>'; ?> 
            <?php $mijncadeau = '<center>Mijn cadeau</center>'; ?> 

         <?php   if ($_SESSION['user_id'] != $product["user_id"]) { 
    echo $overditcadeau;
 } else { 
    echo $mijncadeau;
  } ?>



        </div>
        </div>
<div class="container">
    <br><br>


<?php    $userarray = $this->db->get_Where('users', array('user_id'=>$product["user_id"]))->row_array();  ?>


    <!-- Als het cadeau niet van de ingelogde gebruiker is laat email van de eigenaar van het cadeau zien -->
    <?php if ( $_SESSION['user_id'] != $product["user_id"]): ?>
 <h3>Email van de eigenaar:</h4>
<?php echo $userarray['email'];?>
       <?php endif; ?>
 </div>



<div class="container">
 <div class="row">



     <div class="overditcadeau" >
    <div class="overditcadeau_foto">
    <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" id="cadeaufoto_overditcadeau">
</div>

<div class="overditcadeau_tekst1">
  <h1> <div class="product_naam"> <?php echo $product['product_naam']; ?>  </div> </h1>
   <h3>Over dit cadeau</h3>
     <div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
   </div>
   <div class="overditcadeau_tekst2">

       <!--Als het cadeau niet van de ingelogde gebruiker is laat een button zien dat heet cadeau aanvragen -->
       <div class="cadeau_aanvragen">
  <?php if ( $_SESSION['user_id'] != $product["user_id"]): ?>
     <a href="<?php echo base_url() ?>/Cadeau_ontvang"> <button type="button" class="btn btn-default">Ik wil dit cadeau!</button></a>

     <button onclick="myFunction()">Ik wil dit cadeau!</button>
<script>
function myFunction() {
    alert("Uw cadeau aanvraag is geaccepteerd, Klik hier om een bericht naar de eigenaar van het cadeau te sturen.");
}
</script>

<?php endif; ?>
</div>

<!--Als het cadeau van de ingelogde gebruiker is laat een button zien dat heet cadeau bewerken-->
<div class="cadeau_bewerken">
        <?php if ( $_SESSION['user_id'] ==  $product["user_id"]): ?>
    <a class="btn btn-primary" href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product['product_id']; ?>"> Cadeau bewerken </a>
<?php endif; ?>
    </div>
    <br>
    <div class="cadeau_verwijderen">
        <?php if ( $_SESSION['user_id'] ==  $product["user_id"]): ?>
    <a class="btn btn-primary" href="<?php echo base_url() ?>/Delete_ctrl/delete_product_id/<?php echo $product['product_id']; ?>"> Cadeau verwijderen </a>
<?php endif; ?>
    </div>

    <!--Als het cadeau van de ingelogde gebruiker is laat aantal geïnteresseerden zien-->
    <div class="aantal_geinteresseerden">
         <?php if ( $_SESSION['user_id'] ==  $product["user_id"]): ?>
    <h4>Aantal geïnteresseerden:</h4>
<?php endif; ?>
    </div>

<?php if ( $_SESSION['user_id'] != $product["user_id"]): ?>
     <div class="aangeboden_door">   Aangeboden door:   
<tr>
    <td>
        <a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id']?>">
          <?php echo $userarray['voornaam'];?>
        </a>
    </td>
    </tr>
</div>

<?php endif; ?> 
</div>


     </div>
     </div>   
</div> 





<div class="container">

<footer>
 <p>&copy; kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>

Use isset() to check variable is set or not. 使用isset()来检查变量是否已设置。

Like below:- 如下: -

if (isset($_SESSION['user_id']) && isset($product["user_id"]) && $_SESSION['user_id'] != $product["user_id"]) {

Note:- Do same for other variables too used on the page. 注意: - 对页面上使用的其他变量执行相同操作。

So the whole code need to be like below:- 所以整个代码需要如下: -

<?php include_once ('templates/header.php'); ?>

<div class="all-content">
       <div class="row">
        <div class="col-lg-12 bg-warning" style="font-size:25px">

            <!-- Maak variables voor gele balkje ingelogt of niet --> 
            <?php $overditcadeau = '<center>Over dit cadeau</center>'; ?> 
            <?php $mijncadeau = '<center>Mijn cadeau</center>'; ?> 

         <?php   if (isset($_SESSION['user_id']) &&  isset($product["user_id"]) && $_SESSION['user_id'] != $product["user_id"]) { 
    echo $overditcadeau;
 } else { 
    echo $mijncadeau;
  } ?>



        </div>
        </div>
<div class="container">
    <br><br>


<?php    $userarray = $this->db->get_Where('users', array('user_id'=>$product["user_id"]))->row_array();  ?>


    <!-- Als het cadeau niet van de ingelogde gebruiker is laat email van de eigenaar van het cadeau zien -->
    <?php if (isset($_SESSION['user_id']) &&  isset($product["user_id"]) &&  $_SESSION['user_id'] != $product["user_id"]): ?>
 <h3>Email van de eigenaar:</h4>
<?php echo $userarray['email'];?>
       <?php endif; ?>
 </div>



<div class="container">
 <div class="row">



     <div class="overditcadeau" >
    <div class="overditcadeau_foto">
    <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" id="cadeaufoto_overditcadeau">
</div>

<div class="overditcadeau_tekst1">
  <h1> <div class="product_naam"> <?php echo $product['product_naam']; ?>  </div> </h1>
   <h3>Over dit cadeau</h3>
     <div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
   </div>
   <div class="overditcadeau_tekst2">

       <!--Als het cadeau niet van de ingelogde gebruiker is laat een button zien dat heet cadeau aanvragen -->
       <div class="cadeau_aanvragen">
  <?php if (isset($_SESSION['user_id']) &&  isset($product["user_id"]) &&  $_SESSION['user_id'] != $product["user_id"]): ?>
     <a href="<?php echo base_url() ?>/Cadeau_ontvang"> <button type="button" class="btn btn-default">Ik wil dit cadeau!</button></a>

     <button onclick="myFunction()">Ik wil dit cadeau!</button>
<script>
function myFunction() {
    alert("Uw cadeau aanvraag is geaccepteerd, Klik hier om een bericht naar de eigenaar van het cadeau te sturen.");
}
</script>

<?php endif; ?>
</div>

<!--Als het cadeau van de ingelogde gebruiker is laat een button zien dat heet cadeau bewerken-->
<div class="cadeau_bewerken">
        <?php if (isset($_SESSION['user_id']) &&  isset($product["user_id"]) &&  $_SESSION['user_id'] ==  $product["user_id"]): ?>
    <a class="btn btn-primary" href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product['product_id']; ?>"> Cadeau bewerken </a>
<?php endif; ?>
    </div>
    <br>
    <div class="cadeau_verwijderen">
        <?php if (isset($_SESSION['user_id']) &&  isset($product["user_id"]) &&  $_SESSION['user_id'] ==  $product["user_id"]): ?>
    <a class="btn btn-primary" href="<?php echo base_url() ?>/Delete_ctrl/delete_product_id/<?php echo $product['product_id']; ?>"> Cadeau verwijderen </a>
<?php endif; ?>
    </div>

    <!--Als het cadeau van de ingelogde gebruiker is laat aantal geïnteresseerden zien-->
    <div class="aantal_geinteresseerden">
         <?php if (isset($_SESSION['user_id']) &&  isset($product["user_id"]) &&  $_SESSION['user_id'] ==  $product["user_id"]): ?>
    <h4>Aantal geïnteresseerden:</h4>
<?php endif; ?>
    </div>

<?php if ( $_SESSION['user_id'] != $product["user_id"]): ?>
     <div class="aangeboden_door">   Aangeboden door:   
<tr>
    <td>
        <a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id']?>">
          <?php echo $userarray['voornaam'];?>
        </a>
    </td>
    </tr>
</div>

<?php endif; ?> 
</div>


     </div>
     </div>   
</div> 





<div class="container">

<footer>
 <p>&copy; kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>

Only way is to check if $_SESSION['user_id'] is set everywhere you are using this. 唯一的方法是检查$ _SESSION ['user_id']是否设置在您使用它的任何地方。

As a quick fix (if you have a lot of references to $_SESSION['user_id']), you can put to your second line: 作为一个快速修复(如果你有很多对$ _SESSION ['user_id']的引用),你可以把你的第二行:

if (empty($_SESSION['user_id'])) $_SESSION['user_id'] = -1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM