检查登录和会话

时间:2015-07-15 21:50:52

标签: php session-cookies

我正在为简单的论坛制作API,现在尝试检查使用php登录

控制页面上的

:showForums.php

<?php require_once('session.php');?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TheForums</title>
</head> 
<body>

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('fourmsAPI.php');
/*
function tinyf_forums_get($extra ='')
{
    global $tf_handle;
    $query = sprintf("SELECT * FROM `forums` %s",$extra );
    $qresult = mysqli_query($tf_handle, $query);

    if (!$qresult)
        return NULL;
    $recount = mysqli_num_rows($qresult);
    if ($recount == 0)
        return NULL ;
    $forums = array();
    for($i = 0 ; $i < $recount ; $i++)
        $users[count($forums)] = mysqli_fetch_object($qresult);
    //mysql_free_result($qresult);

    return $forums;

}
*/
$forums = tinyf_forums_get();
if($forums == NULL)
{
    die('problem');
}
$fcount = count($forums);
if($fcount == 0)
{
    die('No Forums ');
}
if($_SESSION['user_info'] == false){
    echo '<a href = "login.php">Login!</a>';
}
else{
    $uname = $_SESSION['user_info']->name ;
    echo '<a href = "logout.php">'.$uname.' -- Logout!'.'</a>' ;
}
?>

<br/>

<ul type = "square">
<?php
for($i = 0 ; $i < $fcount ; $i++)
{
    $forum = $forums[$i];
    echo "<li><a href = \"forum.php?id=$forum->id\"> $forum->title <a/> <br/> $forum->desc --";
    if($_SESSION['user_info']->isadmin ==1){
        echo " <a href = \"deleteForum.php?id=$forum->id\"> Delete <a/> | <a href = \"modifyForum.php?id=$forum->id\"> edit <a/> " ;
    }
    echo "<br/>  </li>"; //$array -> 

}
?>  
</ul>   

</body>
</html>
  

错误:尝试在第62行的/var/www/html/tinyforum/showForums.php中获取非对象的属性

session.php文件

<?php
session_start();

if(!isset($_SESSION['user_info'])){
    $_SESSION['user_info'] = false ;
}
?>

我预计if语句不会被执行

1 个答案:

答案 0 :(得分:2)

如果循环执行,则必须执行if语句检查。内部部分没有按预期执行。您获得的错误来自条件检查。如果用户未登录,则代码等同于

if(null->isadmin ==1){
    echo " <a href = \"deleteForum.php?id=$forum->id\"> Delete <a/> | <a href = \"modifyForum.php?id=$forum->id\"> edit <a/> " ;
}

这显然会产生错误。您可以检查会话是先设置还是使用@运算符。