I have some php code that says if index.php or main root then display some code and if not then show other code and it works on index.php but if it's just the domain name without index.php at the end, it don't work. I have the following code at the moment

<?php $currentpage = $_SERVER['REQUEST_URI'];
if($currentpage=='/' || $currentpage=="/index.php" || $currentpage=="index.php" || $currentpage=="" ) {
?>

Can anyone help me out please as I don't know what to amend for the code to work if main root and not have index.php at the end, the code should be the same for the main root / and index.php

The issue you’re encountering is likely related to how the web server handles URLs when the index.php file is omitted. When you visit a URL without explicitly typing index.php (e.g., www.example.com/), most web servers (like Apache or Nginx) automatically serve the index.php or index.html file from the root directory, but the request URI may not include index.php

Hello ianhaneybs,
It is time to start a bit of debugging , you need to inspect what the variable $currentpage really holds.
e.g. 1

<?php 
    $currentpage = $_SERVER['REQUEST_URI'];
    var_dump($currentpage);
    exit;
?>

But a better choice would be to start logging
e.g. 2

    <?php 
        $currentpage = $_SERVER['REQUEST_URI'];
        error_log("\n"."currentpage:".$currentpage,3,"error_log");
        ...
    ?>

But this error_log file will be where ever your php file is , probably public , which is not a great idea , lets make it a function

function log($msg)
{
    $logDir = getdirname($_SERVER['DOCUMENT_ROOT']);
    $bt = debug_backtrace();
    $caller = array_shift($bt);
    $header = $caller["file"].":".$caller["line"]." ".date("m/d/Y H:i:s")."\n";
    if(is_string($msg))
    {
        error_log("\n".$header.$msg,3,$logDir . "/log.txt");
    }
    else
    {
        error_log("\n".$header.print_r($msg,true),3,$logDir ."/log.txt");
    }
}

Now this log.txt will be one level up from where your document root is , so it will not be public (if you haven't set up anything else there). And you can use it like :

        <?php 
            $currentpage = $_SERVER['REQUEST_URI'];
            log("currentpage:".$currentpage);
        ?>

That way you can understand what is the flow of your execution , and you can add any other log as you like (just remember to remove them before production).

If you use Eclipse (or many other IDEs) you could use variables inspection with breakpoints , but this is not so easy and although I do it in other languages I rarely do it in PHP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.