tags:

views:

533

answers:

3
+1  A: 

You cannot "guess" this information from your show_widget.php script : clientsite will have to transmit that information to your script.

Which means your widget should be included with some code like this (provided the page on clientsite is generated in PHP) :

<script 
    type="text/javascript" 
    src="http://widgetserver.com/show_widget.php?referer=&lt;?php echo urlencode($_SERVER['HTTP_REFERER']); ?>>
</script>

Of course :

  • don't forget to escape the referer while injecting it into the page, for security (and to get valid-HTML)
  • the referer might not always be present : it is sent by the user's browser, and can be both faked and/or disabled.

To avoid a "Notice: Undefined index: HTTP_REFERER" from being generated/displayed when there is no referer sent by the user's browser, you might also want to add a check, to find out if it's defined before using it -- use isset for that ; for instance, something like this might do :

<script 
    type="text/javascript" 
    src="http://widgetserver.com/show_widget.php?referer=&lt;?php echo isset($_SERVER['HTTP_REFERER']) ? urlencode($_SERVER['HTTP_REFERER']) : ''; ?>>
</script>
Pascal MARTIN
Thanks, but as you point out this assumes that the clientsite page is generated in PHP. Is there any way I could use javascript instead of php to transmit the referrer to "show_widget.php"?
Greg
+1  A: 

You could detect the referred on the main script of clientside.com and pass it as a variable to your script:

// clientsite.com script
<?PHP

    echo $main_page;
    $ref = $_SERVER['HTTP_REFERER'];
    echo '<script type="text/javascript" ' .
      'src="http://widgetserver.com/show_widget.php?r=' . $ref . '></script>';

?>

// widgetserver.com script
<?PHP
    $original_referrer = $_REQUEST['r'];
?>
Anax
+3  A: 

document.referrer contains the referrer information you want, so if they're running javascript, you could do something like this:

<script language="javascript" type="text/javascript">
var script = document.createElement("script");
script.src = "http://widgetserver.com/show_widget.php?r="+document.referrer;
document.getElementsByTagName("head")[0].appendChild(script);
</script>

This will need to go OUTSIDE of the head tag (unless it's in a function being bound to the document ready event or something), or it will fail.

The idea behind this is to generate a dynamic tag on the page with the attributes you want. The first line creates it, second line sets the script src, and the third page loads it into the document.head DOM element, which causes it to be evaluated, and loads the document as required.

If your clients are using jQuery, it is easier to use $.getScript, with a similar use of document.referrer

XwipeoutX
This seems like the right approach IMHO, the other answers all asume clientsite.com is PHP.
Jakub
Thanks - this looks like exactly what I was looking for. I'll give it a go.
Greg
@XwipeoutX, could you explain this approach in more detail? I tried the code above as is, but I get a blank screen. IT seems the DOM is replaced by a trivial page...<html><head/></html>
Greg
@Greg I added some extra info there for you, and i tested it locally, worked fine on my side. Double check you're evaluating it after the head tag, or in a document.ready event, that's my guess
XwipeoutX
@XwipeoutX, thanks, this clarifies what the code is doing. I think the problem is that my "show-widget.php" writes some HTML (the widget) using "document.write()" at the point in the file where I put this script. So loading the script into the document.head DOM element puts in the wrong place. Is there any way I can execute the javascript in place instead of adding it to document.head?
Greg
Yeah, just put it where you want to have it. <script>s do not need to be necessarily in <head>.
middus
it will be much more complicated for the user though. Make the client put a <span id="MyAwesomeUniqueId"></div> in the code, and then in your php script, use document.getElementById("MyAwesomeUniqueId").innerHTML = "The HTML I want to display here";Alternatively, replace the last line of the script in my answer with: document.getElementById("MyAwesomeId").appendChild(script);and I _think_ it will run it from there. I try to avoid document.write() as much as I can, domElement.innerHTML is much safer.
XwipeoutX
I created the span element with my ID and attached the script to that as you suggested and... it works! Fantastic!*Really* appreciate the help, XwipeoutX.
Greg