php title in URL - php

I'm building website in php.
For me is important to have pages with similar title and url.
Like this:
Title -> some content
URL -> some-content.php
Each page has the content (articles) retrieved from mysql. Title also is retrieved from mysql.
So my question is: should I generate a .php file for each artivle or it is possible to have one php file with changing content and URL?
How whould you do it?
Thanks for the attention

Definitly you shouldn't define new files for each article. You should have article controller in this controller you need retrieve articles from your model. In Articles you can have slug or id and pass this slug/id to template.
You could also check mod_rewrite to have nice and seo friendly urls.
Some pages to read:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
http://cubiq.org/the-perfect-php-clean-url-generator

The "piles of PHP files" design pattern fell out of style over a decade ago. DO NOT DO THIS.
Modern PHP development encourages the use of a development framework like Laravel that gives you a solid foundation for building your application.
Most of these have a robust routing system that takes care of presenting clean URLs to your visitors while allowing significant flexibility in how those URLs are handled internally. This is a huge advantage to someone concerned about how their site is organized.

No need to make a PHP by page. In your html <head> you can change the title dynamically :
<head>
<title><?php echo $titleFromDB; ?></title>
</head>
Don't forget to escape/protect the variable if the title can be edit by users.

I suggest you use a framework like codeigniter which can handle the required structure of your files (MVC approach) and also helps you create SEO friendly URLs.

I would definitely use one page with a changing url, title & content. It means 1 place to go when any issues occur.
Just make 1 page and use GET variables in the url to change what content is loaded.
e.g. test-page.php?content=content-id
this link is created using the id of the content from the sql table, you then get this id when the page is opened and use it to get the rest of the details from the database that will be used for your title and content.
I hope this makes sense

Related

Generating dynamic HTML pages with PHP

I've been stuck up on this topic for a while. As a project to help me learn MySQL, PHP, and database security a while back, I created a lightweight blog system. It blew up on me and a few of my fellow programmer buddies wanted to use it and now I am working on improving on its features.
The way it works, simply put, is that a blog post is uploaded, the title, author name, and date are stored in a database, but the actual content is used to dynamically generate an html page which is where the actual blog post is viewed. In the database, the path to each blog post is saved so that it can be used to access and view the blog post page. One of the features of my blog system is that the generated HTML is fully customizable, so you aren't limited by a specific template. The way I am currently doing that is by using a function that takes parameters of the values and then returns a HEREDOC with the format desired, which is then written to a file whose name is based on the post title.
function get_full_post_html($title, $author, $date_posted, $text) {
return <<<EOT
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<h3>$author</h3>
<h4>$date_posted</h4>
$text
</body>
</html>
EOT;
}
Not the cleanest solution, which is one of the reasons I am looking for a better way. The other reason why I need to rethink this, is because I am currently adding support for comments, which means the html page that is generated now needs to be dynamic, with my PHP method for getting the comments. Whereas before the blog posts were static content, now with comments enabled, the page becomes dynamic.
I had a look at this question, but frankly the answers were vague and didn't make much sense. The main question I am asking is, how are dynamic pages typically generated in PHP? For example, take this blog post on A List Apart: http://alistapart.com/article/tweaking-the-moral-ui. It has a physical page for the post, tweaking-the-moral-ui, but still has dynamic features like comments, ads, etc. How is this done?
Here is the link to this entire project on GitHub, if you are interested in understanding how it works in depth.
I think there is a starting mistake here.
You are writing pages to file to have a nice url instead of using a rewriteurl logic.
if you want to make them "dynamic" you can regenerate the file every time a user add a comment.
Or follow this answer of another question:
http://stackoverflow.com/questions/16388959/url-rewriting-with-php
So basically you add comment to your blog platform, and you regenerate the page once per request as many platform do. In your index.php you implement some logic to research from the url the post you need.
The pages will be visible because linked in some listing. In the listing instead of:
http://myblog.com/mypost.php?id=33
You will have
http://myblog.com/i love my dog/
Then in your index.php you will analyze
$_SERVER['REQUEST_URI']
and serve the correct post to the user or to the web crawler of search engine

How do I load content based on URL without hashes or php variables?

Let me know if this question needs more clarification.
I am a front-end developer, and I usually use Wordpress with lots of custom fields to put together a CMS for clients.
A current client wants a design portfolio site that initially presents a grid of images that link to projects, but instead of loading a new page on click, the new content loads and fades in smoothly.
I figure the simplest way to do this kind of thing is to load everything up front on one page with ajax (a loading screen is OK), and then just show/hide/move content with jQuery.
The request I am having trouble with is being able to have specific URL's for different projects and images. The client wants a URL scheme like here:
http://collins1.com/work/bp-helios-house/3
Where the number at the end causes a specific image to load in the given project. It seems like this would be simple enough using php variables where like:
http://www.whatever.com?project=3&image=2
And using those to manipulate the initial AJAX load.
But how is this accomplished using a more traditional (pretty) URL structure like the example? If I am building the site as one page loading content, won't the browser attempt to load that as a page and just come up with a 404?
Bonus: How do you change the URL in the address bar to create these links as the user navigates the site without reloading the page?
Thanks!
what you see there, is called url routing. Basically, some server rule that rewrites the url in a proper way, depending on server and scripting language used.
for example, the url
http://server.com/foo/bar
MAY be redirected to
http://server.com/index.php?foo=bar
If you need a lightweight framework to handle this, take a look at www.slimframework.com
If you want real pretty urls you are going to need a server-side framework for url routing and will require you to get into php or ruby on rails. If you want a pure front end solution you can fake it in javascript using hash fragments. For how to do this see
http://backbonejs.org/#Router
http://www.asual.com/jquery/address/
http://benalman.com/projects/jquery-hashchange-plugin/

Prestashop - custom template link

I'm starting with "PrestaShop" and I just can't figure out, how to put a link in template to custom page I created in CMS module... I thought, there might be some easy way, as there is in WordPress, like "get_permalink(ID)", but there's nothing like this and I can't find anything about this anywhere and it just drives me mad.
So, here's the deal, I've got a custom template, and there are some top links, like "About Us". I've created this page in CMS and it has ID "6".
How do I make this bloody "PrestaShop" to generate a link to this page in my template file?
About
I think you're looking for SMARTY template tags and custom variables defined for Prestashop specifically. The one the you probably need is {$base_dir} which will be translated to http://www.yoursite.com/ obviously with appropriate protocol (non-secure HTTP or secured HTTPS).
After that, you only need to include page URL, which you can get from Admin->Tools->CMS section.
If I find any specific tags that you can use to call the content, I will update my post here.
Your Text
the "WHAT goes here" is just the url you want your link drives the client to when clicking on that link.
You need to understand a little minimum of HTML for this I guess. Check html tag a meaning .
Prestashop has a fairly good and extensive documentation. Two and half seconds googling drives me here, just like answering your question it looks like.

Multiple Dynamic SEO/shortened URLs in PHP

I have a PHP system containing user-generated pages, arranged in a complex and non-uniform hierarchy. Pages are created by users, and some pages have sub-pages etc.
I have been asked to add a shortened-url system. So any page, at any point in the hierarchy, can be accessed via domain.com/XXXX where XXXX can be anything - we are not interested in SEO here, the reasoning behind this is its a very social-media driven project, and we would like our users to be able to tweet/other the url of any page they like.
I expect something like; we start on AAAA and head towards ZZZZ as each page is created. Each of these slugs would be stored in the database alongside the actual url eg domain.com/projects.php?p=32
I know mod-rewrite enough to convert domain.com/XXXX into domain.com/index.php?slug=XXXX, but where to go from there leaves me a little stumped. index.php can do the database lookup and header() the user to the actual url, but the slug-url needs to stay in the address bar.
Would using an iframe in index.php be a terrible idea?
I hope thats clear, thanks for reading!
If you used the [R=301] directive at the end of an .htaccess rewrite rule, it will act as a redirect. Meaning if you go to domain.com/XXXX it will show domain.com/index.php?slug=XXXX in the address bar. Is that the behavior you're trying to accomplish?
Also, I wouldn't use a header(), I'd make your index page be the processing page. Either that, or use an include() method instead.
I think using an iframe is a terrible idea, and will lead to a brittle site.
Is there any reason why index.php can't act as a front controller, so instead of redirecting it just shows the page? You should just be able to include the page.
Alternatively, could you not rewrite domain.com/XXXX to domain.com/projects.php?slug=XXXX, and do a slug->p conversion at the top of projects.php? Then the conversion would just need to record slugs and page ids, rather than slugs and full URLs.

How to Have Search Engines Index Database-Driven Content?

How can I make it so that content from a database is available to search engines, like google, for indexing?
Example:
Table in mysql has a field named 'Headline' which equals 'BMW M3 2005'.
My site name is 'MySite'
User enters 'BMW M3 2005 MySite' in google and the record will show up with results?
Google indexes web pages, so you will need to have a page for each of your records, this doesn't mean to say you need to create 1,000 HTML pages, but following my advice above will dynamically / easily provide a seemingly unique page for each product.
For example:
www.mydomain.com/buy/123/nice-bmw-m3-2005
You can use .htaccess to change this link to:
www.mydomain.com/product.php?id=123
Within this script you can dynamically create each page with up-to-date information by querying your database based on the product id in this case 123.
Your script will provide each record with it's own title ('Nice BMW M3 2005'), a nice friendly URL ('www.mydomain.com/buy/123/nice-bmw-m3-2006') and you can include the correct meta information too, as well as images, reviews etc etc.
Job done, and you don't have to create hundreds of static HTML pages.
For more information on .htaccess, check out this tutorial.
What ILMV is trying to explain is that you have to have HTML pages that Google and other search engines can 'crawl' in order for them to index your content.
Since your information is loading dynamically from a database, you will need to use a server-side language like PHP to dynamically load information from the database and then output that information to an HTML page.
You have any number of options for how to accomplish this specifically, ILMV's suggestion is one of the better ways though.
Basically what you need to do first is figure out how to pull the information from the database and then use PHP (or another server-side language) to output the information to an HTML page.
Then you will need to determine whether you want to use the uglier, default url style for php driven pages:
mysite.com/products.php?id=123
But this url is not very user or search engine friendly and will result in your content not being indexed very well.
Or you can use some sort of URL rewriting mechanism (mod_rewrite in a .htaccess file does this or you can look at more complex PHP oriented solutions like Zend Framework that provide what's called a Front Controller to handle mapping of all requests) to make it so that your url's look like:
mysite.com/products/123/nice-bmw-m3-2006
This is what ILMV is talking about with regard to url masking.
Using this method of dynamically loading content will allow you to develop a single page to load the information for a number of different products based on the Id thus making it seem to the various search engines as though you have a unique page for each product.

Categories