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
LVL 3
danomaticAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZylochCommented:
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.
0
ITGaWDCommented:
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

0
OpalelementCommented:
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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

OpalelementCommented:
Another topic like this sprung up (http://www.experts-exchange.com/Database/MySQL/Q_24538662.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

0
lamzaksCommented:
<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'];
?>
0
danomaticAuthor Commented:
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
0
OpalelementCommented:
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

0
OpalelementCommented:
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.
0
OpalelementCommented:
I made one that is cross-domain (so if anyone links to your site, you can still see what they clicked)

The only problem is that it can only separate by absolute or relative URLs... So either of the following would return linktext2:

<a href="link.php?blah=2" rel="nofollow">linktext2</a>
<a href="/ee_testing/link.php?blah=2" rel="nofollow">linktext4</a>

BUT those both have blah=2... It is distinct by that too so the following would all return the correct text.

These two will never work:

<a href="./link.php?blah=3" rel="nofollow">linktext3</a>
<a href="../ee_testing/link.php?blah=5" rel="nofollow">linktext5</a>

I could eventually turn this into a more advanced checking system but I just don't have that kind of time right now, sorry.


<?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;
$c_parts = parse_url($current_url);
foreach($matches[1] as $url)
{
	$parts = parse_url($url);
	foreach($c_parts as $key => $part)
	{
		if(!$parts[$key])
		{
			$parts[$key] = $part;
		}
		if($key == "path" && $parts[$key] == basename($parts[$key]))
		{
			$parts[$key] = $part;
		}
	}
	if($parts == $c_parts)
	{
		echo "You clicked \"" . $matches[2][$i] . "\"";
		break;
	}
	$i++;
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
danomaticAuthor Commented:
Opalelement - that worked! thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.