开发者

Url and title with php snippet

I read several similar posts but I don't see my fault.

index.php looks like:

<head>
<title>Demo Title</title>
</head>
<body>
<?php 
    require_once "footer.php";
?>
</body>

footer.php looks like:

<?php
/*
 * _$ Rev. : 08 Sep 2010 14:52:26 $_
 * footer.php
 */

$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo $url;

$file = @ fopen($_SERVER[$url],"r") or die ("Can't open HTTP_REFERER.");
$text = fread($file,16384);
if (preg_match('/<title>(.*?)<\/title>/is',$text,$found)) {
        $title = $found[1];
} else {
        $title = " -- no title found -- ";
}
?>

A request for the URL http://127.0.0.1/test/index.php results in:

http://127.0.0.1/test/index.phpCan't open HTTP_REFERER.

or for http://127.0.0.1/test/

http://127.0.0.1/test/Can't open HTTP_开发者_如何学编程REFERER.

Any hints appreciated.


$_SERVER is an array which contains a bunch of fields relating to the server config. It does not contain an element named "http://".$host.$param, so trying to open that as a filename will result in the fopen call failing, and thus going to the die() statement.

More likely what you wanted to do was just open the file called "http://".$host.$param. If that's what you want, then just drop the $_SERVER[] bit and it should work better.

Note that because it's a URL, you will need your PHP config to allow opening of remote files using fopen(). PHP isn't always configured this way by default as it can be a security risk. Your dev machine may also be configured differently to the system you will eventually deploy to. If you find you can't open a remote URL using fopen(), there are alternatives such as using CURL, but they're not quite as straightforward as a simple fopen() call.

Also, if you're reading the whole file, you may want to consider file_get_contents() rather than fopen() and fread(), as it replaces the whole thing into a single function call.


try this:

$file = @ fopen($url,"r") or die ("Can't open HTTP_REFERER.");


Try

<?php

$dom = new DOMDocument();

$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;

echo 'getting title for page: "' . $url . '"';

$dom->loadHTML($url);

$dom->getElementsByTagName('title');

if ($dom->length)
{
    $title = $dom->item(0);
    echo $title;
}
else
{
    echo 'title tag not found';
}



?>


I can see your trying to track the referral's title

You need to use $_SERVER['HTTP_REFERER']; to get that

what you want to do is something like this

    $referrer = (!empty($_SERVER['HTTP_REFERER']) && !substr($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_REFERER'] : false);

    if($referrer)
    {
        try
        {
            if(false !== ($resource = fopen($referrer,"r")))
            {
                $contents = '';
                while($contents .= fread($resource,128))
                {}

                if(preg_match('/<title>(.*?)<\/title>/is',$contents,$found))
                {
                     echo 'Referrer: ' $found[1];
                }
            }
        }catch(Exception $e){}
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜