簡體   English   中英

PHP Session在index.php上不存在

[英]PHP Session doesn't exist on index.php

我在index.php上檢查用戶會話時收到此錯誤。 用戶登錄后,我想在菜單中顯示“注銷”按鈕。 這是代碼:

session.php

<?php
   include('config.php');
   session_start();

   $user_check = $_SESSION['login_user'];

   $ses_sql = mysqli_query($db,"select username from users where username = '$user_check' ");

   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['username'];

?>

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
   include('session.php');
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/css/main.css" />
<title>LVRP</title>
</head>

<body>
<div id="top-line"></div>

<div id="header">
    <div class="wrapper">
        <div class="logo"> </div>
    </div>
</div>

<div id="menu">
    <div class="wrapper">
        <ul id="navigation">
            <li><a class="active" href="index.php">Home</a></li>
            <li><a class="inactive" href="/forum" target="_blank">Forums</a></li>
            <li><a class="inactive" href="ucp.php">User Control Panel</a></li>
            <?php if(isset($_SESSION['login_user'])){echo('<li><a class="inactive" href="logout.php">Log out</a></li>');} ?>
        </ul>
    </div>
</div>

<div id="content">

</div>


</body>
</html>

它返回注意:未定義的索引: index.php的第5行上的/var/www/html/session.php中的login_user

“它死於空白頁。”

使用以下方式啟用錯誤

ini_set('display_errors', 'On'); 

在頁面頂部的PHP標記中,並發布錯誤。

請嘗試以下更正:

<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT ID FROM users WHERE username = '$myusername' and password = '$mypassword'";

      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      //$active = $row['active'];
      $active = $row['ID'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         // This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0. (From: http://php.net/manual/en/function.session-register.php)
         //session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: ucp.php");
      }else {
         $error = "Wrong username/password.";
         echo "<pre>";var_dump($error);exit();
      }
   }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM