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?
Related
I don't know why. I'm using php7.0.1,apache2.4.18,mysql5.6.28. My php code is like
<?php
echo "hello world";
?>
and I find it turns out to be this in the browser
<!--?php
echo "hello world";
?-->
<html><head></head><body></body></html>
EDIT
I've uploaded the related files to https://github.com/franklee0817/publicFiles
Your server is clearly not parsing PHP code so PHP tags reach the browser. If you see the actual source code (each browser has a different menu item but keyboard short-curt is often Ctrl+U) you'll see your raw PHP source code. However, if you use the DOM tree provided by your browser developer tools you'll the result of parsing and fixing the HTML tag soup. Applying workarounds to invalid tags is left to browser discretion; yours have decided to entirely omit <?php ... > and wrap it into a comment tag.
I find out what's wrong with my server. The php code is not actually running with php. The apache give the php code straight to browser. everything works properly after I add the below line in httpd.conf.
AddHandler application/x-httpd-php .php
The handler for php was missing. It should be as a default line in httpd.conf, I don't know why it was missing. But after this. Everything back on line. Thank you guys.
I'm learning php, been trying to make a cms, but when I use an include for the header this results in the page appearing as if it were written in pre tags. There's no errors when I debug or anything. And I'm completely stumped. When I put the header back in without the include it renders just fine.
<?php include("..\includes\layouts\header.php"); ?>
That's the include I'm using.
I've tried using the full path name, tried it in different browsers and using :
include($_SERVER["DOCUMENT_ROOT"]
Check that you are closing your php tags, ending your strings with semi colons etc.. My guess is that your html is being sucked into the php code which freaks out and just dumps it.
Maybe try one of the validators such as http://phpcodechecker.com (I have not used this one so I cannot comment on it's effectiveness)
Edit: I am rereading your post and I think I understand what you are trying to say - your header contains the path to your css and when you put it in a separate php file the css doesn't work? So the first thing to do is determine if the issue with the php path or the css path inside the header.php file. Look at your source code to see if the header code is being included - if it is then play around with the css path - though including the header in a php file will not cause that css path to change.
I am guessing that your header is not included at all from your mention of paths. include() works from the loading file's location. The path it wants is a server path and not a url. The one that you have above: ../includes... means that you have an include folder at the same level as the loading file such as (assume index.php is the main file):
/includes/layouts/header.php
/somedirectory/index.php
The ../ means - drop down one directory then go up from there.
If your path is more like:
/includes/layouts/header.php
/index.php
Then the include would be:
include('./includes/layouts/header.php');
Let me know if that works - if it doesn't try to explain your directory structure.
I'm creating a LAMP stack and just started learning PHP. I'm having trouble with my Hello World program. On my server I have a file called index.html. This is the contents of that file (which is basically copy pasted from an online guide):
<html>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
echo '<p>Hello World</p>';
?>
<b>Here is some more HTML</b>
</body>
</html>
When opened on a web browser, I expect my output to look something like this:
My Example
Hello World
Here is some more HTML
Instead, it looks like this:
My Example
Hello World
'; ?> Here is some more HTML
Why is that extra "'; ?>" there? I'm probably making a simple mistake. I've tried accessing index.html on chrome, firefox, and safari, same result each time.
PHP does not work in HTML files, try renaming it to index.php
Apart from that the code is fine (maybe some best practices like separating HTML and PHP, but you'll get to that later)
The reason you don't see the whole PHP code on your screen is because the browser is trying to parse the element <?php *** ?> as if it where valid HTML (like <span>). If you check the html-source, you will see all of your code.
As someone in the comments below mentioned, there are ways to make PHP work in HTML. You should not do this, unless you are very aware of what you are doing. It is not a default setting and it should stay that way. If you should be able to use PHP in HTML files, .phpfiles would not have to exist.
When you expand your knowledge, you will start separating HTML files from PHP files and make PHP include templates. Two separate files for two separate goals
Rename the file to index.php.
In addition to this, the PHP code must be interpreted by a WEB SERVER(Apache in your case).
For example(if you are using XAMPP on Windows), and your php file is present in the webcontent directory of your server's document root(htdocs), then type the following in your browser
http://localhost/htdocs/webcontent/index.php.
Like others said, you must rename index.html to index.php, but if you must use index.html, put the line below into your .htaccess file, so you can process .html files as .php.
AddType application/x-httpd-php .html
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...
I downloaded a Template + CSS File for a Website that I'm Building, the template worked well until I tried to break it down and put every code in its own file (for easy modification and editing in the future).
So, when I cut the head part which included (Title + Meta Data .. etc ), and put it in its own file, and replaced it (for sure) with an include() function, I lost the CSS styles and returned to the basic & standard style (Black & white with no extra format .. etc)
Where did I Go wrong? Knowing that here is the include function that I've used:
<?php
include 'files/head.php';
?>
With an URL like file:///C:/xampp/htdocs/test6/index.php PHP is NOT executed. You must run it with apache being involved. Currently you are opening your PHP script as a regular txt or html file - it is just passed to browser without processing.
In order to make include function work you must run it with apache. As you are using xamp, I think you should simply open it with URL like http://localhost/test6/index.php In this case, apache will get that request and pass it to PHP. PHP engine will interpret your PHP script and "replace" include files/head.php with a content of head.php.
If everything is Ok, after pressing Ctrl+U (or looking at HTML with Developer Tools or Firebug) you should see a content of head.php instead of <?php include ....
Please note that css files should be linked with relative URL like css/screen.css. Or absolute URL like http://localhost/test6/css/screen.css. like Search for relative and absolute URLs in google for more info.