0

I have this error, and I can't solve it:

Notice: Undefined index: id in C:\xampp\htdocs\store\header.php on line 10

Notice: Undefined index: user in C:\xampp\htdocs\store\header.php on line 11

Notice: Undefined index: password in C:\xampp\htdocs\store\header.php on line 12


I have a login page, where I log in, set the id, user and password of the session. And when I log in, everything works fine except for the error that keeps showing.

This is the header.php :

    <?php
    session_start();
            if (isset($_SESSION["user"])) {
            $msg_logout = '<br />
               <div>                                              
                  <a href="logout.php">Log out</a>              
                </div>';        
            }
          // checking if this SESSION is in the DB
            $userID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); 
            $user = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["user"]);
            $password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); 
                        // Connect to the MySQL database  
            include "conx_to_db.php"; 
            $result = mysqli_query($db_1,"SELECT * FROM fans WHERE username='$user' AND password='$password' LIMIT 1"); // query the person
                        // confirm user in DB
            $existCount = mysqli_num_rows($result);
            if ($existCount == 0) { // evaluate the count
            $msg_login = "<div>
                     <a href='login.php' style='text-decoration:none; color: #000;'>Login</a> </div>
                 <div id='bt_registar'>
                     <a href='registo.php' style='text-decoration:none; color: #000;'>Registar</a>
                 </div>";
                 }
     ?>
     <div id="head">
    <div id="logo">
        <a href="index.php"><img src="img/logo34.png" height="80" border="0">
    </div>      
    <div>
    <?php echo $msg_login; $msg_logout; ?>
        <br /><a href="cart.php"><img   src="img/cart.png" height="56" width="56" style="vertical-align:middle;"><strong>Cart</strong></a>

    </div>
</div>

This is the index.php:

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Store test</title>

    <link rel="stylesheet" href="estilo/style.css" type="text/css" media="screen" />

    </head>
    <body>

    <?php include_once("header.php");?>

    <div id="main"> 

<hr>

    <table border="0" cellspacing="10" cellpadding="10">
        <tr>
        <td width="35%" valign="top" style="border-right:3px solid green">     <h3>&#8226; Service</h3><br /><br />
        <p>Benvindo .</p>
        <p>gets some god stuff.<br /><br />

        <p>&eacute; A store bla bla! <br />
        <br />
            Isto &eacute;  test!</p></td>
        <td width="65%" valign="top"><h3>&#8226; Os mais comprados</h3><br   />
     <p><?php echo $List; ?><br /></p>
                  </td>
    <br />
    <br />
        </tr>
      </table>
      </div>

      <?php include_once("footer.php");?>
      </div>
      </body>
      </html>

I hope someone can explain me what is wrong. Thank you

Leonid Glanz
  • 1,261
  • 2
  • 16
  • 36
Afzal
  • 17
  • 1
  • 4
  • 4
    `$_SESSION["id"]`, `$_SESSION["user"]` and `$_SESSION["password"]` are not set – Paul Dessert Sep 26 '13 at 00:06
  • Hi Afzal, welcome to SO. – Ben Sep 26 '13 at 00:09
  • Well i have the Session set on my login page, with id, user and password. Everything works okay when i log in. i forgot to tell that.. i am going to edit the question to add this info. ...Sorry about that – Afzal Sep 26 '13 at 00:18
  • @Afzal - your login page sets the id, user, and password in `$_SESSION`. Those indexes won't exist until the page is called. I've added this to my answer. – Ben Sep 26 '13 at 01:50

5 Answers5

0

As the error suggests, these three lines

$userID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]);
$user = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["user"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);

are the problem. The script is looking for an id address in the $_SESSION array, and it's not there for some reason, so it reports that the index is undefined.

To fix it, you can use

if (isset($_SESSION['id']) { ... }

or make sure that it's set before it's needed.

EDIT

Your login page sets the id, user, and password in $_SESSION. Those indexes won't exist until the page is called.

header.php needs to know those values though, so either set them ahead of time, or use isset.

Ben
  • 54,723
  • 49
  • 178
  • 224
0

Everything is really simple and PHP already told you what is the error.

All you need is just to read. It is telling you that indecies id, user and password are not defined.

So $_SESSION["id"], $_SESSION["user"] and $_SESSION["password"] are not defined.

Few words to make your life simpler: do not paste 2 pages of your code. Try to localize the problem, because I assume that you would be downvoted severely.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
0

After some time i got it.

the solution is:

   if(isset($_SESSION['id'])){
      if(isset($_SESSION['user'])){
        if(isset($_SESSION['password'])){
        $log = 1;
        }
      }
   }

Thanks everyone for your help, i know it looks basic, but i'm a beginner, and learning!

"please note carefully that these are not errors! They are notices." by James ->Checked

Afzal
  • 17
  • 1
  • 4
0

The normal way to initialize a variable:

$userID = isset($_SESSION["id"]) ? $_SESSION["id"] : NULL;

And after when you are going to do any operation with $userID you can check if this is null

if(!empty($userID)){
   //do something
}

Assigning a default value using Ternary operator

empty()

Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
0

Please check you session with given code that session is empty or not

var_dump($_SESSION);

what ever its show reply to this i will let you know where is the issue.