473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do you grab the title of the current page?

37
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:

Expand|Select|Wrap|Line Numbers
  1. static function getURL() {
  2. return "http://" . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];
  3. }
  4.  
Then I was going to use Simple HTML Dom to grab the title:

Expand|Select|Wrap|Line Numbers
  1.    static function getTitle() {
  2.         $url = commonFunctions::getURL();
  3.         $html = file_get_html('http://www.lnreports.com/');
  4.         foreach ($html->find('title') as $title) {
  5.             $title = $title->innertext;
  6.         }
  7.         return $title;
  8.     }
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.
Aug 10 '11 #1
2 11892
dlite922
1,584 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. alert(document.title); // should do it. 
  3.  
  4.  
Let me know if you get stuck with coding. The code you have above is junk, delete it.

Cheers,


Dan
Aug 12 '11 #2
dlite922
1,584 Expert 1GB
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?page=TitleOfPage.

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

Good luck,


Dan
Aug 12 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: JT | last post by:
is there a command in asp to get the name of the current page? furthermore, is there a way to retrieve the name of the current page within an 'included' file (not the name of the included page,...
8
by: Dan | last post by:
When a user clicks on a link in my menu, I want the background color of the link that comes up in the hover to remain on the destination page. My menu looks like this: <div id="adminmenu"> <a...
10
by: Kerberos | last post by:
Is there a way to change the color of a link in a navbar if the address of the link is the current page? For instance if I have: Home - index.php Products - products.php Contact - contact.php...
4
by: Sandy Bremmer | last post by:
I am wondering if the following can be accomplished with javascript (and if so, if you think javascript is an appropriate solution). I'm afraid my javascript skills lack. I need to build a...
3
by: Carolyn Vo | last post by:
I have a datagrid in my web control class that I am trying to get the current rows displayed for. I have enabled paging on the datagrid so if the user is currently on page 3 of 8, and if I have...
2
by: Brian Henry | last post by:
Hi, I have a data grid that is set up like this Page items displayed = 10 EnableViewState = false (i dont want to send large amounts of data over the internet!) CustomPaging = false...
3
by: Rhino | last post by:
Yesterday, I reworked the index on my site - http://sfl.london.on.ca - so that they used list markup and I'm quite pleased with them. However, I'm having a problem with one small aspect of the menu...
3
by: elyob | last post by:
Hi, I'm just wondering how I change some css text when I mouseover a picture. At the moment document.write just writes to a new page. function displayname(name) { document.write(name); }
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
11
by: dominoeffect | last post by:
Hey all! I'm building a site for a friend which uses this Wordpress theme: http://www.felixkrusch.de/about ^ As you can see by visting the site, the title of the current page is always...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.