2

I'm learning php on my own now and I'm developing some simple sites using php include to ease the page creation process. I've searched this website for ways to make it secure but, as a noob, I'm always afraid of messing up.

<?php
    $siteArticles = array('instalacoes','galeria','regiao-e-historia','precos','contactos');
    if( isset($_GET['page']) ){
        if( in_array($_GET['page'], $siteArticles, true) && file_exists('pt/'.'rbs-article-'.$_GET['page'].'.php') ){ 
            include('pt/'.'rbs-article-'.$_GET['page'].'.php'); 
        }
    }else{
        include('pt/rbs-article-home.php');
    }
?>

As you can see, it first checks if the page's allowed through the array and then add a prefix to the name file.

My question is, how secure is this?

Thank you for your time.

1 Answer 1

3

It is secure. The in_array() check is what makes it secure. It is not possible to perform a Local File Inclusion (LFI) attack on this code simply because the requested page must exactly match one of the elements in the whitelist array.

Sign up to request clarification or add additional context in comments.

1 Comment

Just because I'm paranoid, I'd still use array_search and get the page name using the index instead of using $_GET['page']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.