doukunsan5553 2017-03-21 20:44
浏览 97
已采纳

如何在php中为if语句添加多个条件

I'm working on a project in PHP. And in session.php I allow which users can access each page. Logged-in users and other. But the problem is that I can't add all the id-s from the table in my database to the if statement. Because I would like that all 'oglasi' should be accessible by all users.

        <?php
        include_once 'db.php';
        session_start();
        ob_start();

        $query = "SELECT id FROM oglasi";
        $result = mysqli_query($link, $query);

        while ($row = mysqli_fetch_array($result)) {
        $id = $row['id'];
        //if not logged in you can access
        if (empty($_SESSION['user_id']) &&
                $_SERVER['REQUEST_URI'] != '/SP/oglasi/index.php' &&
                $_SERVER['REQUEST_URI'] != '/SP/oglasi/registration.php' &&
                $_SERVER['REQUEST_URI'] != '/SP/oglasi/oglasi.php' &&
                $_SERVER['REQUEST_URI'] != '/SP/oglasi/oglasi_show.php?id='.$id &&
                $_SERVER['REQUEST_URI'] != '/SP/oglasi/user_insert.php' &&
                $_SERVER['REQUEST_URI'] != '/SP/oglasi/login_check.php') 
            header("Location: index.php");
            die();
        }
          }

        ?>

This is the key statement

    $_SERVER['REQUEST_URI'] != '/SP/oglasi/oglasi_show.php?id='.$id &&

$id variable should change 1,2... But it is not changeing.

If I do this it works, but it is not good if you have a lot of data.

    $_SERVER['REQUEST_URI'] != '/SP/oglasi/oglasi_show.php?id=2' &&
    $_SERVER['REQUEST_URI'] != '/SP/oglasi/oglasi_show.php?id=1' &&

Can someone help ?

  • 写回答

1条回答 默认 最新

  • doupishan3309 2017-03-21 20:52
    关注

    I suppose you can simplify check this way:

    include_once 'db.php';
    session_start();
    ob_start();
    
    //if not logged in you can access
    if (empty($_SESSION['user_id']) &&
        strpos($_SERVER['REQUEST_URI'], '/SP/oglasi/') !== 0) {
        header("Location: index.php");
        die();    
    }
    

    Here you check - if pattern /SP/oglasi/ not exists in your REQUEST_URI at position 0 (which means REQUEST_URI starts with it) - then you redirect user to index.php.

    If not all oglasi subsections can be visited by user - you can still use your condition:

    include_once 'db.php';
    session_start();
    ob_start();
    
    if (empty($_SESSION['user_id']) &&
        $_SERVER['REQUEST_URI'] != '/SP/oglasi/index.php' &&
        $_SERVER['REQUEST_URI'] != '/SP/oglasi/registration.php' &&
        $_SERVER['REQUEST_URI'] != '/SP/oglasi/oglasi.php' &&
        $_SERVER['REQUEST_URI'] != '/SP/oglasi/user_insert.php' &&
        $_SERVER['REQUEST_URI'] != '/SP/oglasi/login_check.php' &&
        strpos($_SERVER['REQUEST_URI'], '/SP/oglasi/oglasi_show.php') !== 0  
    ) { 
        header("Location: index.php");
        die();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题