确定是否在 PHP 的包含文件中设置了变量 [英] Determining if a variable is set in an included file in PHP

查看:48
本文介绍了确定是否在 PHP 的包含文件中设置了变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定我是否有独特的问题,或者只是无法在 google 上找到正确的术语来找到它,但我的问题是处理未定义的变量而无法检查它.

not sure if I have a unique problem or just haven't been able to hit the right terms on google to find it, but my problem is dealing with an undefined variable and not being able to check it.

在我的菜单站点中,我包含一个包含所有菜单内容的 php 文件,然后根据需要回显各个部分.我的问题是菜单选项之一显示当前登录的用户.当然,当未登录时,不会设置此项,这会导致日志文件充满未定义的错误.

Across my site for menus I include a php file with all the menu stuff in it, then echo out the various parts as needed. My problem is that one of the menu options shows the currently logged in user. Of course when not logged in this is not set, which results in the log file being filled with undefined errors.

结构如下:

static.php:

static.php:

<?php
$menu='     
    <ul class="menu">
        <li class="menu"><a href="#">Home</a></li>
        <li class="menu"><a href="#">Option</a></li>
    </ul>';
$usermenu=' 
    <ul class="usermenu">
        <li class="menu"><a href="#">'.$_SESSION['user_name'].'</a></li>
        <li class="menu"><a href="#">Change Pass</a></li>
        <li class="menu"><a href="#">Logout</a></li>
    </ul>';

index.php:

<?php include 'static.php';
    echo "$menu";
    if ($login->isUserLoggedIn() == 'true') {
        echo "$usermenu";
    }
    if ($login->isUserAdmin() == 'true') {
        echo "$usermenuadmin";
    }
?>

如您所见,$_SESSION['user_name'] 位于静态文件中,因此如果您未登录,则不会设置它并产生错误.

As you can see, the $_SESSION['user_name'] is within the static file, so if you aren't logged in it isn't set and produces the error.

通常我只会使用 if (!session_status() == PHP_SESSION_NONE) {},但由于它在中间,我不能把它放在那里.

Normally I would just use if (!session_status() == PHP_SESSION_NONE) {}, but since it is in the middle, I cant put it in there.

修复它的一种方法是为类似的事情创建一个单独的静态文件,并且仅在设置会话时才包含它,但是我将不得不跨多个页面更改代码,并且必须有一个单个条目的单独文件.

One way to fix it would be to have a separate static file for things like that and only include it if the session is set, but then I would have to go and change the code across many pages and would have to have a separate file for a single entry.

我在这里遗漏了什么,或者有什么简单的方法可以解决这个问题?从我在网上找到的内容来看,不可能回显 php 代码,因此这会阻止任何正常的修复方法.

Am I missing something here or is there an easy way to fix this? From what I found online it isn't possible to echo out php code, so that prevents any of the normal ways of fixing it.

解决了,请看下面我的回答!

Solved it, see my answer below!

推荐答案

请使用 isset($var) 检查变量是否已声明/设置.

Please use isset($var) to check if the variable has been declared/set.

if(isset($_SESSION) && isset($_SESSION['user_name']))


因此,您的 static.php 将如下所示,


So, your static.php would look as follows,

$usermenu='';
if(isset($_SESSION) && isset($_SESSION['user_name']))
{
    $usermenu='<ul class="usermenu">
        <li class="menu"><a href="#">'.$_SESSION['user_name'].'</a></li>
        <li class="menu"><a href="#">Change Pass</a></li>
        <li class="menu"><a href="#">Logout</a></li>
    </ul>';
}

以上,如果 $_SESSION 和 $_SESSION['user_name'] 未设置,您将在 $usermenu 中有一个空字符串.
作为替代方法,您可以调用 @isset 以避免在 PHP 错误日志中出现警告.
一个严肃的程序代码会使用更复杂的函数集来检查用户是否登录.

Above,you would have an empty string in $usermenu if $_SESSION and $_SESSION['user_name'] are not set.
As an alternative, you can call @isset to avoid warnings in your PHP error log.
A serious program code would use more complex set of functions to check if the user is logged in.

这篇关于确定是否在 PHP 的包含文件中设置了变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆