2017-05-04 55 views
-1

后,我要让我的更改默认菜单登录通过后 和 我有一些代码对我的应用程序是这样PHP制作隐藏和显示菜单登录

的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 C:\ XAMPP \ htdocs中\模板\的index.php上线78

只是process.php

定义为什么它不工作的loggedIn?

+0

哪里的html表单为此? –

+0

'$ _SESSION ['loggedIn'] ='0';'如果。 'mysql_'函数已被弃用。你必须照顾SQL注入..等..等 –

+1

当然希望你打算采取这种生活。你现在拥有的是完全不安全的。 –

回答

0

你要测试如果您的会话与isset()存在,那么如果它是良好的价值:-)

if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == '1'){ ... } 

顺便说一句,mysql_ *已被弃用:你应该使用PDO或mysqli的 看一看:Why shouldn't I use mysql_* functions in PHP?

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

+0

感谢它现在工作XD –

+0

没有问题:)不要忘记把我的答案作为“已解决”,如果它帮助你^^ –