• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Referrer in HTML form

I have an order form in my web. The result from the form fields are then processed in a simople php script. I want to let another site use my order form, and need to know which orders come from this other site. Guess this may be done by a hidden form field, but I've never worked with hidden fields before. Will it work this way (use a hidden field)? Any help - including other ways to solve it?

The order form I've made is located at:
http://www.reverse-therapy.no/component/content/article/53
0
Olaf Berli
Asked:
Olaf Berli
  • 11
  • 8
1 Solution
 
Juan OcasioApplication DeveloperCommented:
Yes A hidden field will be the way to go.  Once you pass that information, you can interogate the field to determine which site it's coming from.
0
 
Olaf BerliAuthor Commented:
Any code suggestion, attributes etc.?
0
 
Juan OcasioApplication DeveloperCommented:
You would set it up  just as you would any other field:

<input type="hidden" name="Site" value="MySite1">

This will be passed along with other form data.

The PHP script can then interogate the name field

$SiteName = $_POST['Site'];

if ($SiteName == 'MySite1')
  do stuff here for site 1;
elseif ($SiteName == 'MySite2')
  do stuff here for site 2;
else
  This is a catch all just in case, but should never be executed...;
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Olaf BerliAuthor Commented:
Where does the referrer url come into this field?

The the page with the form is linked to from several of my other pages at "Site1". My need is to know when the visitor comes from the other site, and the only way to know this seems to be to detect the referrer.....

The php stuff is ok.
0
 
Juan OcasioApplication DeveloperCommented:
If you want to use the referrer you can just add this to your php code:

$siteReferrer =  $_SERVER['HTTP_REFERER'];


if ($siteReferrer == 'MySite1')
  do stuff here for site 1;
elseif ($siteReferrer == 'MySite2')
  do stuff here for site 2;
else
  This is a catch all just in case, but should never be executed...;

You would not need to use a hidden field in this case
0
 
Olaf BerliAuthor Commented:
OK. Even one more question:

I'm processing the results from the order form in a separate php script (another file). If I put this code into that script, wouldn't the order form be the referrer and not the external site where the request actually came from?

Seems like I should add at least the $siteReferrer = ....  into the order page. Is this correct?
0
 
Juan OcasioApplication DeveloperCommented:
True, but the referer will be the site from which it's coming from.  I'm assuming the form will be located at:

http://www.mysite1/form.php

and

http://www.mysite2/form.php
0
 
Juan OcasioApplication DeveloperCommented:
One caveat to using referer:  the HTTP_REFERER is not always set.  You should run a couple of tests on the sites from which you are running the web site to see if the variable is set.
0
 
Olaf BerliAuthor Commented:
OK - beginning to understand.....

There is only one form (http://www.mysite1/form.htm)

In site 2 there is only a link to this page.
The visitors using this link should be detected and treated differently.
0
 
Juan OcasioApplication DeveloperCommented:
Ahhhh.  Ok in that situation you'll have to create the variable on the form page and pass the data.  Something like:

<input type="hidden" name="Site" value="$_SERVER['HTTP_REFERER'];">

Then when you sumbit the form


$SiteName = $_POST['Site'];

if ($SiteName == 'MySite1')
  do stuff here for site 1;
elseif ($SiteName == 'MySite2')
  do stuff here for site 2;
else
  This is a catch all just in case, but should never be executed...;
0
 
Juan OcasioApplication DeveloperCommented:
Oops this should read

<input type="hidden" name="Site" value="<?php $_SERVER['HTTP_REFERER']; ?>">

0
 
Olaf BerliAuthor Commented:
Thanks for your excellent help. Would have been easier if I had explained better in the start...
0
 
Juan OcasioApplication DeveloperCommented:
Hey, no problem.  If you still have questions regarding it, let me know via this post.  One other thing, you can pass any data to that hidden field, so if the HTTP_REFERER doesnt work you pass the URL directly from the link.

Good luck!
0
 
Olaf BerliAuthor Commented:
Thanks.
Gave it a try. Put the input field into my order form. The html editor (in Joomla) changed it a little - moved type=hidden to the end, and ended the statement with a /> instead of >
:
<td><input name="Site" value="<?php $_SERVER['HTTP_REFERER']; ?>" type="hidden" /></td>

In the php script the value is printed, but something isn't quite right. It prints like:
<?php $_SERVER[\'HTTP_REFERER\']; ?>

So, instead of a value, the string itself is printed, and a couple of backslashes are inserted. Any ideas what is going on. The form was entered via a local url (on mysite1 - above)
0
 
Juan OcasioApplication DeveloperCommented:
OK.  when you say:

In the php script the value is printed, but something isn't quite right. It prints like:
<?php $_SERVER[\'HTTP_REFERER\']; ?>

Is that what the source code looks like after it has run?  What is the extension of your page?  is it .php?  in order for it to work it has to be.
0
 
Juan OcasioApplication DeveloperCommented:
and just for clarification, that's .php  not with the question mark :)  I made it look like that above so it may have been confusing
0
 
Olaf BerliAuthor Commented:
The order form, with the hidden form field, is an ordinary Joomla file. Not sure what the extension is, but probably not .php

The script that is processing the fields from the order form has a .php extension. Basically it creates an email where the content from the form fields are put into the message body of the email.
When the email is received, the "content" of the hidden form field shows as
<?php $_SERVER[\'HTTP_REFERER\']; ?>
All other fields are ok.
0
 
Juan OcasioApplication DeveloperCommented:
OK.  Try changing the extension to .php to test it.  The problem is the line <?php $_SERVER[\'HTTP_REFERER\']; ?>
is being treated as regular html because the .php extension is not there.  That would be the only way the php engine will know to parse and run the php code.  you could instead hard code the value of the in the link.  so for example you could put in

<a href='www.mysite.com/form.html?Site='mysite1.com'>Click here to order </a>

0
 
Olaf BerliAuthor Commented:
I don't know how to add or change a file extension within the Joomla CMS system, so I created the files in a separate directory on the server. This changed how it worked. In the email there is no trace of the referer site info. It is just blank (empty). Have tried by entering the address to the order form manually in my browser, and also from a link sent to my hotmail account (that should make hotmail or MSN the referer??)

Anyway - it seems simpler to create a separate set of files for orders only from the other site and have the current setup (for my main site) as it is. It may also make it easier to maintain the systems.

Thanks a lot for your help!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now