0

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.

1
  • Does this not cause a never ending loop for your server requests. because, your trying to request a file your spitting out so each fopen you make to index.php you cause it to fopen again and so on! this is a fail. this may explain your comment below of Server ran out of threads to serve requests Commented Sep 8, 2010 at 13:16

4 Answers 4

2

$_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.

Sign up to request clarification or add additional context in comments.

Comments

1

try this:

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

4 Comments

$_SERVER[$url] is the first error. I replaced the line with your solution but the browser doesn't respond. error.log shows up nothing, until now.
error.log shows [Wed Sep 08 15:07:09 2010] [warn] Server ran out of threads to serve requests. Consider raising the ThreadsPerChild setting
why are you using the full URL to open the file? if its on the same server can't you just use a local link (eg. "index.php" rather than "127.0.0.1/index.php")
also why is there an @ before fopen? are you sure it's needed? tizag.com/phpT/fileopen.php
0

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';
}



?>

1 Comment

I replaced the footer.php with your script. The browser shows getting title for page: "127.0.0.1/test" but no title or the title tag not found message
0

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){}
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.