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
Related
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
I haven't't created a webpage in a long time but I decided to create on now, and probably like everyone else I wanna learn the best way of doing this. But I've stumbled upon a little dilemma on how to process my pages. My first idea was to create one main page with all the CSS and everything needed, then 1 part of the site dedicated to each page's content. Then by using a page variable showing all the content for each site, example.
I have index.php as homepage, then visiting index.php?page=aboutme would make index.php include the aboutme.php in the part dedicated to each page's content. And only having text and some pictures etc in the aboutme.php. However I believe this will be a pain when people google my site and finds interest in the aboutme.php so they get linked to example, mypage.com/pages/aboutme.php and only sees the text and pictures but no CSS and not "the front page". The pros of this is of course that editing pages will be easy, I can create links etc in php loops by just checking contents of maps on my page.
The second example is that I take everything in my index.php above the part dedicated to page content, create a separate file for this, calling it top.php. Take all thee parts under the page and call it bottom.php. Then for each new page I create I include the top and the bottom parts. Making the link mypage.com/aboutme.php include the CSS and "the frontpage". Pros being that you actually can google subpages. This seems like the best idea to me, but like I said, I haven't created a lot of webpages lately and I've seen plenty use of both methods.
I've seen both types of webpages so I kinda wondered which one is the best practice?
I recommend just using php includes for the header, nav, and footer elements and then placing a class (home, about me, contact, etc.) on the body tag (for highlighting nav elements and such). This way the content is on separate pages and gives you more freedom, but saves you from having to retype all of the navigation and stuff each time.
Example:
<!DOCTYPE html>
<head>
<title>
Hello World
</title>
<meta name="description" content="Use this area to provide a description of this page.">
<?php include '_php/_includes/head.php'; ?>
</head>
<body class="home">
<?php include '_php/_includes/header.php'; ?>
<!--
Content Goes Here
Remember: 'div' elements should only be used for non-semantic styling purposes. Content should
be placed in either a 'section' or an 'article' tag.
-->
<?php include '_php/_includes/footer.php'; ?>
</body>
</html>
I would prefer the second option with top and bottom part php files. However, this can become complicate when you need to process context information within them.
For example imagine a top.php containing a table of contents including navigation and highlighting of the currently shown page. I guess, in this situation it would be more appropriate to use your first proposed approach. To alter the table of contents depending on your current page variable (e.g. 'aboutme').
I personally like a 3rd approach which is templating. Here are two options for PHP listed in order of my preference:
http://templatepower.codocad.com/manual/index.php
http://www.smarty.net/
Using template, I suggest you create a layout template which contains all the style and "header" and "footer" of your page. Then dynamically generate the "content" using one template per page. This allows you to use your url scheme of "index.php?page=aboutme". Also, by doing this you actually don't expose the bare naked content page to google.
I've found this is the most simple and maintainable way to build dynamic PHP pages with a shared header/footer.
I've been tasked with providing the backend for a news feed that will be used by our company apps. The feed will pull articles from our current website, which is built with ModX (evolution). So far, I've designed the feed to send JSON through a specified url containing the needed information. It's currently in the following format (using Ditto placeholders):
{
"title":"[+longtitle+]",
"description":"[+description+]",
"link":"[(site_url)][~[+id+]~]"
},
Here's my issue - the link I'm providing through the JSON (in the link tag) opens the full, desktop version of the page. Our current site is not responsive, and was not originally designed to handle mobile devices. We would like to open a small, clean page showing ONLY the ['content'] of that particular article. I'm looking for a way to link to a page showing only this content - no header, no footer, nothing.
I know that I could create a new page to handle all of this, but it needs to be dynamic. New articles are created regularly, and I'd like to avoid having to add another page to handle this for every article, while also making it simple for the writing team to integrate this feature.
One of my ideas so far is:
Pass a GET parameter to the URL "link" in the JSON - something like - www.mysite.com/article1?contentOnly=true. Then, in my article, detect this parameter in PHP and handle accordingly. I would need this snippet on each article written, so it may cause issues down the road if our staff writers forget to add it.
I haven't worked with ModX long, so I'm assuming there's a better way to handle this. Any ideas would be greatly appreciated. Please let me know if I need to provide more information.
I am not 100 % sure how you have done this, but here's my tip.
Don't use the resource itself to output the JSON. Doing this based on a GET-paramter will required the entire site to be uncached. Instead, use a single resource for the feed and supply the id/permalink there.
For example: mysite.com/feed?id=1, mysite.com/feed?latest or something like that.
Done this way, you could have an empty template with just the snippet that is parsing to JSON in it. This has to be uncached of course, but the rest of the site could be cached as normal.
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.
I am beginning to develop a website as a personal project, to discover a little bit web technologies. My question is more about "how to make the things clean" than a technical question.
Here is the thing:
Let's say that an index.php page will include an other menu.php, which will permit to navigate the website.
My index.php look like this, basically:
include("header");
include("menu");
include("DEPENDING ON WHICH LINK AS BEEN CLICKED FROM THE MENU");
include("bottom");
To avoid the POST method, and have a lot of stuff on my URL, I would like to do it an other way, but I do not know what is the best one.
I thought about declaring a variable like $page, when a link is clicked, I can do something like "if $page == home", then I include the home.php page... etc...
I do not know if my question is clear... I know that it will appear as a very easy and beginner question but I don't even know where to look...
Do you know if I can find any "open source website" so I can study the code and see the best practices about it?
P.S.: Sorry for my english which is probably not perfect at all, I am working on it.
You can have a menu like
Home
About
Then on your PHP code
include $_GET["view"] . ".php";
Note that I am not validating, so any parameter passed on the url would be able to include any file.
The $_GET returns the values passed to the page through the URL.
The $_POST returns values posted.
The $_REQUEST returns both $_GET and $_POST values.
A good place to study many languages is W3Schools, you could check there sometime.
Make a page which will be common redirect page.
Every post will come to that page and based on the page parameter it will redirect.
So action of every page is same, but based on page paramter redirect to which ever page you want
You can switch case and
use header to redirect
i think you want to avoid GET method and avoid lot stuff in url
For learning
I thinks this is the simple website for learner.
http://www.w3schools.com/php/default.asp
http://www.plus2net.com/php_tutorial/site_map.php
http://www.tizag.com/phpT/
actually most of all these websites are same.
i hope you know the basic website PHP.net
Then,,.. no one is low level ..every low level will be in a top level one day.. just like you am also trying :)
Don't do what you are trying to do. The whole point of having pages is to handle things with different files. That is, you will have some commonality between files (handled by auto prepend and include path, potentially) such as your header and footer. Each file should include this on its own and print it out directly.
That is, you should not handle everything on one page and then conditionally include a file. Just send users to a different page.
Finally, I recommend not splitting up the header/footer files at all. Instead create a decorator that wraps the main content and displays it all at once. Something like:
$page = <<<HTML
<html><head><title></title></head>
<body>
<div id="top nav"></div>
{CONTENT}
</body>
</html>
HTML;
Then you go through and build your page content. Then you add it to CONTENT in the decorator and print it. PHPTAL is a great way to have this handled externally.
Hi you should please ask one question at a time:
I think this basic tutorial will give you a good idea on how and what to use the include(); func.
http://www.w3schools.com/php/php_includes.asp
I started with:
http://www.solitude.dk/filethingie/
Very simple .php file administrator.
You should definetly check out sourceforge, giant colletion of open source projects just filter by php (search for literally anything).
Just wanted to mention that you can download the full code of more complex pages (that are based on php) like
wordpress (blogging platform) - very easy to install and configure
identi.ca (twitter open source alternave)
You can now download reddit´s source code - quite easy to.
Maybe you wont be able to modify them immeditelly but theyll help you to get the picture