<?php

session_start();

?>

line 39: <?php echo $_SESSION['banner']; ?>
line 40: <?php echo $_SESSION['banner']; ?>

Notice: Undefined index: banner in C:\xampp\htdocs\portal\administrator2\admin\dashboard.php on line 39

Notice: Undefined index: banner in C:\xampp\htdocs\portal\administrator2\admin\dashboard.php on line 40

How to fix the error?

Dani AI

Generated

Undefined index "banner" simply means $_SESSION['banner'] was never present (or the session was lost when you tried to read it). As recommended, check existence before printing; ignore 's suggestion to use defined() was right that defined() is for constants. 's question ("where does it get its value?") is the key: you must assign the banner value in the script that handles the upload/login or DB lookup before you try to show it on dashboard.

A minimal pattern: set the session value in the handler, then read it on the dashboard.

<?php
// set-banner.php (handler)
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
$_SESSION['banner'] = '/uploads/banner.jpg';   // store a simple value (path or id)
header('Location: /administrator2/admin/dashboard.php');
exit;
?>
<?php
// dashboard.php (reader)
if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); }
$banner = $_SESSION['banner'] ?? '/images/default-banner.png';  // PHP 7+ fallback
echo '<img src="' . htmlspecialchars($banner, ENT_QUOTES, 'UTF-8') . '" alt="Banner">';
?>

Notes & troubleshooting

  • Call session_start() (or use the session_status() check above) before any output — even stray whitespace or a BOM will break headers and prevent sessions from working.
  • If you must support PHP < 7, replace the ?? fallback with a ternary using isset().
  • Debug by dumping the session after starting it: echo '<pre>'; var_dump($_SESSION); echo '</pre>'; — that shows whether banner was ever written.
  • Prefer storing small values (file name or id) in session, not binary data or large HTML. Also check cookie/domain differences (example.com vs www.example.com) or server session settings if values disappear across requests.

Relevant manual pages: session_start() and isset().

Recommended Answers

All 6 Replies

Is banner set?

 if (isset($_SESSION['banner'])) {
   echo $_SESSION['banner'];
 }

you can try to use define()

if (defined($_SESSION['banner'])) {
    echo $_SESSION['banner'];
}else{
    echo "$_SESSION['banner'] not defined";
}

$_SESSION['banner'] is not set as guys mentioned earlier

I place this code:

<?php
        if (defined($_SESSION['banner'])) {
            echo $_SESSION['banner'];
        }else{
            echo "not defined";
        }
        ?>

The result is not defined.

How to define $_SESSION['banner']? I already place

<?php 

session_start();

?>
Member Avatar for Member #120589

you can try to use define()

That's used to check named CONSTANTS: http://php.net/manual/en/function.defined.php

Do not use this on variables. isset() is what you need. C'mon davy - a DW veteran of three years?

where does $_SESSION['banner'] get its value ??
from database or from form ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.