我正在尝试从头开始创建一个论坛,我有点纠结。在下面的代码中,我试图设置一个条件,即如果用户未登录,则他无法添加类别。但是,如果用户已登录,该代码可以完美运行,但我得到一个“未定义的索引:如果他没有登录,则为用户 ID。我尝试添加if(isset($_SESSION['userid']))
,但这只会隐藏“你必须登录”的通知。有什么好主意吗?
<?php
include("_/inc/dbcon.php");
$link = mysql_connect($host, $login, $pw);
mysql_select_db($database);
if($link){
}
include("_/inc/session_handler.php");
$create_cat ="";
if($_SESSION['userid'] == false /*| $_SESSION['rank'] !='Emperor' || $_SESSION['rank'] !='Destroyer' || $_SESSION['rank']!= 'Tekken Lord' )*/)
{
//the user is not an admin
echo 'Sorry, you do not have sufficient rights to access this page.';
}
else
{
//the user has admin rights
if($_SERVER['REQUEST_METHOD'] != 'POST')
{ echo "YOU HAVE THE RIGHTS!";
//the form hasn't been posted yet, display it
$create_cat .= '<form method="post" action="">
<input type="text" name="cat_name" placeholder="Category Name"/><br />
<input type="text" name="cat_description" placeholder="Category Description" /><br />
<input type="submit" value="Add category" />
</form>';
}
else
{
//the form has been posted, so save it
$sql = "INSERT INTO sp_categories(cat_name, cat_description)
VALUES('" . mysql_real_escape_string($_POST['cat_name']) . "',
'" . mysql_real_escape_string($_POST['cat_description']) . "')";
$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo 'Error' . mysql_error();
}
else
{
$create_cat .= 'New category succesfully added.';
}
}
}
?>