Furor Magus Posted February 26, 2009 Share Posted February 26, 2009 I have an iframe on another server and had a quick question about it... page in the iframe <?php $host = $_SERVER['HTTP_HOST']; $host = str_replace("www.", "", "$host"); echo $host ?> result = http://cosmicbattle.net/fmtest.html desired result: I would like the code to echo out cosmicbattle.net is there a way were we could make it so the above code will echo out cosmicbattle.net without this line of code. echo "cosmicbattle.net"; Link to comment Share on other sites More sharing options...
0 Wasacz Posted February 26, 2009 Share Posted February 26, 2009 The simpliest way to get a domain from basic URL string is to use regular expression, for example: <?php $s = 'http://www.cosmicbattle.net/fmtest.html'; $s = preg_replace('/^https?:\/\/(?:www\.)?(.+?)(?:\/.*)?$/i', '$1', $s); echo $s; // cosmicbattle.net Link to comment Share on other sites More sharing options...
0 Furor Magus Posted February 26, 2009 Author Share Posted February 26, 2009 The simpliest way to get a domain from basic URL string is to use regular expression, for example: <?php $s = 'http://www.cosmicbattle.net/fmtest.html'; $s = preg_replace('/^https?:\/\/(?:www\.)?(.+?)(?:\/.*)?$/i', '$1', $s); echo $s; // cosmicbattle.net what I am looking to do is put this Iframe on many websites and have it run a script based on the website it is on. for example: if on cosmicbattle.net run script 1 if on fmrpg.com run script 2 if on google.com run script 3 this way i can give out the iframe code and the person can put the iframe code on there site and it will auto configure to there site. the problem i am running into is it is displaying the page it is hosted on (fmrpg.com) not cosmicbattle.net which it is hosted on Link to comment Share on other sites More sharing options...
0 Wasacz Posted February 26, 2009 Share Posted February 26, 2009 You can embed this iframe with additional GET parameter, eg.: <iframe src="frame.php?site=cosmicbattle.net" (…)> Or in more „safe” way, using site-specific code: <iframe src="frame.php?site=qwerty" (…)> Then you can read this on the server the iframe runs on, using $_GET with specified key ('site' in here). Another option is to use JavaScript in your iframe to detect parent frame's hostname: var site = top.location.hostname; switch (site) { /* … */ } Link to comment Share on other sites More sharing options...
0 Furor Magus Posted February 26, 2009 Author Share Posted February 26, 2009 You can embed this iframe with additional GET parameter, eg.: <iframe src="frame.php?site=cosmicbattle.net" (…)> Or in more „safe” way, using site-specific code: <iframe src="frame.php?site=qwerty" (…)> Then you can read this on the server the iframe runs on, using $_GET with specified key ('site' in here). Another option is to use JavaScript in your iframe to detect parent frame's hostname: var site = top.location.hostname; switch (site) { /* … */ } what I would like to do is take option 3 because I want to make it so i can 1 give the same link to everyone and 2 make it so its less likely to be messed with and attempted to be hacked. also top.location.hostname was not picked on on my test it is acting like a blank variable. Link to comment Share on other sites More sharing options...
0 Wasacz Posted February 27, 2009 Share Posted February 27, 2009 See the following: http://stuff.wasacz.net/xhtml/iframe-location/ May be helpful. Link to comment Share on other sites More sharing options...
Question
Furor Magus
I have an iframe on another server and had a quick question about it...
page in the iframe
<?php $host = $_SERVER['HTTP_HOST']; $host = str_replace("www.", "", "$host"); echo $host ?>result = http://cosmicbattle.net/fmtest.html
desired result:
I would like the code to echo out cosmicbattle.net
is there a way were we could make it so the above code will echo out cosmicbattle.net without this line of code.
Link to comment
Share on other sites
5 answers to this question
Recommended Posts