简体   繁体   English

函数未在PHP中定义变量

[英]Function not defining variable in PHP

I've inherited a site from a previous (no longer available for help) developer and I'm not much of a PHP coder to begin with so, bear with me. 我已经继承了以前的开发人员(不再提供帮助)的站点,因此我并不是一个PHP编码器,因此请多多包涵。

I keep getting an error that some variables are undefined and the CMS Admin toolbar keeps showing up at the top of the page even though I'm not logged in. 我一直收到错误消息,指出一些变量未定义,并且即使我没有登录,CMS Admin工具栏也始终显示在页面顶部。

Important Note This only happens in my local development environment. 重要说明这仅在我的本地开发环境中发生。

The error: 错误:

Notice: Undefined variable: buttons in /Users/shortname/Sites/sitename.com/public_html/admin/editable.class.php on line 275

Edit Here's the complete file: 编辑这是完整的文件:

Pastebin of editable.class.php Pastablebin的editable.class.php

In my page, I've got an include 在我的页面中,我有一个包含

<?php
    include_once 'admin/editable.class.php';

    $page = new page(6, 2, 0);
    echo $page->get();
?>

It's job is to grab all the page elements from the DB and throw it on the page. 它的工作是从数据库中获取所有页面元素,然后将其扔到页面上。

The include appears later in the page as well (in the footer) 包含内容也会显示在页面的后面(在页脚中)

<?
include_once "admin/editable.class.php";
$login = new dynamic;

if($login->isAdmin())
{
    echo '
    <div id="adminBar">
        <div>
            <h3>Admin Menu</h3> <span class="editable">Editable Pages: <a href="/company/media.php">In The Media</a> | <a href="/company/faq_about.php">FAQ</a> | <a href="/company/tradeshows.php">Tradeshows</a></span> <a class="logout" href="/admin">Logout</a>
        </div>
    </div>
    ';
}
?>

Which I assume is what shows the CMS info bar and buttons if you're an authenticated user. 如果您是经过身份验证的用户,我认为是显示CMS信息栏和按钮的内容。 The trouble is, I can't authenticate and I can't get the warnings to go away. 问题是,我无法通过身份验证,也无法收到警告。 Here's the authentication functions in editable.class.php : 这是editable.class.php的身份验证功能:

function isAdmin()
    {
        if (isset($_COOKIE["admin"]) && $_COOKIE["admin"] === sha1("username".$this->salt.$this->users["username"]))
        {
            $this->admin = true;

            return true;
        }
        else
        {
            $this->admin = false;

            return false;
        }
    }

    function login()
    {
        if($this->isAdmin())
        {
            return true;
        }

        if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($this->users[$_SERVER['PHP_AUTH_USER']]) || $this->users[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW']) 
        {
            header('WWW-Authenticate: Basic realm="Administration"');
            header('HTTP/1.0 401 Unauthorized');
            echo 'Authorization Required';
            exit;
        }
        else
        {           
            setcookie("admin", sha1("username".$this->salt.$this->users["username"]), 0, "/", ".sitename.com");
            $this->admin = true;

            return true;
        }
    }

    function logout()
    {
        header('HTTP/1.0 401 Unauthorized');
        setcookie("username", "", time() - 1, "/", ".sitename.com");
    }

I tried adding <?php setcookie("username", "", time() - 1, "/", ".sitename.com"); ?> 我尝试添加<?php setcookie("username", "", time() - 1, "/", ".sitename.com"); ?> <?php setcookie("username", "", time() - 1, "/", ".sitename.com"); ?> to see if I could clear out the cookie data and remove the admin tools but, that didn't work either. <?php setcookie("username", "", time() - 1, "/", ".sitename.com"); ?>看看是否可以清除Cookie数据并删除管理工具,但是那也不起作用。 What am I missing on my local development environment that would cause these errors? 我在本地开发环境中缺少哪些会导致这些错误的信息?

It is giving you a notice because when you define the buttons variable, it is out of scope. 它给您一个提示,因为定义按钮变量时,它超出了范围。 So, just change your code from: 因此,只需从以下位置更改代码:

if($this->admin)
{
    $buttons =  $this->editButtons;
}
else
{
    $butons = "";
}

to this: 对此:

$buttons = '';
if($this->admin)
    $buttons = $this->editButtons;

This is shorthand code, you can add the brackets if you like. 这是简写​​代码,您可以根据需要添加方括号。

I suspect the dev environment has the warning level turned on which is why you are getting this. 我怀疑开发环境已打开警告级别,这就是为什么要这样做。

Go to the file in question and before line 275 define the variable 'buttons' as a blank string. 转到相关文件,并在第275行之前将变量“按钮”定义为空白字符串。 You just need to initialise the variable before it is used. 您只需要在使用变量之前对其进行初始化。

$buttons = '';

added after viewing codebin: 在查看代码仓后添加:

try changing 尝试改变

$item["buttons"] = $buttons;

to

if (isset($buttons)) {
  $item["buttons"] = $buttons;
}
else {
  $item['buttons'] = '';
}

that way it only calls $buttons if it's been set already, you need to set $item['buttons'] to a blank string or it'll throw a similar error when used later otherwise 这样,它仅在已经设置了$ buttons的情况下才调用它,您需要将$item['buttons']设置为空白字符串,否则稍后使用时会抛出类似错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM