I am using MAMP. None of my code after the PHP tags displays in a browser.
I suspect it's something related to what content I can send to the browser after sending PHP? Or what content can be sent before the HTML doc?
Code:
<?php
/* Web Controller for searching music */
session_start();
echo "I display fine!";
?>
<html>
<head>
<title>Listen2me</title>
</head>
<body>
<h1>Listen2.me</h1>
<input type="text" name="songChoice" value="Search songs">
</body>
</html>
Maybe the missing doctype declaration could affect the display:
<!DOCTYPE html>
Did you try uploading the file to a different server to see if it's a problem with your localhost or a problem with your file?
When I run into issues where the code looks fine but it's not working fine, I always run it through BBEdit's "zap gremlins" feature. You might have an invisible bad character in there somewhere that's messing stuff up. If you don't have BBEdit, you can recreate the document from scratch. Don't copy-paste, because you'd just be copy-pasting any bad characters right back into the new document.
Related
I am building a small personal website. To keep things small and to avoid writing things over and over again, I have a single header.php file that every sub-directory index.php file include's. So, every index.php file has these lines:
$title = someTitleVariableOrMethod;
include('/var/testsite/docroot/header.php');
And in my header.php file, I have these lines (I know I could probably improve the formatting, but first I want to get the title working).
<html>
<head>
<?php
if (isset($title)) {
echo '<title>' . $title . '</title>';
} else {
echo '<title>Sampletext</title>';
}
?>
<style>
//a bunch of irrelevant css
</style>
</head>
<body>
//this is the end of the header file, the rest is dealt with in the index.php file
But for some reason, the contents of the title and all my CSS show up at the start of <body> (I see this when I press F12 in browser) and NOTHING at all shows up in <head>. I just want the title contents to be put in the title tag. How could I fix this issue?
Thanks in advance.
Make sure that you are accessing the pages from a web server (e.g., XAMPP - then access via http://localhost/site/index.php). If you try to open them directly from your file system the PHP will simply be shown as you described. Also make sure the index.php has the closing body and html tags.
Sorry for the trouble, I found the issue! I was using passthru to get to content for the variable, which I didn't realize prints to the screen. My mistake!
EDIT: The site says I can't accept this answer for two days, but this is accepted
The following Php file renders perfectly fine with Mozilla Firefox. However, when running the same on Google Chrome; result - It displays the entire HTML code instead of rendering it. Basically indicates that Google Chrome browser is unable to understand and display the HTML code.
abc.php File
<?php
session_start();//session is a way to store information (in variables) to be used across multiple pages.
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.3.js"></script>
.... //some lines of code
</html>
Problem was
While Firefox browser parses and renders the HTML code.
Google Chrome isn't able to understand that the php file is a HTML code in itself.
Solution
Placement of the DOCTYPE line as the FIRST LINE of the FILE
Hence the change is in the positioning of the DOCTYPE line indicating the type of document to be displayed on the browser.
abc.php File
<!DOCTYPE html>
<?php
session_start();//session is a way to store information (in variables) to be used across multiple pages.
?>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.3.js"></script>
.... //some lines of code
</html>
I'm working on a website, which I have temporarily hosted here.
You'll notice the mystery letter 'a' I'm getting at the start of every page. I've gone through all the php files (controllers, views, models) and cannot locate where this letter is coming from. Another curiosity is that all the head content is not residing in the head tags when inspected with Firebug. It appears in the body tags, however it still functions correctly. Are these two issues related?
The only thing I have found from searching the internet is that perhaps some files have been saved as ANSI instead of UTF-8. I've tried 'saving as' all my php files as UTF-8 using my editor, but it is a very slow process. Any help debugging this situation would be appreciated.
EDIT- thanks for your response, #erman-belegu. It doesn't seem to be in any controller. For instance, I've set up a 404 redirect, with its own controller and view. The view looks like this:
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="No Page">
</head>
<body>
<h1>No page dude.</h1>
</body>
</html>
But when inspected with firebug, it looks like this:
<html>
<head></head>
<body>
a
<meta content="No Page" name="description">
<h1>No page dude.</h1>
</body>
</html>
I have encoded everything using UTFCast, and am still experiencing the same issue. Any help welcomed.
You see the head inside the body tag because the mysterious "a" is the first character of your output. It's put inside the body tag by the rendering engine of your browser, or by firebug.
If you find the cause of your "a" - almost certainly some content outside PHP tags - the head will return to normal in firebug.
Searching for the "a" is tricky.. I'm not sure how large your codebase is, but I'd say start by exiting the process right before output is sent. You should see only the "a". Then move the exit step by step untill your "a" disappears, and you'll find it.
Check your controllers at start or maybe any print somewhere. Check all your code on these pages because you print these "a ".
Also use UTF-8 without BOM. But, I think that you print it accidentally and dont think that this happens for any other reasons.
I am new to php and wonder what am I missing about the rules for interleaving html and php code.
This is now the second time I run into a situation where a php file only works if my php tag is at the beginning of the file. This is not the case for all my files. I wonder why that might be.
Here is an example:
My file structured as follows works just fine:
<?php
... my php code
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Authentication</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="container">
</div>
</body>
</html>
But when I move that html block at the top (which is what I want ultimately since I use some echo statements in my php code), and leave only the following at the bottom:
</div>
</body>
</html>
then some portion of the php code do not work. For example, my setcookie function no longer sets a cookie (though it does not error out) while I can still run sql queries or echo statements just fine. I ran into a similar issue with a complete different code taken straight out of a tutorial site: the example would only work if the code started with
Some operations (like writing a cookie) must be performed by PHP before any output is sent to the browser, because those operations involve setting response headers (which are always sent before any other content). That seems to be the problem here.
Per the documentation:
setcookie() defines a cookie to be sent along with the rest of the
HTTP headers. Like other headers, cookies must be sent before any
output from your script (this is a protocol restriction). This
requires that you place calls to this function prior to any output,
including <html> and <head> tags as well as any whitespace.
Give those links a look, they will explain something, and never forget to ask here on stackOverflow
http://www.w3schools.com/php/php_cookies.asp
http://php.net/manual/en/features.cookies.php
http://www.w3schools.com/php/php_sessions.asp
http://it.php.net/manual/en/session.examples.basic.php
Answering your question, you can place the tag practically anywhere in the html code, as long as it's structured correctly.
However, there are some functions in php requires that no output is made before it is executed, like setcookie, setsession, header .. etc.
The output here is either;
a. an echo statement in php code.
b. an html code before the tag that the function is in.
and as for that some code in php doesn't work.
Usually, if you have any problem in your code, that php code doesn't work. So, it's better to check if it syntax correct or not before that.
I'm having problems embedding php inside an html file.
I first ran in to this problem when I was trying to 'include' a php file inside tags, and thought it was related to css formatting, or something. But now I've broken this down into the simplest php and html possible, with an example from a book that should work, and I'm still getting this problem. Here's the html sample that doesn't work:
<HEAD>
<TITLE>PHP inside HTML tester</TITLE>
</HEAD>
<BODY>
<?php
echo "Hello World";
?>
</BODY>
</HTML>
I'm expecting 'Hello World' to show up in my browsers, but nothing is displayed. When I try to 'view source', I see exactly the text above. I figure that after all the examples of this I've tried, the code is ok, but something is keeping what's inside the from being recognized.
Any suggestions? And thanks for helping me out with what's probably a dumb question.
There is something wrong with your PHP installation. The web server isn't passing requests for PHP pages off to the PHP interpreter.
If you did indeed save the file as an .html file, then your PHP code will never execute because most web servers have their handler mappings set to route only PHP (.php, .phtml, or .inc extensions) files to the PHP interpreter.
Looks like your server is not able to handle php or your server does not know how to handle the file type with - this code is in.