how to implement php in html file - php

so I'm basically brand new to web based stuff. My university has given me a cPanel hosted server. I need to implement some basic php functions for my main web page.
This is what my cpanel looks like if it helps:
So in my index.html file I have:
<!DOCTYPE html>
<html>
<head>
<title>Current Date/Time</title>
</head>
<body>
<h3>
<?php
date_default_timezone_set('UTC');
echo date("l");
echo "Hello World!";
?>
</h3>
</body>
</html>
But none of the php echo stuff is displaying. I'm not sure if my server actually knows what to do with an html file with php in it.
My instructors have told me: the php file needs to be deployed to public_html folder using the file manager in CPanel.
But i'm not really sure what needs to go into the .php file and how to get my .html file to utilize it.
Thanks for your patience.

Your file name needs to be .php. If the web server is set up with PHP (which I'm going to assume it is) then it will handle the php file fine.
Rename the index.html for now and by default the index.php will be the first page to load.

The file you are using is index.html. This means that it can only display static text and that's the reason why your PHP code is not working.
All you have to do is change the extension from .html to .php then your PHP code should work.
Although I would suggest you Google a few Intro. to HTML and PHP tutorials to get you going on the basics

The file's extension needs to be changed from *.html to *.php or the PHP won't execute, it'll just be treated like raw HTML.
Here's a quick very basic step-by-step of what's happening:
The client requests index.php
Your web server recognizes the *.php extension and tells PHP to interpret it
PHP parses the text looking for PHP code, and evaluates that code, then replaces the PHP code with the output of that PHP code (if any)
For example:
$strVar = 'world';
echo '<span class="contentText">hello ' . $strVar . '</span><br />';
is replaced with that code's output:
<span class="contentText">hello world</span><br />
The resulting HTML is then returned to the client as content to be rendered by the client's web browser
The client's web browser parses the HTML and renders a web page

Related

PHP being commented out when uploaded to server (Ipage)

I am new to PHP and am trying to run my first script. I read that to to run php on your pc, you have run a server on it which I didn't feel like doing.
So instead I am trying to run it on a server hosted by Ipage.com
The program is just a basic html file with a set of full tags
I have tried linking windows explorer using ftp, using the "upload" button on the website and directly editing the file using the editer on the site. When ever I add the php tags and save or upload the file, when I go to view it on the ipage file manager, they are commented out
This is what happens to the file when I upload it
<!DOCTYPE html>
<html>
<title>Striations</title>
<head>
<link href="css/LandingPageStyle.css" type="text/css" rel="stylesheet">
</head>
<body>
<!--?php echo "HELLO"; ?--> //originally <?php echo "HELLO"; ?>
<h2>Our site is currently under construction, please check back later!</h2>
<img src="media/construction man.png"></img>
</body>
</html>
I'm totally new to php and hosting and stuff and I could be missing some important step regarding how to run php
I'd appreciate and explanation of how php is transferred into html before being sent to the browser
EDIT:: I know as a fact that ipage supports php, they had mentioned something about changing my root file directory, but even when I moved my files to where they said, it did the same thing
Is there something else I should be doing to enable php?
It is an html file with php tags... I'm supposed to save it as .html, right?
If you saved this as index.html then the PHP parser won't run it by default. Either name it index.php or (if you can) use htaccess to parse HTML files as PHP

PHP code doesn't show while using Dreamweaver

I create a new HTML file for my project using Dreamweaver and i added a simple php code:
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
so far nothing is appearing while i open the file with google chrome and IE, any thoughts?
PHP has to be executed on the server. Upload it to a web server that supports PHP, or install your own web server locally such as WAMP. You then need to access the file with a URL rather than just opening it. A local URL will look like http://localhost/ or http://127.0.0.1/.
Your file also needs to have the extension .php if it contains PHP code. If you really want to use PHP inside a .html file, your web server will need to be set up specially to handle this.
PHP requires a webserver and an interpreter. Browsers cannot handle PHP on its own.
Look at XAMPP
A good idea to check if your php file is working properly is to load phpinfo function.
This function will show details about your PHP installation:
<?php
phpinfo();
?>
If what you see on the screen is this php code with the entire opening and closing tags (), then you are not running the page with PHP.

Why is echo printing past the quotations?

<!DOCTYPE HTML>
<html>
<body>
<?php
//printing
echo "<p>Hello, World</p>";
?>
</body>
</html>
This is suppose to be a basic hello world example but when I run it in the browser I get an extra "; ?>
Hello, World
"; ?>
Why is that?
EDIT: Yes the file extension is php
EDIT2: I created the file with Notepadd++. I ran the file by right clicking the file and open with chrome.
EDIT3: Okay so what I gathered is that I can't just run the file locally. I will try through a web server. Thank you for the responses so far!
I believe your problem is that you do not have a local server such as xampp.
I would suggest downloading xampp and then follow the instructions for running a php file.
Make sure you have .php as the file extension
You are probably not running your file through your web server, but you are opening it locally.
Make sure your url is something like http://localhost/myFile.php. And not c:\documents and settings\myName\Desktop\myFile.php.
"I created the file with Notepadd++. I ran the file by right clicking
the file and open with chrome."
You are saying your problem. PHP files are not like HTML files. They must be compiled before served.
For example:
<html><body><?php echo date("Y"); ?><body><html>
will be expected to show the system year. If you try to open it browser does not recognize PHP scripts and treats that function as a text and you see
echo date("Y"); ?>
Why don't you see <?php ? Because after <character, the browser thinks that there is an HTML tag coming.
If you run that PHP file correctly and then look at the source HTML you will see
<html><body>2014<body><html>
You must move the file to web server directory like c:\xampp\www or to c:\wamp\www.
Then run apache server.
After that open a browser window and type localhost/yourfile.php:80
80 could vary in according to your settings (Take a look at httpd.conf file).

"Hello, World" in PHP

It's a "Hello, World" code snippet. I have tried to run it using XAMPP, and I am using Dreamweaver to write the code. Upon execution, the page does not display "Hello, World!". What would have went wrong?
<!DOCTYPE html>
<head>
<title>Hello World | Hello and Welcome</title>
</head>
<body>
<?php
Echo "Hello, World!";
?>
</body>
</html>
You are trying to execute PHP code on an HTML page. All PHP files should have the .php extension (implied from the OP's comments). Change your filename.
PHP handlers don't work in .html pages for efficiency reasons. When you use .html instead of .php, you are telling the server not to embed PHP in that page, to save server resources.
Echo would be better as echo.
Is the file extension .php, not .html?
Is PHP configured on the web server?
Please check all those out.
Either you're not saving the file in a php extension so the PHP engine doesn't run on it or your server isn't set up properly
It's working fine on IDEOne
The answer to change the extension to .php is correct, because by default it would be a waste of server resources to actually read every file to see if it contains PHP code in order to decide whether it should execute PHP processing on it.
That said if you really wanted to you can probably configure the server to serve up any file extension as PHP, even a made up one. There's nothing magical about the extension other than how it's configured to be handled.
for hello world program in php just follow this tutorial
but before that you should have wamp server or xamp server on your pc(wamp/xmap are open source software's and easily can found on google)
your 1st php program code:-
<?php
echo "hello world";
?>
output:-
hello world
Tutorial :https://www.youtube.com/watch?v=7qtqzhdEX-c
after this tutorial you can able to move other intermediate level programs of php.
PHP is generally case sensitive (see: http://php.net/manual/en/language.variables.basics.php).
So try changing Echo to echo.

include php script inside HTML

I am a newbie and have never used PHP before. I want to execute PHP script from an HTML file on Linux. What do I need to do?
This is the content of my HTML file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Testing Weather Data</TITLE>
</HEAD>
<BODY>
<div id="weather">
<p>Weather Information
<?php include "/home/tahoang/Desktop/weatherData.php"; ?>
</div>
</BODY>
</HTML>
What output do you receive? Does it just show the PHP code?
I assume that's the case.
For a test, change the extension on the file to .php and run it again. Does it work now?
If so, you will need to associate PHP with .html and .htm files on your server.
EDIT
This is the line you want to add to your Apache config:
AddType application/x-httpd-php .html
You can embed the php code right into HTML using <?php ?> tag and putting your php code between opening and closing tags.
Before that you need to add this handler in /etc/apache2/apache2.conf file to run php scripts inside .html file:
AddType application/x-httpd-php .html
You either run pass the file path as an argument to the php command line program, or you configure a web server to execute the page as PHP (the specifics of which depend on the web server you use) then visit the URL of the page.
Here is the step by step process to include php code in html file ( Tested )
If PHP is working there is only one step left to use PHP scripts in files with *.html or *.htm extensions as well. The magic word is ".htaccess". Please see the Wikipedia definition of .htaccess to learn more about it. According to Wikipedia it is "a directory-level configuration file that allows for decentralized management of web server configuration."
You can probably use such a .htaccess configuration file for your purpose. In our case you want the webserver to parse HTML files like PHP files.
First, create a blank text file and name it ".htaccess". You might ask yourself why the file name starts with a dot. On Unix-like systems this means it is a dot-file is a hidden file.
(Note: If your operating system does not allow file names starting with a dot just name the file "xyz.htaccess" temporarily. As soon as you have uploaded it to your webserver in a later step you can rename the file online to ".htaccess")
Next, open the file with a simple text editor like the "Editor" in MS Windows. Paste the following line into the file:
AddType application/x-httpd-php .html .htm
If this does not work, please remove the line above from your file and paste this alternative line into it, for PHP5:
AddType application/x-httpd-php5 .html .htm
Now upload the .htaccess file to the root directory of your webserver. Make sure that the name of the file is ".htaccess". Your webserver should now parse *.htm and *.html files like PHP files.
You can try if it works by creating a HTML-File like the following. Name it "php-in-html-test.htm", paste the following code into it and upload it to the root directory of your webserver:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Use PHP in HTML files</TITLE>
</HEAD>
<BODY>
<h1>
<?php echo "It works!"; ?>
</h1>
</BODY>
</HTML>
Try to open the file in your browser by typing in: http://www.your-domain.com/php-in-html-test.htm (once again, please replace your-domain.com by your own domain...)
If your browser shows the phrase "It works!" everything works fine and you can use PHP in .*html and *.htm files from now on. However, if not, please try to use the alternative line in the .htaccess file as we showed above. If is still does not work please contact your hosting provider.
I am surprised no-one has mentioned a way to hack it - if you don't want to hassle of changing the Apache config here is how you do it:
In your .html file you simply make use out of iframes
<iframe name="myPHPScript" src="myScript.php" width="100%" frameborder="0"></iframe>
This will simply just display what ever is displayed as if you went directly to www.mysite.com/myScript.php
So for example we could have
<?php
echo 'Hello World!';
?>

Categories