2016-09-06 33 views
0

我有4个文件home.php,log_out.php,blank_one.php,connection.php 并没有包括login.php和index.php,因为它只读取 用户名和密码。会话破坏在登录和禁用回

我的问题是会话单击注销

后,然后单击箭头回

blank_one.php直接我得到这些错误:'(

  • 注意:未定义索引:当前用户

  • 注意:未定义指数:CurrentUserType

  • BLANK ONE

========================= ===============================================

并且由于注销(log_out.php)被点击的内容必须是没有用户发现,EMPTY和back应该被禁用。

有没有办法处理这个会话,以消除它后的错误?

`请帮忙。 :'(

blank_one.php

<?php 
 
    session_start(); 
 
    $currUser = $_SESSION["CurrentUser"]; 
 
    $currUserType = $_SESSION["CurrentUserType"]; 
 
    echo('BLANK ONE'); 
 
    ?>

home.php

<?php 
 
\t session_start(); 
 
\t $currUser = $_SESSION["CurrentUser"]; 
 
\t $currUserType = $_SESSION["CurrentUserType"]; 
 
\t if($currUserType == '1' or $currUserType == '2') \t 
 
\t { 
 
\t \t echo ' 
 
     <html> 
 
     <body> 
 
     <a href="blank_one.php">blank</a> 
 
     <a href="log_out.php">logout</a> 
 
     
 
     </body> 
 
     </html> 
 
     '; 
 
\t \t \t 
 
\t } 
 
\t 
 
\t else if($currUserType == '2'){ 
 
\t 
 
\t } 
 
\t 
 
\t else if($currUserType == '3'){ 
 
\t \t echo ' 
 
\t \t 
 
\t \t '; 
 
\t \t 
 
\t }else{ 
 
\t \t echo '<div> no user found </div>'; 
 
     
 

 
\t } 
 
\t \t 
 
?>

connection.php

\t \t <?php 
 
\t \t $conn = mysql_connect('localhost', 'root', '', 'life'); 
 

 
\t \t \t if (!$conn) 
 
\t \t \t { 
 
\t \t \t \t die('Connect Error: ' . mysql_errno()); 
 
       session_destroy(); 
 
       session_start(); 
 
\t \t \t } 
 
\t \t \t 
 
      else 
 
\t \t \t { 
 
\t \t \t \t //echo ("connected from connection.php"); 
 
       session_start(); 
 
\t \t \t \t echo (""); 
 
\t \t \t } 
 
\t \t ?>

log_out.php

\t \t <?php 
 
\t \t session_start(); 
 
\t \t include('connection.php'); 
 
\t \t $conn = mysql_connect('localhost', 'root', '', 'wildlife'); 
 
\t \t 
 
\t \t if ($conn) 
 
\t \t { 
 
\t \t \t $update=mysql_query("INSERT INTO wrd_user(emp_log_out) VALUES (now())"); 
 
\t \t session_destroy(); 
 
\t \t mysql_close(); 
 
\t \t \t header('Location:index.php'); 
 
\t \t } 
 
\t \t else 
 
\t \t \t { 
 
\t \t \t \t 
 
\t \t \t \t echo (""); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t 
 
\t \t ?>

回答

1

包装你的变量设置代码blank_one.php将解决这个问题:

<?php 
session_start(); 
if (isset($_SESSION["CurrentUser"])) { 
    $currUser = $_SESSION["CurrentUser"]; 
} 
if (isset($_SESSION["CurrentUserType"])) { 
    $currUserType = $_SESSION["CurrentUserType"]; 
} 
echo('BLANK ONE'); 
?> 

你需要相同的home.php了。

+0

HI

+0

你的意思是这样??? for home.php –

+0

如果单独使用它们,那么是的,如果将它们绑定在一起,那么您可以使用单个if语句来检查它们。请在评论中使用代码格式。您可以在新行和4个空格之后将代码写入您的评论。 – szenbalu

1

您应该测试会话变量(使用登录页面创建)是否设置为验证用户是否已登录。

blank_one。PHP

<?php 
session_start(); 
if((isset($_SESSION['CurrentUser']) &&(isset($_SESSION['CurrentUserType'])) { 
    $currUser = $_SESSION["CurrentUser"]; 
    $currUserType = $_SESSION["CurrentUserType"]; 
} 
else { 
    echo('BLANK ONE'); 
} 
?> 

这应该也做在保护的页面,以避免错误和安全问题直接URL访问时,在不登录的用户,而且用户应该在这种情况下被重定向。

home.php

<?php 
    session_start(); 
     if((isset($_SESSION['CurrentUser']) &&(isset($_SESSION['CurrentUserType'])) { 
     $currUser = $_SESSION["CurrentUser"]; 
     $currUserType = $_SESSION["CurrentUserType"]; 
     } 
     else 
     { 
      header('Location:index.php'); 
      die(); 
     } 
    if($currUserType == '1' or $currUserType == '2')  
    { 
     echo ' 
     <html> 
     <body> 
     <a href="blank_one.php">blank</a> 
     <a href="log_out.php">logout</a> 

     </body> 
     </html> 
     '; 

    } 

    else if($currUserType == '2'){ 

    } 

    else if($currUserType == '3'){ 
     echo ' 

     '; 

    }else{ 
     echo '<div> no user found </div>'; 
    } 
?>