2016-04-06 56 views
0

我在檢查index.php上的用戶會話時出現此錯誤。一旦用戶登錄,我想在菜單中顯示「註銷」按鈕。這是代碼: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> 

它返回注意:未定義指數:login_user在/var/www/html/session.php上line 5 on index.php

回答

1

「它死了,空白頁。」

使用

ini_set('display_errors', 'On'); 

在你的頁面的頂部,在你的PHP代碼啓用的錯誤和後期的錯誤。

+0

謝謝。不知道你是否可以啓用錯誤。 在第7行的/var/www/html/config.php中顯示:**警告:mysqli_connect():(28000/1045):拒絕用戶'root'@'localhost'(使用password:YES) * 這是否意味着www-data在Linux中沒有正確的權限? –

+0

當你登錄MySQL時,你使用了什麼密碼? 並把它放在密碼槽中。 – Tommy

+0

它是這種格式的字母數字密碼:** Aaaaaaaa1 ** –

1

嘗試以下更正:

<?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(); 
     } 
    } 
?> 
+0

這可以工作,但是我試圖在用戶登錄後在菜單中添加一個鏈接。我在OP中粘貼了代碼。 –