Jump to content

Undefined index: how do i fix it?


Recommended Posts

<?php

/* Displays user information and some useful messages */

session_start();

 

?>

 

<!DOCTYPE HTML>

<head>

<title>CashBallz</title>

<meta charset="utf-8" />

<meta name="viewport" content="width=device-width, initial-scale=1" />

<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->

<link rel="stylesheet" href="assets/css/main.css" />

<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]-->

<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->

</head>

<body class="landing">

<div id="page-wrapper">

 

<!-- Header -->

<header id="header">

<h1 id="logo"><a href="index.php">CashBallz</a></h1>

<nav id="nav">

<ul>

<li><a href="index.php">Home</a></li>

<li>

<a href="#">Play</a>

<ul>

<li><a href="left-sidebar.php">Server 1</a></li>

<li><a href="right-sidebar.php">Server 2</a></li>

<li><a href="no-sidebar.php">Server 3</a></li>

</ul>

</li>

<li><a href="elements.php">Paypal</a></li>

<li><a href="loginsystem/loginpage.php" class="button special">Sign Up/Login</a></li>

</ul>

</nav>

<br>

<p style="float:right; font-size: 30px" > 

<?php if ($_SESSION['logged_in'] = true){

$first_name = $_SESSION['first_name'];

$last_name = $_SESSION['last_name'];

}   ?>

Welcome <?= $first_name.' '.$last_name ?></title>

?>                </p>

</header>

 

 

I am getting the error: Undefined index: first_name in (my file name) on line 41

and, Undefined index: last_name in (my file name) on line 42

 

How do i fix it? I am new to php. Thank you!

Link to comment
Share on other sites

You don't seem to understand the difference between a variable assignment (“=”) and an equality check (“==”).

 

Your if statement uses an assignment instead of a check, and the assignment is always true. So you always try to access the first and last name, even if the user isn't logged in. On top of that, the “Welcome” message is outside of the statement, so it also happens in any case.

 

Fix that:

<?php

// any HTML input *must* be HTML-escaped to prevent the user from injecting malicious JavaScript code
function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding);
}

session_start();

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Title</title>
    </head>
    <body>
        <?php if (isset($_SESSION['logged_in'])):    // use isset() checks instead of simply accessing data that may not exist; this prevents warnings ?>
            Welcome <?= html_escape($_SESSION['first_name'], 'UTF-8') ?> <?= html_escape($_SESSION['last_name'], 'UTF-8') ?>!
        <?php else: ?>
            Welcome guest!
        <?php endif; ?>
    </body>
</html>
Link to comment
Share on other sites

I just told you. You obviously took no time at all to look into what I posted. Maybe someone else will spoon feed you the answer. On help forums you are expected to put some effort in.

Oh sorry, i didn't see the link t the bottom of your answer, I thought that was just your account description. I'll look into it. Hopefully someone does spoon-feed me the answer though. :)

Link to comment
Share on other sites

 

You don't seem to understand the difference between a variable assignment (“=”) and an equality check (“==”).

 

Your if statement uses an assignment instead of a check, and the assignment is always true. So you always try to access the first and last name, even if the user isn't logged in. On top of that, the “Welcome” message is outside of the statement, so it also happens in any case.

 

Fix that:

<?php

// any HTML input *must* be HTML-escaped to prevent the user from injecting malicious JavaScript code
function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding);
}

session_start();

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Title</title>
    </head>
    <body>
        <?php if (isset($_SESSION['logged_in'])):    // use isset() checks instead of simply accessing data that may not exist; this prevents warnings ?>
            Welcome <?= html_escape($_SESSION['first_name'], 'UTF-8') ?> <?= html_escape($_SESSION['last_name'], 'UTF-8') ?>!
        <?php else: ?>
            Welcome guest!
        <?php endif; ?>
    </body>
</html>

I love you. Thanks so much.

Link to comment
Share on other sites

 

You don't seem to understand the difference between a variable assignment (“=”) and an equality check (“==”).

 

Your if statement uses an assignment instead of a check, and the assignment is always true. So you always try to access the first and last name, even if the user isn't logged in. On top of that, the “Welcome” message is outside of the statement, so it also happens in any case.

 

Fix that:

<?php

// any HTML input *must* be HTML-escaped to prevent the user from injecting malicious JavaScript code
function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding);
}

session_start();

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Title</title>
    </head>
    <body>
        <?php if (isset($_SESSION['logged_in'])):    // use isset() checks instead of simply accessing data that may not exist; this prevents warnings ?>
            Welcome <?= html_escape($_SESSION['first_name'], 'UTF-8') ?> <?= html_escape($_SESSION['last_name'], 'UTF-8') ?>!
        <?php else: ?>
            Welcome guest!
        <?php endif; ?>
    </body>
</html>

Actually never mind, i'm really sorry but i'm still getting the same error.

Link to comment
Share on other sites

Undefined index: first_name in (my file name) on line 41
Undefined index: last_name in (my file name) on line 42

 

"Undefined index" errors occurs when you trying accessing something that does not exists.
 
When you first put
if ($_SESSION['logged_in'] = true)

Inside $_SESSION['logged_in'] is now 'true'. Since it's a session, it stays like that until you clear your cookies/session in your browser. Or session_destroy().

 

Because it's true, and you fixed to '==', it's entering this condition now, but inside $_SESSION['first_name'] there is nothing.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.