2017-05-04 53 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

沒有問題:)不要忘記把我的答案作爲「已解決」,如果它幫助你^^ –