【问题标题】:HTML Validator showing PHP errors that the PHP isn't showingHTML Validator 显示 PHP 未显示的 PHP 错误
【发布时间】:2014-08-26 02:30:41
【问题描述】:

我在一个页面上遇到了一个非常奇怪的问题(它似乎没有发生在其他任何地方)。

当我通过直接输入将页面源复制并粘贴到验证器中时,它是有效的。

但是当使用 URI 时,它会出现各种错误,因为它的来源似乎在我的 doctype 上方有两行:

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

第 11 行是

$userid = $_SESSION['user_id'];

当我print_r($_SESSION); user_id 在那里并且它是正确的。

验证器从哪里得到这些错误,我可以做些什么来修复它?

编辑:这是之前的代码

<?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');
    ?>

这里是 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);
}

这里是 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-->

【问题讨论】:

  • session_start(); 加载了吗?如果你没有它,它是必需的。
  • 是的。我也有错误报告。
  • 完整代码,就像所有东西一样?这有点多,我认为没有人真的想筛选所有内容(特别是因为其中大部分与问题无关)。但是,我会从错误开始的地方提出所有内容。

标签: php validation html-validation


【解决方案1】:

你在header.inc.php 中有$userid = (int) $_SESSION['user_id'];,然后你在$userid = $_SESSION['user_id']; //Line 11 中重新声明它

要么删除它,要么为它设置一个不同的条件语句,因为它已经在另一个文件中声明了。

【讨论】:

  • session_start(); 在之前调用的 header.inc.php 中。会话变量已定义,它在实际站点(以及使用相同设置的所有其他页面)上运行良好。我主要担心的是验证器正在找到这个额外的文档类型和错误消息,我不知道它从哪里得到它,因为它绝对不在实际的页面源中。
  • @qwigoqwaga 好的,所以你写的“这是之前的代码”,这不是你现在使用的吗?
  • @qwigoqwaga 我想我知道发生了什么。你在header.inc.php 中有$userid = (int) $_SESSION['user_id'];,然后你在$userid = $_SESSION['user_id']; //Line 11 中重新声明它
  • 第一个代码块是问题所在主页的顶部,是的。第二个代码块是 header.inc.php,第三个代码块是 headstuff.inc.php(实际上只是 HTML,但我将其包括在内以防万一)
  • @qwigoqwaga 你读到上面的my other comment了吗?
猜你喜欢
  • 2012-08-02
  • 2013-07-21
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 2012-11-10
  • 2014-04-05
  • 2011-04-23
相关资源
最近更新 更多