Why would you host html pages instead of php? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
If I'm hosting a website on a server that has PHP, would I ever need to host a .html file on it? PHP files can have raw HTML code inside of them as well as the added benefit of being able to inject PHP code, so isn't PHP a more inclusive language?

Serving HTML is generally a lot easier for the webserver than serving a page that has to be run through the PHP interpreter.
This can have huge benefits on high-traffic sites. For example, the Obama campaign ran their fundraising platform with a lot of static HTML for that reason.

it always can be situation that you would need to add e.g. maintenance page in pure html

It's not a question of whether you can simply make everything "a PHP file" but rather a question of whether any given file needs to be PHP. If a file contains only static content, there's no need for any server-side processing. So in such cases it would make more sense to make it an HTML file.
Don't look for a tool that covers all cases, look for the right tool for each case.

I read that it is recommended to have <?php flush(); ?> between </head> and <body>. I would assume that this should apply to every webpage, there really isn't a solid reason as to why you would only use HTML.
I agree with ceejayoz, but if that's not a problem for you, then using PHP is great.

It depends on your purpose. If your page uses PHP then the file extension needs to end in .php. If you don't have any PHP then you can just save the page with a .html file extension. A major difference is that PHP is processed on the server side. Meaning the user(client) will not see your PHP code if he views the source, he'll see what was processed from the server in the form of HTML.

One advantage of using .php files over HTML (in this trivial case) is that, you can wrap up all your footer files(if the are the same) or any redundant files, like sidebar, advertisingand just include them in your other files, instead of having to create/manage individual files later, So, you can just open one footer.php file and make as many changes as you want without wasting any time.

Your basic understanding is correct. You can absolutely have a .php file that has only HTML in it and given a simple page and a simple site there would be little difference.
However, the PHP preprocessor adds a bit of overhead to check for PHP code in the file. In most cases this is insignificant, but on a highly optimized site you probably would not want the pages to be processed for nothing.
I wouldn't say that PHP is a more inclusive language though, HTML and PHP are two different things. In many cases (but by no means all) PHP generates HTML. It's just that the resulting HTML from a PHP script that has no PHP tags in it would most likely be the same as it would be for an html file. Although it is likely that HTTP Response Headers would be different and there are other things outside of the file content itself that could be slightlu

Related

why is php written in HTML file since HTML is loaded and run on server side, but PHP only on server side? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i don't understand why is PHP embedded in HTML when its only running on server side and HTML file is loaded in to the browser on client side.. ?
PHP is a server side language which can be, and very often is, used for pre-processing of the output from the server to the client. When the client asks the server for a particular page, for example a product detail page, a calendar or a blog article, PHP will get the data to use on that page from a database or other storage technology and send to the client.
Now if PHP is putting that data into the HTML on the server, the client receives an HTML document complete with all the data as well as the document structure, HTML tags and, unless hosted in separate files, CSS styles and Javascript code.
The most basic way for PHP to do this is inlining the HTML like so in a .php file:
<h1><?php echo "Hello World!";?></h1>
Which will produce HTML for the client that looks like:
<h1>Hello World!</h1>
Which will display as:
Hello World!
In this very simple example it might be hard to see the benefits, but as soon as you start using dynamic data, perhaps as simple as displaying the current day or showing some other piece of information that changes, you can see the benefit of using something like PHP for that task.
Once you create, say a php file that contains HTML with embedded PHP, and the browser client asks your server for that file, your entire file is still on the server and untouched.
But right before the file is delivered to the requesting user, the file is processed (if you have your server to process .php files, that is) and the embedded PHP is replaced with it's output. Usually, the embeded PHP code never reaches the browser client, only the processed file does.
Imagine reading an html file like a book. The language of the book is html. A webserver, such as apache, takes the html code and sends it to the user. It is your browser that interprets it. What your browser receives is pure text.
Now, the apache has a PHP extension. So, when the webserver reads the html code, and suddenly stumbles upon < ?php ?> tags, it does not send it to the user. Instead, it sends it to the PHP server.
The PHP server processes the code and returns back only pure html code. Then everything is sent over to the user.
PHP will only process things that are enclosed within one of its valid code blocks (such as <?php and ?>). Because of this, PHP effectively ignores everything that it was not specifically told to process and can be used to our advantage. For example, what will the output from the following be?
<?php
$var = 5;
?>
$var = 10;<br />
The variable $var has a value of: <?=$var?><br />
Is this a valid script? Yes, the output would be the following:
$var = 10;
The variable $var has a value of: 5
Notice that with the second assignment of $var, when we attempt to change the value from 5 to 10, it has no effect because it is not enclosed within valid PHP code-block syntax. So, instead of being processed, it is simply displayed to the web browser.

Should php be used to make every webpage on a website or only when some database query is involved [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
New to the world of php and dynamic websites I'm consfused as weather to use php from now on even to make basic webpages or continue making webpages with html and only use php when some data Insertion,Retrieval etc.. action to a database using SQL queries is required.. from a website
Don't know if I answered my own question
Clarification is highly appreciated
Thanks
I'm not sure what you think PHP does, but if there's no server-side functionality to put on a particular page then there wouldn't be any PHP code to write.
For example, if you have a page like this:
<html>
<head>
<title>My Page</title>
</head>
<body>
<p>This is my page!</p>
</body>
</html>
(Obviously an over-simplified example, but you get the idea...)
Well, if that's the page, then you don't need any PHP code. So why would you "use PHP" when there's no code to write for it?
When there's some server-side functionality that needs to happen, write code for it. When there isn't... well... don't write code for it.
If there are no dynamic elements within the webpage, then it is not necessary to write the page in a PHP file; an HTML file will suffice. That said, I would recommend saving the page as a PHP file anyways, because you may find yourself needing dynamic PHP functionality later, in which case you would need to convert the file then. By using PHP files in the first place, you will likely be saving yourself an extra step in the future.
PHP is not used just for contents saved in database, but also for contents that are static also such as header and footer section, imagine that your website is about 50 pages, and if you want to change one menu link! you have to edit all 50 web pages while using PHP is only needs to change one file that you included for main page.
In my website, I programmed those section with PHP:
Navigation menus
Header and Footer
Blog section
I use .php pages for all my webpages because eventually, I use a php code inside any of the pages to call from the simplest (header, footer) to buttons, insert texts, codes, anything that may need repetitive work but can be linked to its relevant .php document. And if any change is needed, one can simply change the one page and it will change everywhere. Dynamic works for me, even if I do not necessarily add a database to it.

On what circumstances should we NOT use php inside javascript? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
$('#tip2').qtip({
content: {
text: '<?php echo "I do work."?>'
},
In this case I'm using a jquery plugin but that doesn't matter for the question I believe.
I thought I had to go trough json calls (or other ajax call methods) because php runs first, and only later runs javascript, but, I actually tested this code, and it actually displays the text.
Can anyone please take some words to explain, on what circumstances should we NOT use php inside javascript. Is this one of them? If so, why?
I'm clearly not getting some client side, and server side workflow... please forgive my newbielity towards those matters.
Sorry if this was an inconvenient question.
Update
Please note that I'm asking about facts. Not opinions.
Someone states:
"You shouldn't do that way."
I'm asking:
"What facts (not opinions) sustain that claim?"
It might be sensible to externalise most, if not all, of your JavaScript to another, static file for caching reasons.
It is possible to have a PHP file generate JavaScript, but as all dynamic content, it can not cache as well (while you can set-up caching headers, changes to the generated content will not be immediate, so you'll be forced to find a compromise between content accuracy and bandwidth savings).
I would externalise your example as so:
$('#tip2').qtip({
content: {
text: my_var
},
Then create the following <script> block in your dynamic file before linking the static one:
my_var = '<?php echo 'I do work'; ?>';
This way, the vast majority of your JavaScript will be static, and eligible for Max-Age caching.
To provide you with a real-world example, I have recently implemented a pure JS plotting library that draws discrete data onto a canvas. The data to be plotted is supplied by the CGI (I didn't use PHP, but it shouldn't matter), so I used this approach to declare an array of values in the dynamic file, while externalising the actual handling and drawing code. It works beautifully, uses minimum bandwidth due to caching nicely, and is still very maintainable.
Unlike some other answers, I won't say you should avoid mixing server-side code with client-side; hell, HTML is client-side code that you are generating server-side. Depending on your needs, mixing JS and CGI can extend your possibilities, and applying my caching trick, there is no real downside.
A php file is interpreted by the web server and converted to a pure html file. That html file is then passed to the client's browser, which displays it.
In your case, the php echo is rendered as a string in the html page.
Keep in mind that if you do what you did in your example, the values rendered to the page are the values that exist when the page is rendered by php.
You should try not to use it as much as possible. However, there might be some very rare cases when you do need to pass values from your PHP code (say from a $_SESSION array) into your jQuery/JavaScript functions. In that case, if you don't have any other option then you can pass values like you did above. But in all other cases, try not to do it as much as possible since it is not a good practice. When you are combining 2 different languages like this, there might be cases when you get unpredictable behavior from the application/web-page.

How to organize or use pre-exsisting html into a new php application? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Looking for ideas on how to go about working with a large set of .html files (50,000+) that are preexisting.
What I am trying to accomplish is to be able to keep the same hierarchy, links etc... but add content to them via php or whatever etc..
To be able to organize or route them into a php application or my own cms. I have been thinking ways to do this and come up with a few ideas but hoping to get some feedback from some pros.
I really only need to create something that will get the data once and add it to a cms that I develop. I have a script that I wrote to be able to index and search etc.. but how to go about using the contents and adding to them while keeping structure has me swinging a little. Any thoughts?
UPDATE: I found some tips that made this easier for me as well
Use a .htacess file and ad:
AddType application/x-httpd-php .html .htm
Then I can add includes into those files, I am note sure where to put this in the config file yet but will update when I know.
If you were to use a CMS like Wordpress, assuming your content follows some sort of regular structure, you could recreate the HTML frame as templates and then write a script that parses each HTML file and creates a post based on criteria like the HTML file name.
You can even match the file name by setting the slug on post creation, though you'll need a server rewrite rule to drop the .htm(l) if you intend to keep all the backlinks to the site.
I'm oversimplifying of course, as with 50,000+ files it isn't a trivial job to move all the data.
Perhaps automate it using something like this: http://wordpress.org/extend/plugins/import-html-pages/
The way I would go about it is to keep the html in a function which you require. Separate them logically as:
function output_html_one($link_here, $or_link_there...) {...}
function output_html_two($other_links...) {...}
When you want to output them with the files you have, just call the function, and it will be sent out. I am sure others will give you preferences. This allows you to give them access to variables through arguments, which is a downside, but it keep everything organized and provides some clarity as you can separate the raw html from the logic in your code. I am very curious to see what others say.
EDIT: I added $user inside of html_header so its clearer. For example, lets say you might want to check if a user is logged in. This gives you a way to tailor your html. Then you go to the files which contains this, you can jump in and out of the php tags in order to add dynamic content.

Advice: building a non-database driven simple CMS in PHP [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a site which requires a very simple CMS - basically there's a block of text on the homepage that needs to be editable by the client. Their current hosting plan doesn't allow for a database, and including one will cost an extra $X a month which I think is unnecessary for such a basic system.
The site is currently built using Codeignitor. I'm planning to write the CMS part of it using either flat PHP or TXT files, are there alternative methods worth considering, and what are the pros/cons?
Okay, so further to this, I've opted for a custom flatfile system. I looked at a few of the recommended non DB CMS systems and they seem quite good - particularly this one which I later found: http://get-simple.info/
The reason for building my own is mainly due to the fact that the site is already on the Codeignitor Framework, and I don't want to rebuild it using a different one.
So my question now is - if my system is storing data in two txt files: one for userdata and one for site content, are there massive security issues if I set the sitecontent file permissions to RW? The site is quite small and I can't imagine anyone would want to hack it, but I'd still like to know if there are any major security implications.
Try http://www.opensourcecms.com/
example of some that might interest you
PivotX
pluck
razorCMS
cushyCMS
its ftp's into your hosting account, reads your html, and looks for tags that have a class="cushy" and makes those content feilds editable. its good forwhat your wanting.
I once solved this problem by simply putting markers in an HTML file, as HTML comments, and then had my PHP script parse the file and insert the desired text in between the markers. Done this way, you need no other files other than the PHP that handles the form submission from the CMS and a static HTML page.
In words, read the file into a string, explode() the string using the marker as the delimiter, modify the second (if you have a single editable section enclosed by the markers) array element to contain the new text submitted by the user, then implode the array back into the string, then write the string back as the complete file.
What about sqlite? it is just a file, no need to install anything? But if this is also not welcome, you could just keep the contents in txt files and have a php to read it, put to your template.

Categories