PHP在登录后生成隐藏和显示菜单

PHP在登录后生成隐藏和显示菜单,php,Php,我想在登录通过后更改默认菜单 及 我的应用程序有这样的代码 index.php <?php session_start(); ?> <html> <head></head> <body> <ul> <?php if($_SESSION['loggedIn'] == '1' ): ?> <li><a href="../templates/" title="Home" id="activate

我想在登录通过后更改默认菜单 及 我的应用程序有这样的代码

index.php

<?php session_start(); ?>

<html>
<head></head>
<body>
<ul>
 <?php if($_SESSION['loggedIn'] == '1' ): ?>
<li><a href="../templates/"  title="Home" id="activated">Home</a></li>
<li><a href="catalog.html" title="Catalog" >Catalog</a></li>
<li><a href="signup-for-partners.html" title="Partners">Become Our Partner</a></li>
<li><a href="About-us.html" title="About Us">About Us</a></li>
   <?php else: ?>
   <li><a href="../templates/"  title="" >Home</a></li>
   <li><a href="account.html"  title="" >My Account</a></li>
   <li><a href="catalog.html" title="" >Catalog</a></li>
   <li><a href="logout.php" title="" >logout</a></li>
   <?php endif; ?>
  </ul>

</body> </html>

这是我使用验证码的过程

<?php

session_start();
include 'conection.php';


if($_POST['captcha'] == $_SESSION['captcha']){

    $username = $_POST['UserID'];
    $password =$_POST['Password'];

    $sql = "select * from login where username='".$username."' and password='".$password."'";
    #echo $sql."<br />";
    $query = mysql_query($sql) or die (mysql_error());

    if($query){
        $row = mysql_num_rows($query);

        if($row > 0){
            $_SESSION['loggedIn']='1';
            $_SESSION['UserID']=$username;
            header('Location: ../templates/');
        }
        else {
            header('Location: error.php');
        }
    }
}
else{ 

header('Location: ../templates/');


}
?>

它的错误是这样的

注意:未定义索引:loggedIn in in 第78行的C:\xampp\htdocs\Templates\index.php

刚刚在process.php中定义的loggedIn


为什么它不工作?

您必须使用
isset()
测试会话是否存在,然后它是否是正确的值:-)


顺便说一句,mysql_*已被弃用:您应该使用PDO或Mysqli 看看:


if($var==1)
和写
if($var)
是一样的:1可以是
true
,“1”可以是
string
,或者1可以是
int

这个会话的html表单在哪里?
$\u SESSION['loggedIn']='0'在if之前<代码>mysql
函数已弃用。您必须注意SQL注入。。等等等,希望你打算现场直播。你现在所拥有的是完全不安全的。谢谢它现在可以工作了(没有问题:)如果它对你有帮助的话,别忘了把我的答案写为“已解决”^^
if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == '1' ){ ... }