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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.