Link to home
Start Free TrialLog in
Avatar of danomatic
danomaticFlag for United States of America

asked on

Echo contents of href from previous page?

I am wondering if it's possible with PHP to echo the contents of an href from a previous page.

In other words if I am on page A and click on a link such as the one below:

<a href="page-b.php">Link to page B</a>

... is it possible when the person gets to page b that they see some text, "you clicked on: 'Link to page B' " 

I am hoping to come up with a solution so I don't have to manually enter code into all the hrefs, rather to enter some kind of referrer code on page B as I have many A type pages / links scattered in the site that will all go to page B.

thank you
Avatar of Zyloch
Zyloch
Flag of United States of America image

Here are several things that jump immediately to mind:

1. Write some JavaScript that will automatically append some sort of query string to each href with the anchor text of the anchor. Then it will be appear in $_GET in your next page.

2. Append those query string manually; of course, you don't want to do that.

3. Somehow map href references to their anchor text in an associative array or similar structure (maybe even the database), and then parse the referrer through $_SERVER['HTTP_REFERER'] and match it to the anchor text in your database.

4. Give up on displaying the anchor text and just provide a link back to the previous page.
This could be a option in the Code Snippet below.

Other possibilites is using javascript to get content from page then div which I know how to do,
I think there is something else which may be able to do the same thing but I'm now sure what its called so you would need to look on the net for solutions and other web scripting languages.

Hope this helps
<a href="page-b.php">Link to page B<input type="hidden" id="anything" name="Page_b"></a>
 
<?php
//Page two...
echo $Page_b;
?>

Open in new window

Avatar of Opalelement
Opalelement

You can get the referrer using $_SERVER['HTTP REFERER']

This gives the URL of the page that sent them, meaning you can tell them where they came from by checking that... but you can't get the link they clicked on through that.
Another topic like this sprung up (https://www.experts-exchange.com/questions/24538662/Is-there-an-easy-way-to-pass-and-retrieve-link-text-to-linked-page.html) so I made a PHP solution...

There was another topic similar to this (link below) and it inspired me to write a PHP solution for it since two people wanted it... This gets the URL that you are currently on and the page you came from. It then searches all <a> tags and tries to find the one where the href of that <a> tag matches the URL of the current page. If it does, it outputs the content of the <a> tag.
<?php
$current_url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$ref=$_SERVER['HTTP_REFERER']; 
$file_contents =file_get_contents($ref);
preg_match_all("/<a.*?href=[\"'](.*?)[\"'].*?\>(.*?)<\/a>/i", $file_contents, $matches);
$i = 0;
foreach($matches[1] as $url)
{
        if($url == $current_url)
        {
                echo "You clicked \"" . $matches[2][$i] . "\"";
                break;
        }
$i++;
}
?>

Open in new window

<a href="page-b.php?lastlink=Link to page B">Link to page B<input type="hidden" id="anything" name="Page_b"></a>
 
<?php
//Page two...
echo "you clicked on: ".$_GET['lastlink'];
?>
Avatar of danomatic

ASKER

Opalelement -- Yes, It looks like the other post wants to do exactly the same thing as me.

I put up a test with your code, but it does not seem to work:

http://207.58.187.44/page-a.php (has the simple href to page b)

and

http://207.58.187.44/page-b.php (contains your code as above)
-- nothing gets echoed when I click from the first link to the second
Can you replace page-b content with this? Basically just outputting info for debug purposes. Then I will look at it.
<?php
$current_url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$ref=$_SERVER['HTTP_REFERER']; 
$file_contents =file_get_contents($ref);
preg_match_all("/<a.*?href=[\"'](.*?)[\"'].*?\>(.*?)<\/a>/i", $file_contents, $matches);
$i = 0;
foreach($matches[1] as $url)
{
        if($url == $current_url)
        {
                echo "You clicked \"" . $matches[2][$i] . "\"";
                break;
        }
$i++;
}
echo "<br /> $ref <br />";
echo "$current_url <br />";
echo html_entities($file_contents) . "<br />";
print_r($matches)
?>

Open in new window

I think I figured out why...

If the link is relative, it won't match the full URL. I will work on fixing this.

(by the way, this is from my post in the other topic)
Note that mine will not work if:

1. The page that you go to is redirected
2. It will cause a foreach error if you don't have a referrer (you can do a simple check to fix that)
3. If you submit something through a form, it will not find that on the referrer. It only searches for <a> tags, not forms.
ASKER CERTIFIED SOLUTION
Avatar of Opalelement
Opalelement

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Opalelement - that worked! thanks