检查登录和会话

check login and sessions

我正在 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>

Error: Trying to get property of non-object in /var/www/html/tinyforum/showForums.php on line 62

session.php

<?php
session_start();

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

我预计 if 语句不会被执行

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

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

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