Search code examples
phpmysqlphp-8php-8.1

Why PHP include method do not work on these codes?


I didn't understand it why happens. When I try to get other errors they are including to the page so I can see the error but 404.php couldn't included.

<?php
include('views/nav.php');
if (isset($_GET['id'])){
    $id = preg_replace('#[\s]+#',' ',htmlspecialchars($_GET['id'], ENT_QUOTES, 'UTF-8'));
    if ( is_numeric($id) ) { 
        try{
            $query->execute();
            $blogs = $query->fetchAll();
            if( empty($blogs) ) {
                include('error_pages/404.php'); # DOESN'T WORK
            } else {
                foreach ($blogs as $blog) {
                    include('views/article.php');
                }
            }
            // Close connection.
            $conn = null; 
        } catch(PDOException $e) {
            include('error_pages/500.php'); # THIS ONE IS WORKS.
            // echo "Connection failed: " . $e->getMessage();
        }
    } else {
        include('error_pages/400.php'); # THIS ONE IS WORKS.
    }
} else {
    include('views/blog.php');
}
include('views/footer.php');
?>

I tested these steps;

  1. I broke the query so I get 500 error. That include is working.
  2. I drop a ' to the dynamic link, like www.domain.com/blog.php?id=' so I get 400 error. That include is working too.
  3. But when I drop an another id for example '?id=2' (there is no 2 id row on database) it is not giving 404 so that include is not working. I need help.

Solution

  • Finally I found the solve. I was created a directory on web project it was /error_pages/ and it's owner was me and the group of it was www-data (I'm working on NGINX). I wanted to go and check for permissions. When I type the 'ls -l' it returned the 'owner:owner' value. You need to to that for NGINX;

    1. Go to parent of your web project. (www for mine; (/var/www/domain.com))
    2. In the www folder execute this command: sudo chown -R YOUR_USER:www-data YOUR_PROJECT_FOLDER

    Execute the following command on terminal. This command will set group of your web project as www-data.

    Problem solved ^-^