Solved

Echo contents of href from previous page?

Posted on 2009-07-01
10
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 24757626
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
ID: 24758539
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
ID: 24758758
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 5

Expert Comment

by:Opalelement
ID: 24760741
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
ID: 24762080
<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
 
LVL 3

Author Comment

by:danomatic
ID: 24767436
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
ID: 24769602
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
ID: 24772437
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
ID: 24773127
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
ID: 24817008
Opalelement - that worked! thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

695 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