codeigniter letter at start of every page - php

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.

Related

Why is 'require' PHP function rearranging my HTML?

I have implemented a verification system for my website, where every time the user requests a page, a PHP script is run that checks if the user is currently logged in. If not, then the user is redirected to a login page. This is done by having a single "redirect.php" file which is 'required' by each page, using the syntax <?php require 'redirect.php' ?>.
Every page that has the above statement has all the code in the <head> element removed and put at the beginning of the <body>. Also, a HTML entity is inserted in quotes: " ".
The result is that in DevTools my HTML looks like this:
Whereas, in my editor, it looks like this:
<?php require 'redirect.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Stuff</h1>
</body>
</html>
I know that the code inside the 'redirect.php' file is irrelevant because even if I empty that file I get the same odd behaviour. I've checked for invisible unicode characters that somehow made it into my code; there aren't any.
Does anyone know what this is and how to fix it?
If it helps, I'm with InfinityFree hosting.
 is a Byte Order Mark.
The file you are requiring must be saved in a format that starts with one (typically UTF-8 With BOM).
It gets inserted into your output stream as a character. Since it isn't a character allowed before the start of the body element it implicitly starts the body element (as well as the html and head elements) and gets inserted at the start of it.
The DOM inspector then normalises it as an HTML entity because it isn't a visible character.
When the rest of the document is parsed, some of what you intended to be put in the head is backfilled through the HTML 5 error recovery rules.
Check your editor and make sure the file format you save the PHP files in doesn't include a BOM.
As it has been answered,  represents a Byte-Order-Mark (BOM). Quentin was so kind to clear this up early.
And as you have found out already, <?php require 'redirect.php' ?> is inserting it.
However with what you experience there is more. You've not shared your browser version, but a single BOM alone at the top of redirect.php would not result in the problem you were able to demonstrate (with a common browser).
Here's the differentiation:
It is true that there is an insertion of the BOM. However if it would be at the very beginning of the HTTP response body (the top of the document), it would not be shown in the browser nor via the devtools / in the DOM.
Next to the BOM there must be as well at least one additional character. And it must be positioned before a BOM.
That could be for example another BOM character (or some other whitespace or any other character).
This is important to note when you're looking forward to troubleshoot the issue, as it is most likely not redirect.php (alone), or it must not be at its very beginning, but somewhere there-in, e.g. another require/include.
This could result in a cleaning strategy that orders things differently:
Fix the other files first and then when the browser shows clean, fix the first file (so redirect.php second-last and then the file that has the <?php require 'redirect.php' ?> statement last).
(only an example, this depends on the concrete files in use which I don't know. point in case is it might appear clean by accident)
It might be fine in your usage scenario to keep the one BOM. Personally I would not suggest that, but with the browses and interoperability requirements you have, this can vary.
https://www.w3.org/International/questions/qa-utf8-bom.en.html
http://www.unicode.org/faq/utf_bom.html#bom5
https://www.w3.org/International/questions/qa-byte-order-mark#problems
When I open open this code sample in VSCode and use DevTools the format never changes. Try wrapping your required file in quotes and parenthesis like the example below.
<?php require("redirect.php")?>
DevTools shows the interpreted code. It's possible that your free host inject some ADs into your code. Look at your real source code with CTRL+U in your browser. This should show whats going on

PHP echo include variable in <head><title>

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

IE not loading properly only the first time

I have an application that works perfectly in Chrome,Firefox and Safari, but when I load it on IE happens a strange thing: all the page is loaded on the left side and after one or two refresh it loads properly.
Here are two descriptive images:
And the second time (after one or more refresh) IE loads it like this:
Does anyone know why is IE having this behaviour?
Thank you.
I found the problem, it was quite simple.
I was calling my 'functions.php' as the very first code line (even before <!DOCTYPE html...). This file includes some javascripts function that CAN'T be loaded before <!DOCTYPE html (better between <head></head> tags).
Once I placed them between <head> tags the page is loading properly.
Thank you for the hints.

Symfony 2 Debug Toolbar not showing, breaks HTML in dev environment

I really need your help with this.
I don't know what I've done to break the Symfony 2 toolbar. The debug tool bars stop showing and when I look at the source code of the page, I can see something like this:
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>hello</h1>
<div id="sfwdt
and then the page terminates. This page I set up for debugging use the simplest HTML layout without using any variable and is not behind a firewall. I simply want to get the debugger toolbar showing. It looks like the debugger did try to load but for some reason it teminate at:
<div id="sfwdt
and breaks the entire page. If I turn the debugger off, the ending tag of the body and html come back.
I've searched for the occurrence of 'sfwdt' and it looks like it is from one of the twig templates of the profiler, however, when I modify that template to try things out, nothing change. Any one can point me to the right direction what's going wrong ?
And there's no error message found. I've comment out all services and keep it to the minimum, still no luck.
AppBundle/Resources/views/layout2.html.twig
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>hello</h1>
</body>
</html>
AppBundle/Resources/views/Default/index.html.twig
{% extends "AppBundle::layout2.html.twig" %}
Sorry, I finally found the issue.
One of the custom bundles registers the kernel.response event and try adding a content-length header to the response. The content-length does not take into account the extra code for the debugger bar and it trims out the extra content added for the debugger toolbar !! and it explains why the page always terminates at <div id="sfwdt, because it takes the same number characters as body and html end tags

PHP MAMP not showing all of HTML document

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.

Categories