• 0

[PHP] Iframes help


Question

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

5 answers to this question

Recommended Posts

  • 0

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

  • 0
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

  • 0

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

  • 0
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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.