Converting a .php file to a .html file? - php

I have a .php file which has several queries in it. I want the output file as a .html file...
Is there any way to do this. I am currently doing this by saving, using my browser, that executed PHP file, as an .html file. But when I launch my product that should not be the case of the client.
Please suggest a way, thanks in advance...

Here is a sample code:
<?php
ob_start();
// your PHP / HTML code here
file_put_contents('where/to/save/generated.html', ob_get_clean());
?>

Not sure if I understand the question right.
But you can generate html files from php over command line:
php index.php > index.html

Another option is to use apache's mod_rewrite or the IIS equivelant to rewrite your URL's from the browser perspective.
This would require no coding change, make sure the Apache extension is installed and add this to the .htaccess file of your root web directory:
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]*)\.html$ $1.php?%{QUERY_STRING} [NC]

you can use output buffering (ob_start, etc.) and then write the content of the buffer to a file at the end of your script

Is this (this) what you need?
If these are not options, you can always curl to the page if it's running on a web server/

It looks like you want to save a static version of a dynamic web site. A tool like WinHTTrack comes in handy.

Related

PHP isn't working in file without extension

I had this setup:
images
image1.jpg
image2.jpg
header.html
about
index.php
image3.jpg
But going to MyWebsite.com/About gave it an extra slash on the end. I decided to go with the solution of creating a file called about in my home directory:
images
image1.jpg
image2.jpg
header.html
about
aboutfiles
image3.jpg
The problem is that now this file won't let me use .php:
<?php include('header.html');?>
It's not showing the header file. What can I do to make this work?
Your problem is that your web server does not recognize the filename "about" as a php document. You have three options
Use a ".php" extension on your page document, such as "index.php" which will run your php scripting.
Install a solution such as mod_rewrite that will translate urls such as /about to a file actual like "about.php".
Adjust your servers mime-type for php documents. Learn more about MIME types here. https://developer.mozilla.org/en-US/docs/Properly_Configuring_Server_MIME_Types
The first solution is the simplest and easiest, in any document you use PHP, the extension should be .php
About the mywebsite.com/about issue, it's more an Apache configuration issue. You have to tell Apache that index.php should be an index file.
First, you can configure your Apache (or IIS) to use whatever Extensions to process PHP-Code.
You can define .ThisIsAPHPFile as valid extension, if you want.
However, Directorys are always reflected with a trailing /: www.example.com/dir1/ (Browsers not always showing the trailing /) while files have an extension: www.example.com/dir1/index.html.
So, from what i see, you want to use www.example.com/about but showing the about-FILE ?
Therefore you can use rewrite Engines of your Webserver. Either have a look at mod-rewrite (http://httpd.apache.org/docs/current/en/mod/mod_rewrite.html), when using Apache, or (one possibility) ISAPI-Rewrite (http://www.isapirewrite.com/docs/), when using IIS.
If you edit your virtualhost directive in your apache conf file, you can add the following:
DefaultType application/x-httpd-php
This will tell apache to send web paths that do not end in an extension to render using the php engine.
Therefore, /about would act as if it was about.php. Another potentially more useful approach is to name the file about.php on the server, and allow referencing it without the .php in the url. For this, you would configure it the opposite way.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

PHP in HTML files not running or being commented out

First of all, I am not trying to run php within a js script, there is a similar question on here that refers to a user trying to run php from inside a js script.
I have added many combinations of
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php .html
to the .htaccess file in a higher level directory containing the .html file I want to run php in. This has not worked. (I am open to trying new combinations)
The is either not read at all or commented out when viewing the source in broswer.
My question is how to get to be run inside of an html file OR is there a better way to include php functionality in an html document without having the code in the same document.
Additionally my host uses cpanel if this helps anything.
I can elaborate on anything I need to, thanks in advance.
You can try to use mod_rewrite for that task:
RewriteEngine On
RewriteRule ^(.*).html index.php [QSA]

Access a stand-alone php file with WordPress installed

I'm wanting to upload a standard php file to the root of my website which also has WordPress installed. I think there is something with the .htaccess file that's not letting me access a file even if it exists but instead passing every call through to WordPress's index.php file which in turn throws me to a WordPress 'page can't be found'.
I've looked through similar questions on here with no avail.
What I'm thinking is there is something funky going on, or I need to modify my .htaccess from the standard one that ships with WordPress or ??
Any help is appreciated!!
Thanks in advance,
Mark
If your file is called foo.php, add this line to your .htaccess file, directly below RewriteEngine on:
RewriteRule ^foo.php$ - [L]
Add a RewriteCond with the name of the file you want to access, that checks if the current file is the file that you want to access.

Rewrite path to my files

I have a JS file that has a path to another script. If I do not want to reveal my directory structure, what would be the best way to obscure it?
For example, can I add a rewrite rule in .htaccess file and use that in my JS file or is there a better way to do that?
Current JS file:
URL_PATH = '/incl/pro/dir/files/server.php';
// change to:
URL_PATH = '/dir/server.php';
Create a .htaccess file in your DOCUMENT_ROOT location with following content:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^dir/(.*)$ /incl/pro/dir/files/$1 [L,NC]
Rewrite rule will take care of forwarding your request of /dir/server.php to /incl/pro/dir/files/server.php internally.
Yes, this would be a good use of .htaccess files to obscure the actual locations of files.
I'm a bit confused by what you're trying to ask here.
A js file is executed on the client machine, not the server machine. So whatever paths you may put on a js script will never be executed on the client machine unless they happen to have that exact path.
A better alternative is to not include paths to a script. Instead have a ajax call to that script. You can't trust that whatever hitting that .php script is from a js file or from someone manually typing it in.
For example, if your site dir is at /incld/site/scripts/yourscript.php and in your js file, you can make an ajax using jQuery to GET the content of www.yoursite.com/scripts/yourscripts.php and parse out whatever you need or execute a script that is required.

Create a RESTful url for a php script

I have a php script on a webserver. Currently, the script is being accessed as http://www.mydomain.com/scriptname.php .
Is there a way i can create a user friendly url for accessing this script, something like http://www.mydomain.com/appname, so when this url is called it invokes the php script ?
Please help.
Thank You
You want mod_rewrite if you're using Apache HTTPD: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
If you're using a different web server, it may have something similar (lighttpd has a similar functionality builtin).
Once it's enabled, you can use something like this in your .htaccess file to rewrite appname to scriptname.php
RewriteEngine on
RewriteRule ^appname$ scriptname.php
If you don't have access to your apache/lighttpd configuration file a little hack that may work is putting the script in http://www.mydomain.com/appname/index.php; http://www.mydomain.com/appname/ will then probably work.
Using the default settings of most PHP hosts, you can put the file "index.php" inside your folder http://www.mydomain.com/appname, then fill the index.php with next code:
<?php
header("location: http://www.mydomain.com/scriptname.php");
?>
That would do it.

Categories