Jump to content
xisto Community
vizskywalker

Obtain The Page Title

Recommended Posts

I'm working on a PHP script that is a module of a larger script which is designed to be protable between any website. However, to properly execute in a dynamic fashion, the script needs to know the title of the web page it is included in. Is there any way, via HTTP Headers or another method, other than using libcurl to obtain the page and parse for the title, to obtain the title for a php script?~Viz

Share this post


Link to post
Share on other sites

I'm working on a PHP script that is a module of a larger script which is designed to be protable between any website. However, to properly execute in a dynamic fashion, the script needs to know the title of the web page it is included in. Is there any way, via HTTP Headers or another method, other than using libcurl to obtain the page and parse for the title, to obtain the title for a php script?
~Viz

I dont know if via HTTP Headers you can do it, i think that a simple way will be by using an array with all the titles of the web pages that you need or may be by using a template engine.

best regards,

Share this post


Link to post
Share on other sites

I think that viz is trying to avoid hardcoding or archiving the page titles to allow the script to be plug-n-play. If you have to add the page details for every page you want to use, then the end user will be forced to do so as well. But if you write the script to collect that data from the sourc, the in theory any web page could be used with out any difficulties.This assumes that the script doesn't require that all of the pages that might be used have a data record stored someplace so that the script will know their URL's.vujsa

Share this post


Link to post
Share on other sites

You can use the DOM extensions to parse the HTML of the page, and then using SimpleXML or something just retrieve the title... quite easily. If you're interested write back and I'll help you through some code.

Share this post


Link to post
Share on other sites

Other way you can do it is using Output Buffering, use ob_start(); and later when you output something, it won't be sent to the browser yet, the script will still be able to send headers even though you made echo and etc. so later just use str_replace() to replace the title or to write the title from the area {title} ? There are a lot of ways in my opinion. :(

Share this post


Link to post
Share on other sites

Okay, a couple clarification points:1) file_get_contents() and libcurl, fopen, whatever, accomplish the same thing, all require the script to open itself and parse itself, which is something I'd rather avoid, since it is already loaded once, having to load it twice will eat up system resources.2) Since the script is designed to be plug-n-play, the goal is to not need any scripting on the part of the user. So anything requiring the title to be hard-coded into the script somewhere will not work.3) I've thought about using DOM, but as far as I can tell, you'd still need to load the script a second time (the first being when the script is actually run) into a DOM object, which leads to the problems associated with number 1.~Viz

Share this post


Link to post
Share on other sites

Wow you really know how to make your life difficult don't you?Let me make sure I understand how this thing is suppose to work.The user will for whatever reason use your script inside of their webpage. How will they do this? I mean is thi with an iframe, frame, or include? Is this part of a larger script that has to be used or can this work with any webpage? You said this will work as a module, why not make the parent of the module do this work. If I had some idea of how you will request this "module", I might be able to provide more help.You can use the file system without parsing the script first by using file read but then you have the problem of finding the page title. If the script is complex enough, it may have dozens of pre-defined page titles or all of the titles could be generated on the fly in which case, even if you knew the variable that housed the title, you may not get the right title or may now get anything at all. If this is truely plug-n-play, then you couldn't be sure of the page title variable and what if your script is called by and HTML document instead of a PHP document or maybe ASP, etc... Another problem is what if the page that you request from was a PHP script that used and included file to get create the title. In this case you'd be looking in the wrong file unless you wanted to check the file for includes and then check the included files and any included files they may have, etc...What if the page titles are all saved in a database? Then you'd need a way access the correct database and table, etc...I think that you may have to go ahead and parse the requesting document twice. This still has a few problems since the server could end up with a different version the second time through. If the user is loged in and in a member only section, the server may not be able to read the same data since it is not the member. I think this happens with file_get_contents().You said that you don't want to have the user enter title data into the code. I guess this is this the same for a database? If you are requesting your script via a url, then you could pass the information in the link using the GET method of passing variables to the server. With the right JavaScript code, you can dynamically build the page title into the url on the client side. You'd want to use a script of some kind to build the url since having the user do it has problems other that your desire to reduce the users work load to get the script working. If a title uses spaces in it which most page title do, then the user would have to code the spaces to work in the url. While many people know that "%20" is used for space in a url, there are a lot of other characters which are not so easy but nearly as common. So if you only want to parse the script once and don't want user involvement, then the JavaScript generated url is that way to go since the only time you can get the data you need is AFTER the page has been parsed and the only parsed copy is in the browser.Hope This Helps. :(vujsa

Share this post


Link to post
Share on other sites

Yeah, I hadn't formally decided how I wanted to include it. Right now I've been using a php include, but then again right now I've got the title hard coded. I'd been thinking about using javascript, and I think I will use it via AJAX to call and return the information. That has several added benefits including not requiring the calling page to be a php page.~Viz

Share this post


Link to post
Share on other sites

i am currently also trying to figure this problem, right now i can do it by making a separate file for every title, but i would rather have one page do all that, i'm thinking of using a simple changel location script

<?phpif(isset($_GET['page'])){$location = $_GET['page'];}if (empty($page)) {$page='home'; }changelocation($page);function changelocation($page) {echo "<title>";switch ($page) { 		case 'home': 	 echo "your title";	 	break; }echo "</title>"}?>

hope this helps.... if not, let me know, i haven't tried it yet -- update -- just finished checking it, on my test server it works perfect... let me give you an example of the complete code i use, feel free to take and modify it as you will

<?php if(isset($_GET['page'])){$location = $_GET['page'];}if (empty($page)) {$page='home'; }changelocation($page);function changelocation($page) {include ('header.html');echo '<title>';switch ($page) {	 case 'home':		echo 'title goes here';	 break; }echo '</title>';include ('header2.html');	echo '<table border="0" cellspacing="5" cellpadding="5" width="768">';	echo '<tr>';	echo '<td colspan="2">';	include ('title.html');	echo '</td>';	echo '</tr>';	echo '<tr>';	echo '<td width="150" valign="top">';	include ('menu.html');	echo '</td>';	echo '<td valign="top">';	switch ($page) { 		case 'home': 	include ('home.html');	 	break; }	echo '</td>';	echo '</tr>';	echo '<tr>';	echo '<td colspan="2">';	include ("tfoot.html");	echo '</td></tr></table>';include ('footer.html');} ?>

ok, just add as many cases as you want and it should work perfect
Edited by t0nedef (see edit history)

Share this post


Link to post
Share on other sites

The problem with that is that once again the loaded script has no idea what the title is, instead it must redirect to a known page or force its own title, instead of working with the title of the page it is embeded in.~Viz

Share this post


Link to post
Share on other sites

The problem with that is that once again the loaded script has no idea what the title is, instead it must redirect to a known page or force its own title, instead of working with the title of the page it is embeded in.
~Viz

ok, ok, let me think on this for a while, i might be able to come up with something, like some way for the page to report its title to the script and the script to grab that and stick it between the title flags??

heres an idea -- *i've left a couple of the essential page elements out and only posting the related portions

base page --
<?phpecho '<title> your title - ';echo $title;echo '</title>;?>

page you want the title from
<?php$title='the topic';?>the rest of your html goes here -- 
then each page would know its own title, and when you load up the article or page you want, it should load your title into it...

oh, this is only a rough draft.... lemme play around with it for a bit
Edited by t0nedef (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.