Javascript to verify domain, then launch a different website

Posted on 2011-02-18
Last Modified: 2012-05-11
Scenario - our clients can run our web pages from THEIR websites, as though our page were on their server.  

Objective - I would like to come up with some code that I can give our client to put on their website that will launch one of our pages. Once there, they can navigate around our other pages.    The code should verify that it's coming from the client's domain (so it can't be stolen)

An example, may be something like Google Maps.   I can add google maps to my website by installing the js that google provides me.  If I try to run the same script from a different server, it will create an error.  I know some menu code that you purchase can do the same thing - error if tried to run from the wrong website.

So, my question has two parts.
what js code can I give a client that will (1) ensure it is run from a particular domain
and (2) open up one of our pages as though it is on their site.

Is  window.location.hostname   a reliable way to verify the domain?
Question by:gdemaria
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
LVL 52

Assisted Solution

_agx_ earned 250 total points
ID: 34931702
Unfortunately my javascript-foo isn't nearly that strong ;-) The closest I've seen to deciphering how google maps works is this thread.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34931839
That isn't usually done with javascript.  It is done on the server with a server language like PHP or ASP that can read the 'Server Variables'.  The requesting server IP is available because the server has to know where to send the request back to.

The code would go on your server to verify that the request is coming from an acceptable place.
LVL 39

Author Comment

ID: 34933276
agx - thanks for the link, it's hard to find info out there and that helped. Just wish it had more detail

Dave - thanks for you reply.  This is a bit different than you are thinking.  Google Maps, for example, doesn't ask for our server's IP address.  I don't want to allow only our Client to view the pages, I want the world to view the page IF it's launched by the client's website.  Locking down the server info or IP would prevent others from seeing it.   The js I would like to create authenticates the fact that our page is launched by the correct domain and then is available to the world to view.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 34933611
"window.location.hostname" is not used in the Google Maps javascript.  I think the article linked by @_agx_ explains how the HTTP_REFERER sent to the server can be checked against the 'key' in their database.
LVL 39

Accepted Solution

gdemaria earned 0 total points
ID: 34933683

This seems to be a great article on the subject

The link should bring you down to a section called : URL-Based API Key Restriction
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34933699
Yes, that's a good description of what I was trying to say.  The 'key' gets checked against the 'HTTP_REFERER' in the database on the server when the file request is made.  It is not done by javascript alone.
LVL 52

Expert Comment

ID: 34934167
I'm still reading, but that's probably the most detailed article I've seen on the subject!
LVL 39

Author Closing Comment

ID: 35106981
For the benefit of the archives I am assigning my post as the "solution" but noting both of your contributions and splitting points between you.  Thanks for your help!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Echo'd values in dropdowns 6 29
[form-control] Retain values after a POST action 21 33
Slush on text 2 11
Table header must be on top 2 27
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question