1

So I made a custom page handler to make my code much cleaner. I have it running on multiple websites but on the website I am working right now it doesn't work.

This is the code:

<?php
    $page = $_GET["p"];
    include_once("pages/" . $page . ".php");
    if($page == "") {
        header("Location: index.php?p=index");
    }
?>

and this is the error: Warning: include_once(): Failed opening 'pages/.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/ubuntu/workspace/admin_panel/page-handler.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at /home/ubuntu/workspace/admin_panel/header.php:474) in /home/ubuntu/workspace/admin_panel/page-handler.php on line 5

It works on all of the other websites I have it running on.

Does someone know why it isn't working? Thanks.

  • Seems like $_GET['p'] is empty. Also, you should be doing something to ensure the users can only reach the files you want them to. Perhaps create an array of valid pages, and check if $page is one of them. – rjdown Jul 30 '16 at 19:23
  • @rjdown I know that's why I made an if statement. And about the files, it's not needed. Its all in a different server – Rik Nijdeken Jul 30 '16 at 19:24
  • `if(!empty($_GET["p"])){ $page = $_GET["p"]; (do stuff/include) }` - basically, your logic is off. You're checking if equal to `""` and trying to include before the GET is set. – Funk Forty Niner Jul 30 '16 at 19:25
  • But Rik you are trying to include the page BEFORE your if statement! – rjdown Jul 30 '16 at 19:25
  • @rjdown Even if I inlude them after the if statement it doesn't work – Rik Nijdeken Jul 30 '16 at 19:27
  • and that header bit is caused by something else. Outputting before it somewhere. – Funk Forty Niner Jul 30 '16 at 19:27
  • 1
    @Fred-ii- Your answer worked for me, could you add is as an answer I don't want to take credit – Rik Nijdeken Jul 30 '16 at 19:30

1 Answers1

2

As requested by the OP:

Comment to answer:

if(!empty($_GET["p"])){ 

   $page = $_GET["p"]; 

   // (do stuff/include) 

}

Basically, your logic is off. You're checking if equal to "" and trying to include before the GET is set.

Check the following post also about the headers sent warning:

Plus, it is also best to add exit; after any header. Otherwise your code may want to continue to execute, should you have more below it.

Reference:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141