Reputation: 85
I have one common page: default.php
<?php
________Some Codes________
?>
And my project has 100+ .php files. (Yes all files have require_once 'default.php';
on the first line of all pages)
Now, I am deciding to display alert in all the files except 2. (say 1.php & 2.php).
Of course, I'll add that alert in my default.php.
So now my default.php will look like:
<?php
________Some Codes_______
echo $comn_alert = "<script>alert('Hi');</script>";
?>
How can I stop $comn_alert
from executing on 1.php & 2.php?
Upvotes: 1
Views: 92
Reputation: 17805
Define a constant before including the file and then check if that constant is defined in default.php
.It would be something like below:
1.php:
<?php
define('AVOID_ALERT_1',1);
require_once('default.php');
2.php:
<?php
define('AVOID_ALERT_2',1);
require_once('default.php');
default.php:
<?php
// __some__codes
if(!(defined('AVOID_ALERT_1') || defined('AVOID_ALERT_2'))){
echo $comn_alert = "<script>alert('Hi');</script>";
}
Upvotes: 0
Reputation: 1391
Several approaches:
$_SERVER['PHP_SELF']
in default.php
to check if it's included from either 1.php
or 2.php
default.php
(i.e. the logic used in all files) into another file (e.g default_minimal.php
) and make default.php
require that file and contain the code in question (e.g. the alert()
). Make then 1.php
and 2.php
only require default_minimal.php
1.php
and 2.php
before requiring and then check in default.php
for the existence / absence of this variable.Upvotes: 1