1

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>";

?>

Question:

How can I stop $comn_alert from executing on 1.php & 2.php?

7
  • In this case, alert can go into a separate file and be included conditionally. Anyway, what does __FILE__ give you when you add this in default.php? Commented Jun 27, 2020 at 14:06
  • Or you can do this current URL check in javascript itself. Commented Jun 27, 2020 at 14:07
  • @vivek_23 "In this case, alert can go into a separate file and be included conditionally" so I need to add to 98 files individually? Also, "what does __FILE__ give you", I didn't get this. Commented Jun 27, 2020 at 14:18
  • 1
    PHP doc says for _FILE__: "If used inside an include, the name of the included file is returned.", so I'd guess it wouldn't work Commented Jun 27, 2020 at 14:18
  • 1
    @vivek_23 Do you mean including default in 1 & 2? If so, yes I can. Commented Jun 27, 2020 at 14:26

2 Answers 2

1

Several approaches:

  • You could use $_SERVER['PHP_SELF'] in default.php to check if it's included from either 1.php or 2.php
  • You could restructure your inclusion structure a little bit: Extract the common logic of 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
  • Set a variable in 1.php and 2.php before requiring and then check in default.php for the existence / absence of this variable.
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I thought of the 3rd way where OP can define a constant before including the file and then check if that constant is defined in default.php.
Can you please expand your answer (small code) for point 1 & 3. And what are the merits and demerits of them? I'll not go with point 2 as in future if I need to change some codes in default, I'd have to change in the other one too.
0

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>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.