Forum Moderators: coopster

Message Too Old, No Replies

Similar PHP code for this JavaScript?

Need PHP replacement

         

EarleyGirl

8:37 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Hello All,

Here's the scenario: Client wants to know what page prior to the contact page that the customer was on and wants that included in the information when they receive the submitted contact form. I have done this previously with the javascipt below:

<script type="text/javascript">
function loadRefURL (){
document.forms[0].theAddr.value = document.referrer;
}
</script>
<body onload="loadRefURL()">

In the form I have this:
<input type="hidden" name="theAddr" />

In the php code I have this:
$theAddr = $_POST['theAddr'] ;

And so on. The thing is - I'm switching the contact form to another PHP script which includes the form and PHP on one page (no separate PHP file). For some reason when I add the javascript to this contact page, I get an error when trying to send the form.

Is there something similar with PHP that serves this type of function?

Thanks,
Hope

coopster

8:58 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi EarleyGirl. You can still use JavaScript of course, but if it is just the referring page you are after you could try the $_SERVER [php.net] index HTTP_REFERER to see if it has been sent by the browser (it's not always reliable, of course). Otherwise, if the user is logged in or using a session you could always store the previous page in a $_SESSION variable.

EarleyGirl

9:35 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



but if it is just the referring page you are after you could try the $_SERVER index HTTP_REFERER to see if it has been sent by the browser (it's not always reliable, of course)

Thanks, coopster. But doesn't the HTTP_REFERER only reference the page the visitor is at? For instance, if the visitor is filling out the contact form on contact.php, wouldn't contact.php then be the one listed as the HTTP_REFERER? What my client wants to know is what page on their site was visited right before they went to the contact page. Is that possible with PHP or does it have to include sessions and such?

Thanks,
Hope

coopster

9:42 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



When the user reaches that contact.php page, you can grab that variable if it is indeed set and you will have the page that they were referred from. Try it yourself by setting up a page that has a link to a new page. In your new page, which is a PHP scripted page, output the variable and you will see what I mean. A very quick example ... hopefully it will make sense to you.

page1.php

<html><head><title>Page1</title></head> 
<body><p><a href="page2.php">Page2</a></p></body></html>

page2.php
<html><head><title>Page2</title></head> 
<body><pre>
<?php print_r($_SERVER);?>
</pre></body></html>

EarleyGirl

12:22 am on Jan 6, 2006 (gmt 0)

10+ Year Member



Thanks coopster. That works when done with your example. But this is already in the code:

if (isset($_REQUEST['submit'])) {
$refering_page = $_SERVER['HTTP_REFERER'];

and later:
$refering_page!== $script_location) {
which is the contact.php page.

How would I incorporate your method? Wouldn't that take a lot of complicated PHP code?

Thanks,
Hope

coopster

3:43 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



My code is not a working example for your application but merely a demonstration of how the $_SERVER variable you are after carries from one page to another.

When you hit your landing page (page2.php in my example, contact.php in your real application), you will want to grab the HTTP_REFERER variable and place it in your own variable before the form is displayed and/or submitted for the first time. How do you do that? Check to see if you have stored it already, perhaps in a hidden form field. Make sense? Something like the following ...

page2.php

<html><head><title>Page2</title></head> 
<body><form method="post" action="page2.php">
<?php
if (isset [php.net]($_POST['referer'])) {
$referer = htmlentities [php.net]($_POST['referer']);
} else if (isset($_SERVER['HTTP_REFERER'])) {
$referer = htmlentities [php.net]($_SERVER['HTTP_REFERER']);
} else {
$referer = 'unknown';
}
?>
<input name="referer" type="hidden" value="<?php print $referer;?>" />
</form></body></html>