Jump to content

Archived

This topic is now archived and is closed to further replies.

skatermike21988

Quck referrer question

Recommended Posts

Hey guys,

i am just curious to know if there is a way to get the title of a refering page.

i am currently useing @$HTTP_REFERER to recieve the link and was wondering about recieving the title.

thanks for any help.

Share this post


Link to post
Share on other sites
As far as I know, the title isn't passed by the browser. You'd have to use the referer URL and scrape the page to pull out its title. You could use a combo of file() and eregi() for your scraping, if you're not too great with regular expressions, pop over to our Regex forum or give [url=http://www.regular-expressions.info]this site[/url] a look.

Share this post


Link to post
Share on other sites
ok how would i go abou cURL and the scrapeing with regex i am relatively new to php and am really trying hard to learn this stuff.

Share this post


Link to post
Share on other sites
If the referring page is comming from a page you have control over you could pass the title through the url.

Share this post


Link to post
Share on other sites
Well what i am doing is i have a visitor tracking system and instead of it displaying the long url (like you would get if you searched yahoo, or google etc) for it to display the page title. the way i have it is it logs the user's i.p. how many times they have visited last time they have visited and referrer. and underneath it i have the top ten referrers but displaying a long link like:

http://search.yahoo.com/search?p=thescreenguy&fr=FP-tab-web-t402&toggle=1&cop=&ei=UTF-8

have it display the title of the page.

Share this post


Link to post
Share on other sites
This code will try and connect to the referring page and get the title.
It has several fallbacks so if the title is equal to (boolean) [b]false[/b] then you can use the URL.

[code]
<?php

$referer = ((isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']!='')?($_SERVER['HTTP_REFERER']):(false));
$title = false;
if($referer!=false)
{
        $curl  = curl_init();
        CURL_SETOPT($curl,CURLOPT_URL,$referer);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_FOLLOWLOCATION,0);
        curl_setopt($curl,CURLOPT_POST,0);
        $result = curl_exec($curl);
        if($result)
        {
                $res = preg_match('@<title.*?>(.*?)</title.*?>@',$result,$matches);
                if($res)
                {
                        $title = ((isset($matches[1]))?($matches[1]):(false));
                }
                else
                {
                        $title = false;
                }
        }
        else
        {
                $title = false;
        }
}
[/code]

Now, $title should be the title of the page, or [b]false[/b] if none was found.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.