[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Referrer in HTML form

Posted on 2011-05-03
19
Medium Priority
?
331 Views
Last Modified: 2012-05-11
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
Comment
Question by:Olaf Berli
  • 11
  • 8
19 Comments
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35514051
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
 

Author Comment

by:Olaf Berli
ID: 35514077
Any code suggestion, attributes etc.?
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35514173
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Olaf Berli
ID: 35514254
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35514587
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
 

Author Comment

by:Olaf Berli
ID: 35514882
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35515631
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35515643
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
 

Author Comment

by:Olaf Berli
ID: 35515700
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35515806
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
 
LVL 15

Accepted Solution

by:
Juan Ocasio earned 2000 total points
ID: 35515823
Oops this should read

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

0
 

Author Closing Comment

by:Olaf Berli
ID: 35515905
Thanks for your excellent help. Would have been easier if I had explained better in the start...
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35516028
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
 

Author Comment

by:Olaf Berli
ID: 35517041
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35517180
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35517193
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
 

Author Comment

by:Olaf Berli
ID: 35684264
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
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35689343
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
 

Author Comment

by:Olaf Berli
ID: 35693153
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

831 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