-2

I get the following error message:

Undefined index: log in C:\wamp\www\Networking Script\home.php on line 20

When using the following PHP code:

<?php
  include("session/DBConnection.php");
   $user = $_SESSION['log']['username'];  //this is the 20th line in home.php
    $query = mysql_query("SELECT * FROM members WHERE username = '$user'")or die (mysql_error()); 
    $display = mysql_fetch_array($query);   ?>

Please Help me Guys :) Thank you :)

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Lalith
  • 1
  • 2

6 Answers6

2

Start your script with

session_start();

Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
1

Undefined index is probably a "notice". Just means that you are trying to access a part of the array that hasn't been initialized first.

you can use array_key_exists before you read from the array to check if the offset exists first

Marc
  • 416
  • 3
  • 8
0

It is also possible that you have just forgotten to start your session. Remember, you also have to use this function to resume a session: PHP: session_start - Manual

Eastborn
  • 73
  • 1
  • 5
0

You need to invoke session_start at beginning of each file where session data is accessed.

Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
0

it just tell you that variable on line 20 does not exist yet.

session_start();

if(isset($_SESSION['log']['username'])
{

include("session/DBConnection.php");
        $user = $_SESSION['log']['username'];  
        $query = mysql_query("SELECT * FROM members WHERE username = '".mysql_real_escape_string($user)."'")or die (mysql_error()); 
        $display = mysql_fetch_array($query); 
{
else
{
// on username
}

http://www.php.net/manual/en/book.pdo.php

http://uk1.php.net/manual/en/mysqlinfo.api.choosing.php

http://php.net/manual/en/function.htmlspecialchars.php output code to browser

http://php.net/manual/en/function.mysql-real-escape-string.php

amigura
  • 541
  • 3
  • 6
0

Pretty sure it's been said, but at the risk of being rude......It means that vairable has not been set. It's empty. session_start(); Would be the first thing to check. If it doesn't get through your "if isset" piece this value won't be set. Try simply printing out the variable after it's allegedly been set, also check the variable BEFORE it gets sent anywhere.

TSkagen
  • 67
  • 7