PHP placement within HTML page? [duplicate] - php

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.

Related

Pass variable from PHP into HTML [duplicate]

This question already has answers here:
How do I add PHP code/file to HTML(.html) files?
(12 answers)
Closed 7 years ago.
I've got a simple .php file with Rock, Paper, Scissors game (some schoolwork) that tracks your score and a .html file with buttons to choose from R/P/S.
Score is tracked via session variable, is there a simple way to pass the value of this variable and display it on the html page?
You should generate your HTML page from PHP file too. That's how php works. That way you can print out PHP variables. HTML is no programming language - it's just markup language...like ...hmmm..XML.
You can add addhandler in .htaccess for html files to treat it as php file, see example:
AddHandler application/x-httpd-php .html .htm
but instead you can, avoid above and change the file extension from .html to .php you can pass it as query string GET, see example below:
http://example.com/myphppage.php?key=value
or you can access session by adding session_start() at starting of page
If you save your .html file as a .php file, you can do this quite easily by adding
<?php echo $_SESSION['score']; ?>
into your HTML. For example:
<p><?php echo $_SESSION['score']; ?></p>

Header displaced after PHP implementation (UTF-8 BOM)

My company had a decent website before I joined them, outsourced and untouched for years since. After joining I've been slowly improving it in my free-time.
This week I rewrote the header/left-column/footer which are the same on all pages so that I could include them using PHP and avoid fixing them one-by-one when I want to change something (among other benefits).
I'm not well-versed in messing with the .htaccess nor PHP but really all that I have changed is:
Allowing PHP to work with .html files by adding to .htaccess:
AddType application/x-httpd-php5 .html .htm
I changed:
<?xml version="1.0" encoding="UTF-8"?>
To:
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";?>
To avoid the <?xml tag from throwing PHP short tag errors
copy and pasted the three common elements, header, left-column, and footer into separate HTML files and and included them using:
<?php include '_header.html';?>
changed the upper left text a little, but that shouldn't affect anything.
Looking at the source code after loading both pages (PHP include and plain html) they look identical.
I haven't changed any of the CSS and so they are using the same code.
Yet for some reason switching to the PHP include version adds an empty line at the top of the page.
Here is the current (plain XHTML) version:
http://www.akidc.co.jp/index.html
The new file for reference is:
((removed outdated link))
(I can't change the .htaccess until I upload all the new pages at once or the <?xml tag will error them all so it's up'd as a .php)
The header file I'm including is at _header.html
Please help me solve the gap ^_^
Run a diff on the source, you'll immediately notice what's going here.
In the PHP version, you have a U+FEFF just before the <div id="header" ...>. This is interpreted as content by the browser, so it attempts to display this content.
In order to solve the issue, make sure all your source code is stored in UTF-8 format.
By the way, here's the diff:
See that <feff>?
And now, look at the elements of your page. This is what you had before:
and this is what you get in PHP version:
Notice the additional " " block I highlighted?

PHP INCLUDE function inside an HTML file

I have an HTML file that has a rather long navigation menu inside of it. I want to take that menu out of the HTML and place it into an external PHP page and then call it with
<?php include 'navigation.php'; ?> in the HTML file.
I have tried just adding this into the HTML file but it doesn't display anything as well as no errors on the page.
What do I need to do (if it's even possible) to keep the files HTML and use the php require function?
Add this in in your httpd.conf and then you can process PHP code on HTML pages
AddHandler application/x-httpd-php .php .html
Q: Did you give the page a .php suffix? That should be all you need to do.
Remember the way PHP works - you basically "embed" your PHP code in an HTML page, and the server executes the PHP before it serves (the rest of) the HTML.
But in order for PHP to "see" your code, you need to make sure your "HTML page" has a .php suffix.
As a crude workaround, you can add ".html" to the list of file suffixes that PHP will parse.
But this could cause other things to break.
If you want to embed PHP code in your "index.html", the best, cleanest approach is to simply rename it "index.php".
IMHO...

Include in included file doesn't work PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
I have the following html list:
<ul id="ulGroups">
<?php
include('organizer_picturearchive_groups.php');
?>
</ul>
I am trying to include this file, which also includes another file:
<?php
include('lib/organizer_functions/picture_archive.php');
echo select_all_picture_category_groups();
?>
Basically, what I am trying to do is to call a function that is in the picture_archive.php file. I do that in the organizer_picturearchive_groups.php file.
I am including this file in the first file where the html list is. I am not pasting the whole code from there, because it is a lot, but it works.
However the include thing doesn't, because when I tried to make a test echo statement in the "organizer_picturearchive_groups.php" file it works and I can see the text I am printing.
Do you have ideas what may cause this problem ?
Double check the path. If organizer_picturearchive_groups.php is in the same directory as the code, then try configuring the path as relative:
<?php
include('./organizer_picturearchive_groups.php');
?>

Get same PHP session ID for both html and php files

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.

Categories