简体   繁体   中英

PHP Session doesn't exist on index.php

I get this error while checking for user session on index.php. Once a user has logged on, I want to display a "Log out" button in the menu. This is the code:

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>

It returns Notice: Undefined index: login_user in /var/www/html/session.php on line 5 on index.php

"It dies with a blank page."

Enable errors using

ini_set('display_errors', 'On'); 

At the top of your page, in your PHP tags and post the error.

try the following corrections:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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