how to include php script in html file
This is duplicate questions. Please refer this link. I hope Its will help you.
How do I add PHP code/file to HTML(.html) files?
Thank You!
Apache - How can I run a PHP script when opening HTML files?
If you serve static HTML files, you cannot prevent a user to access it without using a programming language like PHP (or e.g. .htaccess
). There are however multiple ways to handle your situation:
- You could fetch all administrative data from the PHP scripts with AJAX. This means that all your users could theoretically see how your administration is built of (the whole HTML structure without relevant data). Of course, you can create a redirect, if the AJAX call fails, but you are still offering the whole HTML of the administration.
- You can store your administration HTML file in the back-end. The user then tries to access the administration and the only thing he gets, is an AJAX call to the back-end. If the call is successful, the HTML is delivered with AJAX to the front-end. This prevents the users to see your administration HTML.
- You can use a small PHP snippet on top of every administration page which checks if a user should have access to this page. This prevents the users also to see your administration HTML.
- You store the HTML of your administration in an HTML file, also served to the user. Then, you make an initial call to the back-end on page load. In the success case, you make another AJAX call to fetch the administartion HTML. The user could potentially see the administration HTML (if he directly opens the file).
- You could use a PHP independent server authentication with
.htaccess
..htaccess
is an apache server authentication configuration file. You can use it to prevent a user from accessing your site (so, there is no access to the HTML file at all). When trying to access the page, a popup appears and the user has to enter his credentials. It is completely independent from your business logic and the allowed users have to be set in a.htpasswd
text file. There is no way to use a modern database-relying user management with.htaccess
server authentication. With this solution, you also cannot use an HTML form to log in. I would only recommend this solution for testing purposes and not for a modern website.
If your PHP back-end is solidly built, I would go for solution 1
. The user cannot do anything with your HTML, if you are properly checking the user's input on your back-end interface. This means, you serve all the HTML, but do not display it to the user until the data is loaded. You can show a spinner while it is loading. Here is some basic code:
JavaScript (jQuery):
$(document).ready(function() {
$.ajax({
url: 'administration.php',
method: 'GET'
success: function(response) {
$('input.username').val(response.username);
// ( ... )
$('.administration').fadeIn('fast');
},
error: function() {
window.location.href = 'index.php';
}
});
});
HTML:
<body>
<div class="administration">
<label>Username
<input type="text" class="username" />
</label>
</div>
</div>
CSS:
.administration {
display: none;
}
Execute php script from html
To use ajax, you must first include jquery in the head of your doc, then put the following code below at the end of the body tag (right before </body>
tag)
You can then edit the data variable to send your data as $_POST. To access the data this script sends to your php script, you just call the $_POST variables. Ex: to access var2, put in your php script $_POST['var2']. If you use GET instead of post, remember to use $_GET['var2'] in your php script to get that variable.
var data = 'var1=value&var2=value2&var3=value3';
$.ajax({
type: "POST", //can be POST or GET
url: "URL_TO_SCRIPT_GOES_HERE.php",
dataType: "html", //or json
data: data, //data to send as $_POST to script
success: function(response) {
//Once data received, do this
alert(response);
},
error: function(response) {
}
});
The database connection line should just be put in the php file. It has nothing to do with the jquery ajax script.
To execute the ajax call, you can just run it how I have here (it will load on page load) or you can put it in a javascript function and call it according to an event:
function my_ajax_call() {
var data = 'var1=value&var2=value2&var3=value3';
$.ajax({
type: "POST", //can be POST or GET
url: "URL_TO_SCRIPT_GOES_HERE.php",
dataType: "html", //or json
data: data, //data to send as $_POST to script
success: function(response) {
//Once data received, do this
alert(response);
},
error: function(response) {
}
});
}
A onclick button trigger:
<a onclick='my_ajax_call()'>CALL AJAX</a>
Command to run html with php script
Rename file.html
to file.php
and check.
I think you write PHP and HTML in a single document, and you use only html
extension.
How do I run PHP in HTML?
Rename index.html
to index.php
, and fill it with the following:
<html>
<head></head>
<?php
require('./file.php');
?>
</html>
How do I show / run a PHP file in a browser? As if it was a webpage
You need to download a server, and install it. If you want to go to the trouble, you can get XAMPP, and once it's installed, move the PHP file to the root of your installation (usually c:\xampp\htdocs\
on windows) and then use the url localhost/script.php
in your browser.
Related Topics
Submit an HTML Form With Empty Checkboxes
Laravel Certificate Verification Errors When Sending Tls Email
Regular Expression Pattern to Match Url With or Without Http://Www
Array_Splice() For Associative Arrays
Switch PHP Versions on Commandline Ubuntu 16.04
Safely Catch a 'Allowed Memory Size Exhausted' Error in PHP
What Do the "=&" and "&=" Operators in PHP Mean
Ternary Operator and String Concatenation Quirk
Workaround For Basic Syntax Not Being Parsed
Get the Files Inside a Directory
How to Change Envelope from Address Using PHP Mail
Laravel 5 Failed Opening Required Bootstrap/../Vendor/Autoload.PHP
Passing Multiple Variables to Another Page in Url
How to Make Dot Match Newline Characters Using Regular Expressions
How to Write to the Console in PHP
Calculate Age Based on Date of Birth