0

一旦用户登录了我的在线商店的帐户(使用 cartweaver 4 php 构建),我试图隐藏一个 div,但我似乎无法让它工作。

用于定义用户是否登录的 php 代码如下:

if(!strlen($_SESSION["cwclient"]["cwCustomerID"]) ||
    $_SESSION["cwclient"]["cwCustomerID"] === 0 ||
    $_SESSION["cwclient"]["cwCustomerID"] === "0" ||
    $_SESSION["cwclient"]["cwCustomerType"] == 0 ||
    (isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
       strtolower($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))

并且我想在登录时隐藏的 div 包含一个表,已被赋予一个 id 和类:

  <div id="newcustomertable" class='newcustomer'>

我尝试使用 css 应用此方法:Show Hide div if, if statement is true

但这最终在我的测试服务器上给了我一个未定义的变量错误。

我承认我是一个 php 新手,所以如果任何能够更了解这一点的人都可以提供帮助并可能找到解决方案,我将不胜感激。

谢谢你。

4

4 回答 4

0

您至少有 3 个变量可能会引发未定义变量警告:

$_SESSION["cwclient"]
$_SESSION["cwclient"]["cwCustomerID"]
$_SESSION["cwclient"]["cwCustomerType"]

在进行任何测试之前,您需要 isset() 中的每一个。

于 2012-08-17T21:16:39.340 回答
0

对于初学者,PHP 中的 strlen 函数返回一个 INT,而不是布尔值。

更改自:

(!strlen($_SESSION["cwclient"]["cwCustomerID"])

到:

(strlen($_SESSION["cwclient"]["cwCustomerID"]) > 0)
于 2012-08-17T21:17:38.010 回答
0

首先,这不仅可以简化为以下 if 语句以使其更容易

 if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) { 
#code here 
}

其次,如果他们已登录,则隐藏 div 你可以这样做

     if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) { 
    $shouldhide="style='visibility:hidden;'";
    } else {
$shouldhide=""; 
}
echo"<div id='hidethis' $shouldhide>"; 

尽管如果内容存在安全风险,最好不要输出它,因为它在源代码中仍然可见。

于 2012-08-17T21:25:36.530 回答
0
<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) || 
!strlen(@$_SESSION["cwclient"]["cwCustomerID"]) ||
@$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
@$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(@$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?  
}
?>

确保条件检查是在 div 元素被渲染之后进行的。因为如果将它放在元素之前,页面将不会完全呈现,因此脚本将无法获取要隐藏的 div。

REVERSE Con​​dition 基于 OP 的请求。我知道这是一个kluge,伙计们。不要喷我!

<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) || 
!strlen(@$_SESSION["cwclient"]["cwCustomerID"]) ||
@$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
@$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(@$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
 //do nothing
}
else
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?  
}
?>
于 2012-08-17T21:52:58.813 回答