I am trying to grab the title of an html page using php. Grabbing the title is actually not the problem, the problem is grabbing the "current" page that a user is on. For example, the page I am on now has a title tag, and I would want to store this title in a php variable. The reason why I want to do this is to track active visitors on the site, and what pages they are currently viewing.
My strategy was to grab the current URL, by calling this function:
-
static function getURL() {
-
return "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
-
}
-
Then I was going to use Simple HTML Dom to grab the title:
- static function getTitle() {
-
$url = commonFunctions::getURL();
-
$html = file_get_html('http://www.lnreports.com/');
-
foreach ($html->find('title') as $title) {
-
$title = $title->innertext;
-
}
-
return $title;
-
}
The problem with this, is that the code hangs the site. Each time I want to grab the URL of the page I am currently on, it hangs (I think because it is doing an infinite loop). However, it works fine if I grab a target URL. But then this defeats the purpose of what I am trying to accomplish.
Can anyone help me figure out a solution to grab the current title of a page and store it into a variable? cURL and file_get_contents doesn't seem to work for current pages the user is on, the server hangs in these cases.