How do you grab the title of the current page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chopin
    New Member
    • Mar 2007
    • 37

    How do you grab the title of the current page?

    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:

    Code:
    static function getURL() {
    return "http://" . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];
    }
    Then I was going to use Simple HTML Dom to grab the title:

    Code:
       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_conten ts doesn't seem to work for current pages the user is on, the server hangs in these cases.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    You do know PHP executes before the server even pulls up the page, right?

    you already have the title in the file, why not grab it there?

    You're going about tracking all wrong.

    The page should call out (i.e. using JavaScript and AJAX it should call a php page. The php page wouldn't return anything. it's solely to put tracking info on the server.

    since you already have the (assuming static) title in the html, just call a URL.

    In fact I bet it would pretty pretty easy to grab it via JavaScript.

    Code:
    alert(document.title); // should do it.
    Let me know if you get stuck with coding. The code you have above is junk, delete it.

    Cheers,


    Dan

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      More Info:

      See jQuey->get() function. : http://api.jquery.com/jQuery.get/

      If you don't want jQuery library, search for XMLHttpRequest in your favorite search engine.

      the url will call will be something like tracker.php?pag e=TitleOfPage.

      in tracker.php do what you need to do with $_GET['page'].

      Good luck,


      Dan

      Comment

      Working...