简体   繁体   English

用php登录时隐藏div

[英]Hiding a div upon login with php

I am trying to hide a div once a user has logged into there account of my online store (built with cartweaver 4 php) but i just cant seem to get it to work. 一旦用户登录到我的在线商店(使用cartweaver 4 php构建)的帐户,我就试图隐藏div,但是我似乎无法正常工作。

the php code for defining wether the user is logged in or not is as followed: 定义用户是否登录的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"))

and the div i would like to hide upon login, which contains a table, have been given an id and class of: 我想在登录时隐藏的div(包含一个表)的ID和类为:

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

I have tried applying this method using css: Show Hide div if, if statement is true 我尝试使用CSS应用此方法: 如果语句为true,则显示隐藏div

but that ended up giving me an undefined variable error on my testing server. 但这最终在测试服务器上给了我一个未定义的变量错误。

I admit i am a bit of a php newbie, so if anyone who is able to make more sense of this could help out and possibly find a solution it would be much appreciated. 我承认我有点像php新手,所以如果有人能够对此有更多的了解,可以为您提供帮助并找到解决方案,将不胜感激。

Thank you. 谢谢。

You've got at least 3 variables there that could be throwing your undefined variable warning: 您至少有3个变量可能会引发未定义的变量警告:

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

you need to isset() each of those before doing any tests. 您需要在进行任何测试之前对每个参数进行isset()处理。

For starters, the strlen function in PHP returns an INT, not a boolean. 对于初学者,PHP中的strlen函数返回INT而不是布尔值。

Change from: 更改自:

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

to: 至:

(strlen($_SESSION["cwclient"]["cwCustomerID"]) > 0)

First of all could that not just be simplified to the following if statement to make it easier 首先,这不仅可以简化为以下if语句,以使其更容易

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

Second of all to hide the div if they are logged in you could do it like this 第二,如果他们已登录,则隐藏div,您可以这样做

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

Although if the content is a security risk it'd be better to not output it at all as it's still visible in the source code. 尽管如果内容存在安全隐患,最好还是完全不输出,因为它在源代码中仍然可见。

<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>
<?  
}
?>

Make sure that the condition check is AFTER the div element getting rendered. 确保在div元素被渲染之后进行条件检查。 Because if you put it before the element the page would not have fully rendered and so the script will not be able to get the div that you want to hide. 因为如果将其放在元素之前,页面将无法完全呈现,因此脚本将无法获取要隐藏的div。

REVERSE Condition based on OP's request. 根据OP的请求,条件为REVERSE。 I know this is a kluge, folks. 伙计们,我知道这是克鲁格。 Dont flame me! 不要火焰我!

<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>
<?  
}
?>

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

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