I know the title is vague, but the details for this particular question would make the title absurd.
I have an index.php file that has a virtual include within it:
<!--#include virtual="/includes/header.include"-->
This header.include file is HTML and I want to add a few lines of PHP inside of it.
My .htaccess file is set up to process .include files as php. The actual line from the .htaccess file is:
AddType application/x-httpd-php .include
The PHP that I'm trying to add is:
<?php echo "test <br />"; ?>
But when I load the website, the only thing that prints is:
"; ?>
If I view the source of the index.php I can see the entire PHP code that I have above.
Some other things I've already tried:
Copy the .htaccess file into the directory that the header.include file is in
Both the accepted answer and the answer with 6 upvotes from this question
Tried <br> instead of <br />
What am I doing wrong? Why won't the PHP get parsed correctly?
My PHP version is 5.1.6
Related
Recently I started a project to be able to share my codes, but I see the detail that I can not insert the code without the server executing it. I would like to know in what way I can put the code as it does here in stackoverflow.
Example:
<? echo'I need to put this type of code.'; ?>
Or where I can use the text editor they use here. I want to view my code similar to imagizer.imageshack.com/img924/8002/jHual3.png. The code syntax is all the code in my web page.
If you want to view the code of a PHP page, then you can read the file and apply syntax highlighting. There's a function for that:
echo highlight_file('/path/to/file.php');
To highlight the running file:
echo highlight_file(__FILE__);
If your webserver supports it you can just rename the file .phps and it will display highlighted when executed. Apache would need this in httpd.conf:
AddType application/x-httpd-php-source .phps
I have read on many sites and documents saying like php is embedded in html.
This is ok to understand but this statement is bit confusing for me.
If i have .html file and if i used php code for eg. following line:
<h1> <?php echo "This is PHP"; ?> </h1>
It wont work. But if i used same line of code in .php file it outputs the result.
So my confusion is if we are putting php code in .html file it is not giving results but still we are saying php is embedded in html.
Why cant we say html embedded in php and not php is embedded in html?
Now this line also outputs the same if it is used in .php file
<?php echo "<h1> This is PHP </h1>"; ?>
<h1> <?php echo "This is PHP "; ?> </h1>
Now here the file is .php so we can say we are putting html code in .php file, so if i say html is embedded in php is it right or wrong?
I would say this distinction is not right and not wrong, it's just useless...
You write a .php file, with some php code and some html inside.
Web server parses your file, interprets and executes php code, combines it with html, and produces an html page, which is sent to the requesting browser.
Thant's it... :-)
It depends on your server configurations if you want .html files to be treated as an php file(if it not already does) add this on your .htaccess file
<FilesMatch "\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
Now these is an example of PHP in HTML
<input type="text" name="<?php echo $name;?>" value="<?php echo $value;?>"/>
And this is the example of HTML inside PHP
<?php echo "<input type=\"text\" name=\"$name\" value=\"$value\"/>";
You can not say html is embedded in php only if it is inside .php file it depends on your code and not your file extension .
This question already has answers here:
PHP in HTML page, Super Quick Advice
(3 answers)
Closed 9 years ago.
I have asked a question leading up to this earlier. Thanks again for your help.
I have updated my .htaccess file in my root directory with the following:
AddType application/x-httpd-php .html
Where within my .html page should I place the php tag?,
I still cannot seem to get my .html page to load the .php script.
If you have followed this guide correctly, then according to the article, you should be able to execute php by simply opening a php tag.
From the article:
Things to watch out for:
If you have an existing .htaccess file, add this to it, do not overwrite it or other >settings may stop working! Always be very careful with your .htaccess file and ask your >host if you need help
Anything in your .html files that starts with your file for some other reason (an XML tag for example) you will need to echo these lines >to prevent errors. For example:
<?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
Where within my .html page should I place the php tag?
The answer is you can place your php content anywhere in your html page as long as it is inside <?php ?> tag.
even if you have done correct modification to .htaccess file and still your php content is not getting parsed than check whether you are connected to server or not.
Hi I am a newbie to web development. I am trying to display echo message from a php file in html page.
PHP File:
<?php
echo "Hello";
?>
Other file:
<form method="get" action="latest.php">
</form>
Note: These both are two different files
I am completely new to this. I don't know where I am going wrong.
Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP. However you can simply tell your web server to parse your HTML files as PHP too and then you can run your PHP code in an HTML file wherever you want. Now assuming your Web server is Apache.
Step 1:
Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website
AddType application/x-httpd-php .html
Step 2:
Go to Your HTML file and add an include statement where you want your PHP file to be included
<form method="get" action="latest.php">
<?php include ("yourPHPfile.php"); ?>
</form>
Step 3:
All done, now go check output in browser.
Just put the code together:
<form method="get" action="latest.php">
<?php echo "Hello"; ?>
</form>
Everything inside the Tags will be interpreted as PHP, no matter where it is. But make sure the ending of the file is .php!
May be try this..
<form method="get" action="latest.php">
<?php include('php_file.php'); ?>
</form>
Please note that other file should also be a .php file. if it is an html file you either need to change it to .php or you need to make ajax request.
You can't execute php codes in a file ending other than .php.
But you can have HTML code displaying in a .php file.
So I would recommend you change the extension to .php.
You can simply have
<?php
$name = "Harry Potter";
?>
<h1>Hello, <?php echo $name;?>!</h1>
Also you need a web server software and php processor. WAMP or XAMP may help you on that.
I am editing my answer since some are really picky.
In general you can't execute php in files which dont have the .php extension. But with some tweaks and line changes you can. I am just stating that you can't since, you mentioned that you are new to web development and I would like to make things simpler rather than confusing
If you want to execute php code in .html file extension than u need to do some htaccess changes.since file extension plays a major role ,it only tells the browser how to handle or parse a particular page.
You can create a .htaccess file at the root folder of your website and in the htaccess file add this line:
AddType application/x-httpd-php .html .htm
If you only plan on including the PHP on one page, it is better to setup this way:
<Files yourwebpage.html>
AddType application/x-httpd-php .html
</Files>
This code will only make the PHP executable on the yourpage.html file, and not on all of your html pages.
If you are ok with changing your file extension to .php than your work will be quite easy.you can simply include a php code between html code.like this
<form method="get" action="latest.php">
<labe><?php echo "hello" ?></label>
</form>
Its so simple you can just put php tag and write php code even inside the html
<form method="get" action="latest.php">
<div><?php echo "Hello";?></div>
</form>
Whenever you echo something, it is printed on html page only.
If you want to echo inside a particular html element, just put the code there. For example,
<html>
<body>
<div class="myDiv1"><?php echo "This is myDiv1"; ?><br></div>
<div class="myDiv2"><?php echo "This is myDiv2"; ?></div>
</body>
</html>
Hope this helps...
I have two files:
html
php
where html file is calling the php file to do something.
Now i would like to get the PHP session ID (php code) into both my html file as well as my php file.
I would like to know how would I do so so that both html file and php file have the same PHP session ID.
Thanks for your help in advance.
You can't have PHP sessions in HTML files. Best to just change the HTML file to a PHP file.
As mentioned in the comments - make sure you start the session at very top of the file before any spaces before the opening php tags.
To retrieve the session ID use php function session_id() but if the both files are on same domain you just need to call session_start at the very top and it'll just use same session across.
EDIT
To answer your qs in comments below -
Yes, a PHP file can just have HTML code but no PHP code at all or add PHP where required. e.g.
Myfile.php
<html>
<head> ... </head>
<body>
<h1>some title</h1>
......
......
Go to next page (<?php echo $_GET['next_page']; ?>)
......
</body>
</html
so you just open and close php tags where you needed php stuff to go.
To print session ID - just use somewhere in the HTML of PHP file.
Yes, session_id() will give you the same session ID in php2 file - again, make sure you call session_start function at the very top in the php2 file too.
If you have access to your apache configuration, or a simple .htaccess file, you can tell Apache to handle php code inside of an .html file. You can do this by creating an .htaccess file (remember the . (dot) as the first character in that filename) on the document root of the site (probably public_html/) and putting this into it:
# Add this to public_html/.htaccess file
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
You should be able to reload the html page and your PHP code will run great.