Tibby
Tibby

Reputation: 344

PHP How to get the url of the page where include is used

I have an Index.php with this line:

<?php echo (file_get_contents("http://www.mydomain.com/includes/header.php")); ?>

In the header.php I would like to include a DIV secion only if the visitor is on www.mydomain.com/index.php on the rest I would like to Hide this.

I figured to use $_SERVER['SERVER_NAME'] and REQUEST_URI to get the location and then use an IF to check if the page is index.php ELSE dont show the DIV section.

The problem is, if I get it into the header.php I get the url to be www.mydomain.com/includes/header.php insted of www.mydomain.com/index.php

Any Idea how to get over this problem?

Upvotes: 0

Views: 99

Answers (2)

larsAnders
larsAnders

Reputation: 3813

You could use define a variable based on $_SERVER['SCRIPT_NAME'] variable. So, within the index.php file, you would define it first, and then include the header file.

$thisPage = $_SERVER['SCRIPT_NAME'];
include('includes/header.php');

Then, in the header.php file, an if statement:

if ($thisPage == 'index.php'){
    //all the code
    //echo this, that, and the other
}

Upvotes: 0

CLHardman
CLHardman

Reputation: 128

Is there a specific reason why you want to use file_get_contents? You could use include_once('header.php');. in your index.php file. Determining if you are on index.php page you can use in your header.php file

if(basename($_SERVER['PHP_SELF']) == "index.php") { 
    /* show whatever you need */
}

Upvotes: 1

Related Questions