Solved

Echo contents of href from previous page?

Posted on 2009-07-01
10
622 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:danomatic
10 Comments
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
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
 
LVL 5

Expert Comment

by:ITGaWD
Comment Utility
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
 
LVL 5

Expert Comment

by:Opalelement
Comment Utility
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
 
LVL 5

Expert Comment

by:Opalelement
Comment Utility
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
 
LVL 1

Expert Comment

by:lamzaks
Comment Utility
<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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

by:danomatic
Comment Utility
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
 
LVL 5

Expert Comment

by:Opalelement
Comment Utility
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
 
LVL 5

Expert Comment

by:Opalelement
Comment Utility
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
 
LVL 5

Accepted Solution

by:
Opalelement earned 500 total points
Comment Utility
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
 
LVL 3

Author Comment

by:danomatic
Comment Utility
Opalelement - that worked! thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now