Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if php file is being called within a src="..."?

In my website I have

<script src="js.php"></script>

Question is very simple but I have no idea of the answer:

Within js.php, how can I check if the file has been called though a script src="..."?

Purpose is to change the returned HTML code of js.php depending on how this php script file is called (direct access or script src="...").

like image 747
user2824618 Avatar asked Jan 25 '26 04:01

user2824618


1 Answers

The way to do it would be to assign a session variable to true right before you call the js.php file

session_start();
$_SESSION['src'] = true;
<script src="js.php"></script>

Then in the php file

session_start();
if(isset($_SESSION['src']) && $_SESSION['src'] == true) {
    // file was called from a src
    $_SESSION['src'] = false; // this is important so that it can't be called from direct access
}
like image 82
Ali Avatar answered Jan 27 '26 16:01

Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!