Jump to content

php if else not working with main root / only working for index.php


Recommended Posts

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

How about something like this?
 

<?php 
$currentpage = $_SERVER['REQUEST_URI'];
if ($currentpage == '/' || $currentpage == '/index.php') {
    // Code for root or index.php page
} else {
    // Code for other pages
}
?>

 

Edited by Strider64
  • 2 weeks later...
On 12/9/2024 at 7:09 PM, ianhaney10 said:

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

Barand's solution is righ.The issue you're facing is likely because the URL behaves differently depending on whether index.php is explicitly mentioned. The code snippet you provided should work, but it can be simplified for clarity. Here's a refined version:

 

<?php
$currentpage = $_SERVER['REQUEST_URI'];

if ($currentpage == '/' || $currentpage == '/index.php') {
    // Code for root or index.php page
} else {
    // Code for other pages
}
?>

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.