Jump to content

Recommended Posts

Ohk, so I'm just wondering about one thing.

 

Is there a way to use PHP to detect where its script is used from?

 

Example:

I put: <script src='test.php'></script> in a page located index.html (same directory for now, but may not always be the case). Can test.php detect that it's being called from index.html?

Link to comment
https://forums.phpfreaks.com/topic/84558-solved-url-detection/
Share on other sites

I guess I don't understand your question.

 

Heres what I understood you wanted.

 

User vists site - Is forwarded to index.html

index.html uses <script> to include 'test.php'

test.php receives index.html as the referer.

 

What do you mean "display that URL out on index.html" ?

"index.html" is the referer...

Link to comment
https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430845
Share on other sites

Yes. Your other question does not.

 

For example, if you only wanted test.php to run if its included from index.html you could do something like:

 

<?
# this is just pseudo code
if($referer == "index.html"){
  // Your script goes here
}else{
  echo "Sorry, you cant use this file elsewhere";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430848
Share on other sites

So I would do something like this?

 

index.html

<script src='test.php'></script>

 

test.php

<?php
if ($referer == "index.html") echo "true";
else echo "false";
?>

 

I tried that but when I go to index.html, it displays a blank page :S

 

Edit: nevermind, solved. Stupid me. XD

Link to comment
https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430855
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.