Identify which page requests another via require_once

Asked

Viewed 31 times

0

I have a header.php, where is contained my page bar, which in turn has the breadcrumb and also what I call "Toolbar" which are the action buttons related to the page being accessed. For example:

When the user accesses the "Customers" module, he will access client-list.php, that makes a require_once in the header.php.

The problem is that each module will have different buttons, just as the user will have permissions. So while accessing client-list.php, the header.php shall display relevant buttons to client-list.php, also considering the permissions.

How can I make mine header.php identify which page/module is calling it through require_once to show information relevant to this page/module?

  • Use variables from session PHP, so you can store data between pages.

1 answer

2


You can in all page scripts create a constant like this:

<?php
define('PAGINA', basename(__FILE__));

require_once 'header.php';

And in the header.php use if, switch or anything else, something like:

<?php
switch (PAGINA) {
    case 'client-list.php':
        //Botões especificos para client-list
        break;

    case 'contato.php':
        //Botões especificos para contato
        break;

}

Using $_SERVER['SCRIPT_FILENAME']

To simplify/facilitate can also the variable $_SERVER['SCRIPT_FILENAME'] within the header.php and you won’t need to do/add anything in the other scripts:

<?php
$pagina = basename($_SERVER['SCRIPT_FILENAME']);

switch (PAGINA) {
    case 'client-list.php':
        //Botões especificos para client-list
        break;

    case 'contato.php':
        //Botões especificos para contato
        break;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.