PDA

Click to See Complete Forum and Search --> : Getting the title in the webbrowser?


iamme2007
May 19th, 2007, 09:56 PM
I know how to get the URL of what site they're at but not the site title, does anyone know how? This is how I do the URL.

if (!isset($_SERVER['HTTP_REFERER'])) {
$url = "Unknown";
}
else {
$url = $_SERVER['HTTP_REFERER'];
}

visualAd
May 20th, 2007, 03:55 AM
You will have to load the contents of the URL into an XML document and extract the title element. You must first ensure the Referrer is a url:

if (parse_url($_SERVER['HTTP_REFERER'])) {
$page = new DOMDocument('1.0');
$page->loadHTML(file_get_contents($_SERVER['HTTP_REFERER']));

$title = $page->getElementsByTagName('title')->item(0)->nodeValue;
}

iamme2007
May 20th, 2007, 04:53 AM
This is what I've done:-

if (!isset($_SERVER['HTTP_REFERER'])) { // Check the url they came from
$url = "Unknown"; // If no URL then show the words Unknown
}
else {
$url = $_SERVER['HTTP_REFERER']; // Shows the URL
$page = new DOMDocument(1.0);
$page->loadHTML(file_get_contents($url));

$title = $pages->getElementsByTagName('title')->item(0)->nodeValue;
}


And where it shows:

ImageString ($back, 5, 170, 100, "$title", $textcolor);

But it doesn't work.

visualAd
May 20th, 2007, 05:43 AM
It doesn't work? Well without more information I could only speculate on why ;)

visualAd
May 20th, 2007, 05:44 AM
BTW, you have changed the code. I explained that you MUST check the referer is a URL. Otherwise someone could forge the header and have your script open files on the web server instead of the URL

penagate
May 20th, 2007, 05:57 AM
You should get the message "Call to member function on a non-object". Don't suppress error messages; it makes debugging impossible and wastes your time.

I think opening the referring URL in such a manner is a bad practice, because it results in double the amount of requests for the server hosting the page containing the image (one from the client, and one from the server hosting the dynamic image).

Also, the quotes around $title are superfluous, since $title is a string anyway.
ImageString ($back, 5, 170, 100, "$title", $textcolor);

iamme2007
May 20th, 2007, 06:44 AM
When I remove the quotes around the string I get this "The image “http://localhost:2000/stat%20sig/sig.php” cannot be displayed, because it contains errors." thats why its there.

And I've found a script that can get the page title but I'm having trouble finding the code that does it, the link:-

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=356&lngWId=8

visualAd
May 20th, 2007, 09:36 AM
The quotes around the string make NO difference. What error does you script produce, how does it not work?

visualAd
May 20th, 2007, 09:40 AM
You should get the message "Call to member function on a non-object". Don't suppress error messages; it makes debugging impossible and wastes your time.

I think opening the referring URL in such a manner is a bad practice, because it results in double the amount of requests for the server hosting the page containing the image (one from the client, and one from the server hosting the dynamic image).

Also, the quotes around $title are superfluous, since $title is a string anyway.
Defiantly, you should at the very least be caching the results.

iamme2007
May 20th, 2007, 10:06 AM
I've uploaded the complete source, its what I've done so far, I'm new to PHP.

Can someone see why its giving me the error when I remove the strings from the quotes, also try adding the current site title?

visualAd
May 20th, 2007, 12:15 PM
Why would you want to remove the quotes? You should not have quotes if you are using a single variable. I.e: "$text" is identical to $text.

If you want someone to do it for you then hire someone. You've been given some example code and some advice on how to do it. So how about you tell us what error you are getting and we go from there?

Tip: remove the Content-Type header from the top of the script while debugging, so you can see the error messages. :)