Forum Moderators: coopster

Message Too Old, No Replies

Request Referer PHP problem-2

trouble with URL referer through HTML/PHP

         

jerrye

9:22 am on Aug 8, 2008 (gmt 0)

10+ Year Member



Hi all, probably just my brain not working to full potential, but i have a site which a number of URLs link to. on the second page of this site, i have a form for readers details but i also want to harvest the reffering URL's and submit it at the same time as the form.

I think i need to use HTTP_REFERER but a little lost i did have a look at a previous post on this site, but couldn't make sense of it.

below is the basic code for the form and then for the php handeling page.

i think i'm going to need something on the homepage so that the URL can be grabbed and then passed to the form on the second page. like i said probably dead easy, but i'm loosing hair.

form page.....

<form method="post" action="http://www.example.com/_private/prospa/send-funnel1-NEW.php">
<input type="hidden" name="recd" value="<?=isset($recd) ? $recd : $HTTP_referer?>">
<div align="center">
<table width="508" border="1">
<tr valign="middle" class="style24">
<td width="105"><div align="left" class="style1">*Full Name:</div></td>
<td width="146" class="style1">
<input type="text" name="name">
</td>
<td width="71"><div align="left" class="style1">*Email:</div></td>
<td width="158" class="style1">
<input type="text" name="email">
</td>
</tr>
<tr valign="middle" class="style24">
<td><div align="left" class="style1">*Main Phone:</div></td>
<td class="style1">
<input type="text" name="homephone">
</td>
<td><div align="left" class="style1">2nd Phone or Mobile :</div></td>
<td class="style1">
<input type="text" name="mobile">
</td>
</tr>
<tr valign="middle" class="style24">
<td><div align="left" class="style17">*House Number or name </div></td>
<td><input name="housenumber" type="text"></td>
<td><div align="left" class="style17">Street</div></td>
<td><input name="street" type="text"></td>
</tr>
<tr valign="middle" class="style24">
<td><div align="left" class="style17">Town</div></td>
<td><input name="town" type="text"></td>
<td><div align="left" class="style17">City</div></td>
<td><input name="city" type="text"></td>
</tr>
<tr valign="middle" class="style24">
<td><div align="left" class="style17">County</div></td>
<td><input name="county" type="text"></td>
<td><div align="left" class="style17">*Postcode</div></td>
<td><input name="postcode" type="text"></td>
</tr>
<tr valign="middle" class="style24">
<td colspan="2"><div align="left" class="style17">Where did you hear about the Opportunity?

</div></td>
<td colspan="2">
<div align="left">
<span class="style17">
*
<select name="reflist" id="reflist">
<option>Please Select</option>
<option>Internet Search Engine</option>
<option>Personal recomendation</option>
<option>Email Invitation</option>
<option>Shop Ad, Card or Flyer</option>
</select>
</span>
<label><input onfocus="this.value = ''; this.onfocus = function () {}" type="text" name="referal" value="Distributor Name or 6 Digit Number" size="35"></label>
</div></td>
</tr>
</table>
<span class="style36">All fields marked * are required </span><br>
<p align="center" class="style17"> <input name="submit" type="submit" value=" Next &gt; ">
</p>
<p class="style36">Applicants must be over eighteen and living in the UK, ROI, Germany or Netherlands. </p>
</div>
</form> </td>
</tr>

PHP HANDLER PAGE.....

<?php

$to = "jerrye@example.com" . ', ';
$to .= "shane@example.net";
$site = $_POST['recd'];
$name = $_POST['name'];
$email = $_POST['email'];
$homephone = $_POST['homephone'];
$mobile= $_POST['mobile'];
$housenumber= $_POST['housenumber'];
$street= $_POST['street'];
$town= $_POST['town'];
$city = $_POST['city'];
$county= $_POST['county'];
$postcode = $_POST['postcode'];
$reflist= $_POST['reflist'];
$referal = $_POST['referal'];
$d = date('l dS \of F Y h:i:s A');
$sub = "PROSpA Reply Form";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = "From: $name <$email>\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$mes .= "Site: ".$recd."\n<br>";
$mes .= "Name: ".$name."\n<br>";
$mes .= "House: ".$housenumber."\n<br>";
$mes .= "Street: ".$street."\n<br>";
$mes .= "Town: ".$town."\n<br>";
$mes .= "City: ".$city."\n<br>";
$mes .= "Postcode: ".$postcode."\n<br>";
$mes .= "Home Phone: ".$homephone."\n<br>";
$mes .= "Mobile: ".$mobile."\n<br>";
$mes .= "Email: ".$email."\n<br>";
$mes .= "Referal: ".$reflist."\n<br>";
$mes .= "Referal: ".$referal."\n<br>";
$mes .= "Date & Time: ".$d;
if (empty($name) ¦¦ empty($housenumber) ¦¦ empty($homephone) ¦¦ empty($postcode) ¦¦ empty($reflist))
{
echo " <h3>Sorry all fields marked * are required.</h3>";
}
elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
print " <h3>Sorry the email address you entered looks like it's invalid.</h3>";
}
else
{

mail($to, $sub, $mes, $headers);

echo '<script language="Javascript">';
echo 'window.location="movie.htm"';
echo '</script>';

}
?>

hope this makes sense.
Jerry

[edited by: eelixduppy at 12:47 pm (utc) on Aug. 8, 2008]
[edit reason] exemplified [/edit]

eelixduppy

1:37 pm on Aug 8, 2008 (gmt 0)



Hello and Welcome to WebmasterWorld! :)


<input type="hidden" name="recd" value="<?=isset($recd) ? $recd : $HTTP_referer?>">

Here is where your problem lies, then. I'm guessing you have the register_globals directive set in your configuration file? If you don't, ti doesn't really matter, but try the following instead:


<input type="hidden" name="recd" value="<?php echo isset($recd) ? $recd : $_SERVER['HTTP_REFERER']; ?>">

The code above doesn't assume any of your settings, and should pretty much work with anything. Try that and see if it helps.

jerrye

11:05 pm on Aug 9, 2008 (gmt 0)

10+ Year Member



sadly still not getting a value back on the email. would it be because the referer would be from the first page and not the URL of the refering site? just get a blank at the moment. i would have thought i would get somethingback.

any thoughts?

jerrye

7:30 pm on Aug 10, 2008 (gmt 0)

10+ Year Member



not getting anything out of HTTP_REFERER at all. is there a simple way to test if its even working before i send it through to the form?

jatar_k

1:46 am on Aug 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try just echoing it on the page to see what the value is

echo $_SERVER['HTTP_REFERER'];

that should tell you whether it is there to be sent.

If the form isn't their original point of entry then you need to store it in a cookie/session and then get it from there to send in your form.

jerrye

7:44 am on Aug 11, 2008 (gmt 0)

10+ Year Member



echo $_SERVER['HTTP_REFERER']
works on the .php form handling page. but your right, i need to capture the url from the entry page.

entry.HTM -> formentry.HTM -> formhandler.PHP

entry.HTM
homepage, this is the one where i need to harvest the referer.

formentry.HTM
form entry for contact details along with a hidden field for the referer from entry.HTM

formhandler.PHP
sends the form via email including the hidden referer field.

hope this is making sense.

jatar_k

1:53 pm on Aug 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you actually need to get the referer on any page they may enter on

can you use php in your htm pages?

if not then you have a couple options

you could grab the referer and save it to a cookie using javascript. You would need to test to see if the cookie was already set, if it is, then don't save over it. Then grab the value from that cookie on your form page.

otherwise you could enable php parsing for the .htm extension and do as above using php, the only difference is with php you could use cookies or sessions, your choice.

jerrye

3:31 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



i can use php on my htm pages. but if the javascript way is easier then that would do. any pointers?

jatar_k

4:43 pm on Aug 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



using php is probably better because you can write a little chunk of code and then store it in a separate file making it easy to include on every page.

the steps are pretty simple

check to see if cookie exists
if it does not exist then grab the referer and store it in a cookie

on you form page just grab the data from the cookie and put it in your form as a hidden element

jerrye

5:13 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



so on the homepage grab the referer and place it in a cookie. then on the formpage page, place the cookie result in a hidden field in the form? the process makes sense. not used cookies before though. is it a simple affair?

jatar_k

5:22 pm on Aug 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



some links

[php.net...]
[php.net...]

remember not to make them required, if they don't take the cookie, then they don't.

jerrye

11:28 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



cool, almost there.

on my index page i have...

<?php
$value = $_SERVER['HTTP_REFERER'];
setcookie("refercookie", $value, time()+360);
?>

and its coming through on the email. i'm trying to then delete the cookie with...

setcookie ("refercookie", "", time() - 360);

is that right?
just testing but not certain its there yet.
thanks again for all your help on this.

jatar_k

12:15 pm on Aug 12, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the only thing you don't have is a test to see if the cookie exists. If you only plan on doing it on your home page then that's fine, as long as someone doesn't go to the homepage twice before your form.

I would suggest a test to see if the cookie exists before you set it

<?php
if (!isset($_COOKIE['refercookie'])) {
$value = $_SERVER['HTTP_REFERER'];
setcookie("refercookie", $value, time()+360);
}
?>

jerrye

4:10 pm on Aug 12, 2008 (gmt 0)

10+ Year Member



makes sense. one last question (i hope)

i'm using..

setcookie ("refercookie", "", time() - 360);

to delete the cookie, i have this placed after the email is sent but before the page opens the next page (automated) i take it this is the right place. as i don't want to delete the cookie before i send it, but want rid asap.

makes sense?

Thanks again,
Jerry.

jatar_k

1:17 pm on Aug 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sounds right

test it and see ;)

jerrye

3:25 pm on Aug 13, 2008 (gmt 0)

10+ Year Member



looks to be ok, but tricky to test.
the live stuff looks to be coming through ok, but if i try from this computer then its blank.

jerrye

5:42 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



still not working. i'm loosing a lot of leads. any help would be great as i'm getting it in the neck now.
please helllllllp.
jerry

jerrye

12:42 pm on Aug 17, 2008 (gmt 0)

10+ Year Member



it looks like it kinda works. but is very inconsistant. is tjis because i have to rely of the visitors computor allowing the cookie in the first place? i allow cookies but when i test it no new cookie appears in the cookie folder on my machine. and unsuprisingly no referer detail comes through on the reply email.
is there some way i can make this severside and take away the inconsistency?

Many thanks,
Jerry

PHP_Chimp

4:02 pm on Aug 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could look into using a session to store your value. However the session id is usually stored as a cookie. Session cookies are almost always allowed--even if people block cookies in general--however there is still no guarantee that a session cookie will be stored. So you are really back in the same place you are now.

The other problem could be that the HTTP_REFERER is not always set. As this is something that is set by the client; therefore you have no control over it. So I would look for an implementation that does not require the referer and treat that piece of information as nice if you get it and ignore it if you dont.

If that is not an option then you may have to look at passing variables between the sites. If you are going to do this then just be aware that if you are going to get someone to link to a page using page.php?referer=example.com then it doesnt take a rocket scientist to work out how to get around your system. Hidden post variables have the same problem, as people can see them in the source code.