473,791 Members | 2,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you grab the title of the current page?

37 New Member
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_conten ts doesn't seem to work for current pages the user is on, the server hangs in these cases.
Aug 10 '11 #1
2 12137
dlite922
1,584 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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
Aug 12 '11 #3

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

Similar topics

3
2034
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, but the name of the page that is including the file)? i hope im making sense thanks for the help
8
6604
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 class="mainlink mainlink1" href="">Events</a><br> <a class="mainlink mainlink2" href="">Home Page Teaser</a><br> <a class="mainlink mainlink3" href="">Testimonials</a><br> <a class="mainlink mainlink4" href="">Repertoire</a><br>
10
2278
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 Let's say I'm on the home page, and the Home link is dark green whereas Products and Contact are green.
4
2117
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 hyperlink that includes the current web page URI. When clicked, the current page is then processed by a program. For example, it would look something like this: <a href="http://some.program.com/index.php?current-web-page-URI">text etc.</a>
3
2264
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 allowed for the table to show only 5 rows at a time, for example, I want to get the 5 rows that are displayed on page 3 of 8. How do I do this???? Thanks!
2
2035
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 AllowPaging = true
3
2645
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 and could use some help in fixing it. The problem is that I want the index to always highlight the link for the content which is currently being displayed on the right hand side of the page. This behaviour should be over and above the...
3
1960
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
3201
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 follows: The code below is for the first page:session_start is in line 3 <link href="css/jobSheet.css" rel="stylesheet" type="text/css" /> session_start();
11
5655
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 underlined on the main menu. In the code the menu is displayed by this code:
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.