Rashid Farooq
Rashid Farooq

Reputation: 365

What is the standard way to get the title of the page

My webpage Urls are such likes

url = http://www.example.com/2223/this-is-the-url-text-of-page

In the above URL 2223 is the id and this-is-the-url-text-of-page is the static text that is stored in the database in a different field. I retrieve the webpage data on the base of id.

But the Title is some thing that has to be shown immediately before any thing else. So, If i retrieve the title from the database and then show it on the top of the page, I think it would not be an efficient way. What is the standard way to show the title of the page.

Do Most sites retrieve the title from the database?

OR

Do Most sites append the title in the URL?

OR

Is there any other way exists?

Upvotes: 0

Views: 188

Answers (8)

tgkprog
tgkprog

Reputation: 4598

You mean get the title when constructing the page -> then get it from a separate field from the db. store friendly url and title separately.

also if theuser does not enter the title part in URL just use the id to redirect to the correct page.


No longer relevant:

I assume: are you trying to build a set of links dynamically, of pages in the db (and you might have new pages at anytime or old ones removed) and so need to get the title only?

For this -> you could run a cron job 20 minutes and make a static include page. Write to a new page and when done, delete the main include 'links.html' and rename the new page to this.

Can even have a link in the admin section to run this any time

Please give more context. there are no standard solutions. or rather there are 3-4 depending on the situation.

Upvotes: 0

symcbean
symcbean

Reputation: 48357

and then show it on the top of the page, I think it would not be an efficient way

It rather depends. The title is embedded within the HTML, near the top. While there are ways of injecting this later, in practice it's usually not worth doing. PHP/HTTP performs best when output buffering is enabled - and the content is sent to the browser in a single burst - hence the 'efficient' solution is to send nothing to the browser to the browser until the full content is availalble. If your script is very slow (taking more than about 4 seconds to generate) then, depending on how the DOM is constructed, it may be possible for the browser to begin rendering when the HTML is incomplete - in this case forcing chunked encoding can improve perceived performance - but this is complex and difficult to implement.

Further, the first bit of information sent in the response is whether the request behaved as expected - was the user authenticated? Did anything go wrong processing the script? Was the content validated OK or do you need to redirect the browser back to the previous page? In an authentication transition you also need to drop cookies (i.e. modify the headers) after processing the request. You shouldn't supply the status until you're reasonably sure that you know what the enswer will be - checking the database is working is an important part of that. If you start producing output too early then you're returning incorrect data in your response headers and reducing your options for handling error conditions.

So both from the point of view of performance and functionality, it makes a lot more sense to delay generating the output until you know what the output will be.

Upvotes: 1

pulsar
pulsar

Reputation: 987

this function may be faster than exploding, especially for longer URLs:

basename($url);

Upvotes: 0

Breezer
Breezer

Reputation: 10490

Using the database to retrieve the title with the rest of the page is to recommend. Why? Well to check a id is no more to check there's only number contained in the var, but a string on the other hand has a wide range of what it could and can't contain, making it harder to create rules for it which in return opens up for security issues.

In other words just simple add a join to your query and you'll be good to go.

Second if you use mod_rewrite to create those url's the regular $_GET variable is still available.

Third if not you could use somekind of function like end(explode('/', $url));.

but at the end as I said earlier the database is to recommend, it's more secure then the global var method, and it's more reliable then the "run function" on url string method.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59997

Get the title from the database. The URL should be short, to the point and eaasy to type in. The title should be descriptive. That means that it can be longer, punctuation and be a lot longer.

Upvotes: 0

Do Most sites retrieve the title from the database? : YES

Do Most sites append the title in the URL? : The title of the page and the url are not the same thing.

title : This is the URL text of this page
url : this-is-the-url-text-of-page

There is two ways to do this. You can store the title AND the url in the database or you "construct" the url progammatically from the title. First solution is more efficient.

Upvotes: 2

Ian Cant
Ian Cant

Reputation: 403

Generally in my experience there are a few factors to consider:

  1. The actual page title may contain information that is lost in the friendly url - eg. punctuation or extra words. Do you really want a title that should say "I Can't Access the Internet" to actually say "I Cant Access The Internet".
  2. To generate the page I would usually expect further information would be required from the database too. Maybe article content, keywords, header image etc. Therefore the database query would be there anyway.
  3. The primary purpose of friendly urls is from search engine optimisation. As you are also passing the id of each page, do the database look up on the id. So long as your database tables primary key is also on the id, this will help increase your lookup speeds.

Hope that helps answer some of your questions.

Upvotes: 1

Silvertiger
Silvertiger

Reputation: 1680

the title of a page is defined in the html header as such:

<title>php - What is the standard way to get the title of the page - Stack Overflow</title>

This can be static or dynamic depending on the page and how it's built by the designer. not sure what the question is if you're trying to reference a database since there's a whole mix we don't see there.

what are you trying to accomplish?

Upvotes: 0

Related Questions