0

I'm having a really weird issue with one page (it doesn't seem to be happening anywhere else).

When I copy and paste the page source by direct input into the validator, it's valid.

But when using the URI it gives all kinds of errors because its source seems to have two lines above my doctype saying:

<!DOCTYPE html><br />
<b>Notice</b>: Undefined index: user_id in <b>[path]</b> on line <b>11</b><br />

Line 11 is

$userid = $_SESSION['user_id'];

When I print_r($_SESSION); user_id is there and it's correct.

Where is the validator getting these errors from and what can I do to fix it?

EDIT: Here's the code from before that

<?php
    if(!isset($filepath)){
        $filepath = '../';
    }

    $lastseen = 'chatting away on the forums';
    include($filepath . 'includes/header.inc.php');

    print_r($_SESSION); //this displays Array ( [user_id] => 39 [name] => theatre [staff] => officer [logintime] => 8:46pm [upgrade] => undead ) exactly as it should

    $userid = $_SESSION['user_id']; //Line 11

if(isset($_GET['id']) && is_numeric($_GET['id']) && ($_GET['id'] > 0)){
    $boardid = (int) $_GET['id'];
}

/* Validate form submission */
if(($_SERVER['REQUEST_METHOD'] == 'POST') && ($_SERVER['HTTP_HOST'] == 'zombiesim.net') && isset($_POST['postcheck']) && ($_POST['postcheck'] == 'yes')){
    //a bunch of other form validation type stuff, but it doesn't even get here
    }
    ?>
    <!doctype html>
    <html dir="ltr" lang="en-US">
    <head>
    <meta charset="utf-8" />
    <!-- other meta tags, title, and link tags here -->
    <?php
    include($filepath . 'includes/pagehead.inc.php');
    ?>

And here's header.inc.php

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);

    /* Start sessions and output buffering */
    session_start();
    ob_start();

    /* Include Functions */
include_once($filepath . 'includes/functions.inc.php');

/* Definte Referrer URL for Form Validation */
DEFINE('LASTURL', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

/* If logged in, update last seen */
if(isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id'])){
    $userid = (int) $_SESSION['user_id'];
    include($filepath . 'dbupdate.inc.php');
    $now = time();
    $now = date("Y-m-d H:i:s", $now);
    $seenq = "UPDATE users SET lastseen='" . $now . "', lastseenat='" . $lastseen . "' WHERE user_id=$userid";
    $seenr = mysqli_query($dbupdate, $seenq);
}

And here's pagehead.inc.php

<!-- Javascript for Google Analytics tracking -->
<script type="text/javascript" src="<?php echo $filepath; ?>js/analytics.js"></script>
</head>

<body>
<!--then begins the navigation and ends with the opening div for the main content-->
TylerH
  • 20,799
  • 66
  • 75
  • 101
qwigoqwaga
  • 37
  • 8
  • 1
    Is `session_start();` loaded? It's required if you don't have it. – Funk Forty Niner Aug 26 '14 at 02:31
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – scrowler Aug 26 '14 at 02:32
  • Yep. I also have error reporting on. – qwigoqwaga Aug 26 '14 at 02:32
  • Full code, like the entirety of everything? It's kinda a lot and I figured no one really wants to sift through it all (especially since most of it is irrelevant to the issue). But, I'll go put up everything through where the errors begin. – qwigoqwaga Aug 26 '14 at 02:38
  • What validator are you using? If it isn't a PHP-based one, then any PHP variables will likely throw errors, because HTML isn't capable of parsing PHP. – TylerH Jul 14 '23 at 17:57

1 Answers1

1

You have $userid = (int) $_SESSION['user_id']; in header.inc.php and then you're redeclaring it in $userid = $_SESSION['user_id']; //Line 11

Either remove it, or set a different conditional statement for it, since it's already declared in the other file.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • The `session_start();` is in header.inc.php though, which is called before. The session variable is defined and it works fine on the actual site (as well as on every other page that uses this same setup). My major concern is that the validator is finding this extra doctype and error message and I have no idea where it's getting it from because it's definitely not in the actual page source. – qwigoqwaga Aug 26 '14 at 12:59
  • @qwigoqwaga OK, so where you wrote "Here's the code from before that", that isn't what you're using right now? – Funk Forty Niner Aug 26 '14 at 13:04
  • @qwigoqwaga I think I know what's going on. You have `$userid = (int) $_SESSION['user_id'];` in `header.inc.php` and then you're redeclaring it in `$userid = $_SESSION['user_id']; //Line 11` <= remove that one, since it's already declared in the other file. You're overwriting it. – Funk Forty Niner Aug 26 '14 at 13:11
  • The first code block is the top of the main page where the issue is, yes. The second code block is header.inc.php and the third code block is headstuff.inc.php (which is really just HTML, but I included it in case it was relevant) – qwigoqwaga Aug 26 '14 at 13:11
  • @qwigoqwaga Did you read [`my other comment`](http://stackoverflow.com/questions/25497138/html-validator-showing-php-errors-that-the-php-isnt-showing/25498091?noredirect=1#comment39815667_25498091) just above? – Funk Forty Niner Aug 26 '14 at 13:13
  • Just now, yes. Wouldn't that not make a difference, though, because it's being set to the same thing? (And I'm fairly certain I've done that on other pages...I'll go hunt those down and see if I'm getting the same error [they appear to be having the same error too]). – qwigoqwaga Aug 26 '14 at 13:14
  • @qwigoqwaga You can only declare it once. If anything, you would need to do the same thing as you did with `if(isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id'])){ $userid = (int) $_SESSION['user_id'];` – Funk Forty Niner Aug 26 '14 at 13:15
  • Wouldn't that still be declaring it twice, though? And then why isn't PHP showing me this error? – qwigoqwaga Aug 26 '14 at 13:21
  • @qwigoqwaga Technically yes. You could either remove that line `$userid = $_SESSION['user_id']; //Line 11` (since it's already declared), or set a different conditional statement to it, such as "if not set, redirect or show an error message". – Funk Forty Niner Aug 26 '14 at 13:23
  • @qwigoqwaga You're welcome; so is everything ok now? – Funk Forty Niner Aug 26 '14 at 13:32
  • Yeah I got that page worked out. I'll just have to remember to check my other pages and fix those too. Thank you. – qwigoqwaga Aug 26 '14 at 13:33