0

So I just started learning PHP and I was making a local project to test it out.

I'm using the built in webserver https://www.php.net/manual/en/features.commandline.webserver.php using the command php -S localhost:8000 router.php

And I wanted to navigate to my test.php file which is next to my index.php file at http://localhost:8000/test

But it only works if I use http://localhost:8000/test.php

On the in built webserver page it mentioned how to serve a .el file

So from that I came up with

<?php
// router.php
$pathName = $_SERVER['REQUEST_URI'];
$pathName = str_replace("/", "\\", $pathName);
$file =   __DIR__ . $pathName . ".php";
if(file_exists($file)){       
           include($file);
       }else{
           return false;
       }
?>

But it doesn't seem right.

How would I go about doing it the right way? Can anyone point me in the right direction? Couldn't find anything on stackoverflow but it's probably just because I don't know what to search for.

EDIT: I do want the include functionality and not readfile, I was just wondering if the approach to checking whether the .php file was there was the right one.

Hafax
  • 369
  • 2
  • 14
  • 2
    @Scoots Except you cannot use an `.htaccess` file when using the builtin web server :) – RiggsFolly Jun 29 '20 at 12:11
  • @RiggsFolly Ah; my bad. I've never used the builtin. – Scoots Jun 29 '20 at 12:12
  • 1
    This may be helpful https://stackoverflow.com/questions/27381520/php-built-in-server-and-htaccess-mod-rewrites Or it may in fact be a DUP – RiggsFolly Jun 29 '20 at 12:12
  • @RiggsFolly doesn't seem to be the same question, but it does seem I'm on the right path using that router.php file to achieve what I'm trying to do. – Hafax Jun 29 '20 at 12:23

1 Answers1

1

Your router.php is almost correct.

If you want to show the content of the php file, use readfile like this:

<?php
// router.php
$script = $_SERVER["REQUEST_URI"];
$path = pathinfo($script);
if (empty($path["extension"])) {
    $php = ".$script.php";
    if(file_exists($php)){
        readfile($php);
    }else{
        return FALSE;
    }
}else{
    return FALSE;
}
?>

This will return the content of the PHP file when access without extension and it exists. And run as normal when it does not exist or access via extension.

Or, if you want to run the php file, use include like this:

<?php
// router.php
$script = $_SERVER["REQUEST_URI"];
$path = pathinfo($script);
if (empty($path["extension"])) {
    $php = ".$script.php";
    if(file_exists($php)){
        include($php);
    }else{
        return FALSE;
    }
}else{
    return FALSE;
}
?>

This will return the output of the PHP script when access without extension and it exists. And run as normal when it does not exist or access via extension.

BTW, use the built-in server ONLY for test, not to the public for security.

tgarm
  • 473
  • 3
  • 8
  • I did want the "include" functionality, but your approach to checking the whether the file is there seems better than my string replace approach. If you change readfile to include again I'll accept your answer. – Hafax Jun 29 '20 at 13:03