SE13013
SE13013

Reputation: 343

How do I only allow require_once things depending on which page 'required' it?

I just had an idea to make things a little cleaner and less cluttered. And I'm wondering if it's at all possible to determine which page is requesting access to another PHP file?

Index.php:

<?php
    require_once('RequiredThing.php');
    // ...
?>

RequiredThing.php:

<?php
    if(INDEX-PAGE REQUESTED THIS)
    {
        // Do stuff.
    }
    else if(ABOUT-PAGE REQUESTED THIS)
    {
        // Do diffrent stuff.
    }
?>

I hope this is making sense.

Upvotes: 0

Views: 44

Answers (2)

Tariq hussain
Tariq hussain

Reputation: 614

Just a little trick to do this . i hope this will help . before require just define $page var.

about.php

  <?php
    $page = 'about';
    require 'RequiredThing.php';
    ?>

Index.php

   <?php
     $page = 'index';
    require 'RequiredThing.php';

RequiredThing.php

if($page == 'about'){
    // Do stuff. 
}elseif($page == 'index'){
    // Do diffrent stuff
}

?>

Upvotes: 1

Saedawke
Saedawke

Reputation: 471

Try this

<?php
if(basename(__FILE__, '.php') === "index")
{
    // Do stuff.
}
else if(basename(__FILE__, '.php') === "about")
{
    // Do diffrent stuff.
}

?>

Upvotes: 1

Related Questions