dow56114 2013-11-08 03:17
浏览 40

Php mysql if / else简写

Can anyone see what is wrong with this shorthand in relation to the problem below?

echo "<td><font size=1 color=#e4d6b5>" . ($row['tier']<$_SESSIONS['tier'] ? "ACCESS DENIED" : $row['contents']) . "</font></td>";

I have two tables in my database:

1.) members (id,username,email,password,salt,tier) <-tier is the user's security clearance level.

2.) opwire (category,contents,date,userid,seclevel) <-opwire stores user submitted data, userid is just a number that references the user who just submitted data to opwire. Seclevel is how high of a security clearance level (tier) a user needs to see that particular line of submitted data.

I'm attempting to get the currently logged in user to be granted or restricted access to 'contents' based on their security tier (referencing member's tier vs opwire's seclevel.) I'm also not entirely confident I'm using http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL

and I usually get the current user with:

$userId = $_SESSION['user_id'];

The entire table building php is below. Currently users with any tier can incorrectly view any seclevel contents when their tier should be restricting it.

<?php 

include_once 'functions.php';
include_once 'db_connect.php';
sec_session_start();

if(login_check($mysqli) == true) {

$con=mysqli_connect("localhost","mylogin","mypassword","mysqldatabase");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


function getColor($strOption)
{
   switch ($strOption)
   {
       case "Case 1":
       return "#cbae80";

       case "Case 2":
       return "#e59350";

       case "Case 3":
       return "#b7aaa4";

    }
}


$result = mysqli_query($con,"SELECT opwire.*,members.username FROM opwire 
          LEFT JOIN members on opwire.userid=members.id order by date DESC");

echo "<table border='1'>
<tr>
<th>Category</th>
<th>Contents</th>
<th>Date/Time</th>
<th>Operative</th>
</tr>";

while($row = mysqli_fetch_array($result))
 {
 echo "<tr>";
 echo "<td><font size=1 color='".getColor($row['category'])."'> " . $row['category'] . "</font></td>";
 echo "<td><font size=1 color=#e4d6b5>" . ($row['tier']<$_SESSIONS['tier'] ? "ACCESS DENIED" : $row['contents']) . "</font></td>";
 echo "<td><font size=1 color=silver>" . $row['date'] . "</font></td>";
 echo "<td><font size=1 color=gold>" . $row['username'] . "</font></td>";
 echo "</tr>";
 }
echo "</table>";

mysqli_close($con);

} else {
   echo 'Access to this area requires security clearance. <br/>';
}

?>
  • 写回答

1条回答 默认 最新

  • drd43058 2013-11-08 03:44
    关注

    I believe you would need to have "BREAK" at the end of each case, otherwise it will just fall all the way through to "#b7aaa4";

    function getColor($strOption)
     {
      switch ($strOption)
      {
       case "Case 1":
       return "#cbae80";
       break;
    
       case "Case 2":
       return "#e59350";
       break;
    
       case "Case 3":
       return "#b7aaa4";
       break;
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?