Ken2k7 Posted January 4, 2008 Share Posted January 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/ Share on other sites More sharing options...
mrdamien Posted January 5, 2008 Share Posted January 5, 2008 I think the HTTP_REFERER is what your looking for. Note: It can't always be trusted. Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430838 Share on other sites More sharing options...
Ken2k7 Posted January 5, 2008 Author Share Posted January 5, 2008 I was thinking of that, but how would I display that URL out on index.html? Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430841 Share on other sites More sharing options...
mrdamien Posted January 5, 2008 Share Posted January 5, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430845 Share on other sites More sharing options...
Ken2k7 Posted January 5, 2008 Author Share Posted January 5, 2008 Think of it as checking where test.php is being called from. Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430846 Share on other sites More sharing options...
mrdamien Posted January 5, 2008 Share Posted January 5, 2008 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430848 Share on other sites More sharing options...
Ken2k7 Posted January 5, 2008 Author Share Posted January 5, 2008 I'm guessing I have to define $referer? Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430849 Share on other sites More sharing options...
mrdamien Posted January 5, 2008 Share Posted January 5, 2008 its predefined in $_SERVER['HTTP_REFERER'] http://de2.php.net/reserved.variables Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430851 Share on other sites More sharing options...
Ken2k7 Posted January 5, 2008 Author Share Posted January 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84558-solved-url-detection/#findComment-430855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.