How can I sanitize my include statements?

前端 未结 4 1352
孤街浪徒
孤街浪徒 2021-01-20 19:47

How do I clean this so users can\'t pull pages outside of the local domain?



        
4条回答
  •  孤街浪徒
    2021-01-20 20:08

    The safest way is to whitelist your pages:

    $page = 'home.php';
    
    $allowedPages = array('one.php', 'two.php', ...);
    
    if (!empty($_GET['page']) && in_array($_GET['page'], $allowedPages))
        $page = $_GET['page'];
    
    include $page;
    

提交回复
热议问题