Php include annoying top margin - php

I don't know why every time I try to include my header using PHP’s include there's a top margin. I checked it using Firebug and it says there's a 22px offset margin on the top. Is anybody experiencing this problem? I think it's a CSS propiety: top: 22px. But nothing can change it even if I write h1 style="top: 0px; margin-top: 0px;". I think it's a php-CSS mystery that will never be solved.
edit: The only way to get rid of that top margin offset or whatever it is, is to add the follow properties to the H1: top: 0px;
position: absolute;
Will those properties generate more problems in the future?
is there a better way to solve this top margin-offset problem?
edit2: I think there's a problem with the encoding. Is there a conflict between the encoding of the included file (header.html) and the index file?
My index goes like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
<div id="page-wrap">
<?php include_once("header2.html"); ?>
</div>
</body>
</html>
With this CSS:
* {
padding: 0px;
margin: 0px;
}
My header.html (the one that’s being included):
<h1>Header 2</h1>
And that’s the output:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
<div id="page-wrap">
<h1>Header 2</h1> </div>
</body>
</html>
God its so simple that I really dont know where the top margin is coming from (in all browsers).
It only happens when I use php includes.
The source code looks the same as when I dont use php include.

This is pretty weird, but I copied & pasted your output HTML into Notepad++, and there was a strange character just prior to the h1. When pasting it into Notepad, the h in the h1 was subscripted.
Therefore, it looks to me like you may have an erroneous (or unexpected for PHP) character in your included HTML (or in the PHP including it). My suspicion? The dreaded UTF-8 BOM. Try to change the encoding of your included HTML file to eliminate the BOM (which I've always had problems with when dealing with PHP).
Edit: Yep. Just verified with a hex editor. There's a byte-order mark at the beginning of your included HTML. I don't know which editor you use, but you should have the option to change your text encoding settings.
See this for more info about PHP & the BOM.

You ought to post your generated HTML, like by copying what you see when you select "View Source" from your web browser. Oftentimes, I've seen mysterious blank lines in PHP-generated HTML because PHP is configured to output errors and warnings. Even if this is not caused by that, we can more-easily diagnose the problem if we see the outputted HTML.

I experienced the same problem...so what I did to fix it was to add an html opening comment at the beggining of the first file and an html closing comment at the beginning of the second file. This encloses the BOM (or whatever is appearing in between the files) inside a comment so it's not outputted in html.
For example:
#header.php
<?php ?>
<html>
<!--
#body.php
<?php ?>
-->
<body>
...
I tried to fix the problem converting between encoding formats in notepad++ to no avail. So this is only a temporary fix til I find a better solution to the problem.

well, without knowing what's in your php include, it's hard to tell, but make sure there's no CSS inside of it. also make sure that CSS you're loading is getting found and loaded. i debug by changing the background color or something else visual so u can be sure it's loading.

i'd bet anything that it has nothing to do with the php include it's self. like previously mentioned, there may be some css in the included file that is doing it, but i've never seen php anything like what you are describing.
i would look at the doctype declaration [temporarily remove it to see what happens], and remove the current html declaration tag and replace it with a plain <html> tag [again just temporarily, to debug]

A lot of times an extra new line will be at the end of a PHP script file you are including after the ?> tag. This will cause an extra character to be included in the generated output. You can remedy this problem by not having a closing ?> tag at the very end of your file. PHP knows to "assume" that it's closed.

Do you have the option to give us a link? I just tried locally a couple of things and you may get this "unwanted" new line for apparently "no reason" at all between your content and the "doctype" declaration, if there is a nonprintable character. Try to delete all characters after the ">" and take a look at it with a hexeditor to make certain of it. Please provide us more info.

Thats very interesting, because I just used your markup and your css. I also put it into an php file and included a html file. But there is no margin. Nowhere.
So make sure your browser is able to find the css file with
* { margin: 0; padding: 0 }
And I suggest you to use a reset sheet like this one - http://meyerweb.com/eric/tools/css/reset/index.html.
p.s. if you own some webspace, put it online and send us a link.

Dreamweaver drives me nuts with it's automatic BOM additions. Here's the fix:
http://www.adobe.com/support/documentation/en/dreamweaver/mx2004/dwusing_errata/dwusing_errata2.html

I am using notepad and I selected from the menu -> Encoding->Encoding with UTF-8 without BOM and it worked just fine. This should be done for every included file. I think this is the solution to your problem. Have a nice evening.

I almost killed my keyobard trying to solve to problem.
Then I tried PSPad editor, change explicitly .inc or .php to UTF-8 and IT WORKED !!

I had one time this problem and here is the way to fix it - its really problem in charset.I have spent 10 hours to find it. In my situation I change at all php files(all php files, all tpl(if you have) files) charset from UTF-8 to UTF-8 without bom and save it and try then. After that you can also make at .htaccess string "AddDefaultCharset UTF-8".

Related

PHP seems to output HTML code incorrectly and with hidden characters

I have a php page. The code is:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width">
<?php include("some_file.php"); ?>
<title>Calculadora dos Custos do Automóvel</title>
</head>
<body>
<p>aaa</p>
</body>
</html>
As you can see nothing is wrong.
Though, view source of Firefox gives red erros and w3 validator gives also errors.
I'm around this for two days, and it is so strange. I suppose it has to do with some strange characters php might be inputting into the HTML code but I'm not sure. Or perhaps the encoding considering that the code has Portuguese characters.
There are two BOMs just before the <title> tag. Remove them (e.g. delete everything between the second <meta> and the <title>) and everything should work fine.
From what I see here you are using Notepad++ and we already cleared that the problem is because of BOM of the php files that you include before title.
To solve this for each file that you use open in Notepad++ and from menu choose Encoding -> Encode in UTF-8 without BOM and save the file. Do this for all your files in the probject (php files).
You should not have anymore that special characters in front of title.
Also make sure you start the <?php tag at the beginning of the file (no white spaces or new lines before) and if the file contains only php you don't have to use closing ?> tag. This way you are sure that you will not have spaces at the end of the file.
As others have said BOMs are a problem when including PHP files, even if those files do not echo anything
PHP includes
At the time this article was written, if you include some external
file in a page using PHP and that file starts with a BOM, it may
create blank lines.
This is because the BOM is not stripped before inclusion into the
page, and acts like a character occupying a line of text.
You should ensure that the included files do not start with a BOM.
in: http://www.w3.org/International/questions/qa-byte-order-mark
Detecting BOMs
You can online detect BOMs within the content of your HTML page using the W3C Internationalization Checker
There is something before
<title>
remove everything before title tag
try to save your file with UTF-8 encoding
You certainly have a problem with the white space before in your tag. Please rewrite your code afresh using your replacing the spaces with spaces from your keyboard.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width">
<title>Calculadora dos Custos do Automóvel</title>
</head>
<body>
<p>aaa</p>
</body>
</html>

source code differs from inspect element code by changing tags positions

When my page is loaded from the server, source code differs from inspect element by changing the tags positions.
Here how the code looks in the view-source page
<!DOCTYPE html>
<html dir="rtl">
<head>
<link rel="stylesheet" type="text/css" href="css/mainclasses.css">
<title>title </title>
</head>
<body style="margin:0px;">
<div id="wrapper" style="margin:0px auto;">
but when I inspect elements all the tags inside the head tag goes under the body tag, and two double quotations appear under the body tag.
<!DOCTYPE html>
<html dir="rtl">
<head></head>
<body style="margin:0px;">
""
<link rel="stylesheet" type="text/css" href="css/mainclasses.css">
<title>title </title>
<div id="wrapper" style="margin:0px auto;">
The page works perfectly and all css properties appears and the title also appears.But I also want the HTML code to be good.
I searched for this and found many things that may cause this problem and none of them helped me.
There is no JavaScript code in my website, and I disabled all browser extensions so that JavaScript don't affect the page.I also checked the source tab in the developer tool and no JavaScript code were there.
I checked all my css properties and none of them causes this problem.(css also doesn't put strange chars).
The quotations that appeared are not spaces. I tried to put all my php code in one line and remove all unnecessary spaces and it didn't work.
I'm not using UTF8 BOM encoding, all files are UTF8 without BOM.so the strange chars are not due to file encoding.
I'm sure that the php files print the tags in the right order and don't print any quotation mark.
When I right-click the quotation marks in the developer tool,I only have the option Break on, and I can't edit it as HTML.
I tried to run the page from other web browsers and the problem didn't go.
I'm not using any framework.
What would be the cause of this problem, and how can I solve it?
I solved my problem.
I checked my HTML code. there were a symbol somewhere inside the head tag. I deleted the symbol and the HTML code in developer tool was corrected.

PHP does some things twice when this meta tag is in the file when browsed by Firefox

I am wondering why php does certain things twice, instead of once, when a certain meta tag is in the html portion of the file and the file is browsed by Firefox.
The code is like this:
<? /*...normal php code, including writing record to MySQL...*/
send('dan#example.com',$subject,$body);
?>
<!DOCTYPE html><html>
<!--PROBLEM on next line-->
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<title><?= $thisPage?></title>
<link href="<?= $cssURL?>css.freedom-and-purpose.css" rel="stylesheet" type="text/css" media="screen, projection" />
<?
include $dataPath . 'data.php';
?>
The result is TWO records written the database and TWO emails sent, whenever the page is called by FIREFOX. IE and Chrome not producing the problem.
There is a lot of other code in the program, but the reason I showed the portion above is that removing the line that starts with
<META...
solves the problem.
That meta tag is in there because one of the packages I run included it in their code sample.
So, what is that meta tag causes php to double do on DB-writes? And same thing on sending email?
Chances are this is actually a request for favicon.ico being caught by your main PHP file. Putting an empty file in favicon.ico or preventing your PHP from handling that URL should do the trick
enter code hereI would suggest you go through your code in some details and check its formatting.
Phil mentioned the meta tag about which I agree with. His suggestion of <meta charset="utf-8"> would be my preference.
Secondly the line sending the email looks odd. Single quotes aren't an option in php for data replacement, so the line send('dan#example.com','$subject','$body); would result in an email with the subject "$subject" and body just "$body".
Additionally send('dan#example.com','$subject','$body); appears to be missing a quote after $body.
I would advise you to move away from short php tags for opening and closing chunks of php <? ?> and get in the habit of <?php for clarity and to ensure the server you're using processes the code correctly.
Finally, I hope include $dataPath . 'data.php'; adds a </head> and a <body> to the html, as you're currently missing those too.

Understanding cache limiter | headers already sent php warning

I have a site that is functioning fine, but just by refreshing the main page (index_3.php) my error log populates with two warnings.
[08-Oct-2013 11:36:09] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home2/mysite/public_html/mysubsite/index_3.php:7) in /home2/mysite/public_html/mysubsite/functions.php on line 12
[08-Oct-2013 11:36:09] PHP Warning: session_regenerate_id() [<a href='function.session-regenerate-id'>function.session-regenerate-id</a>]: Cannot regenerate session id - headers already sent in /home2/mysite/public_html/mysubsite/functions.php on line 13
I have researched enough to understand that something is sending the page data before the session begins, but I can't seem to root out the cause. functions.php is taken right from this tutorial site under "Create PHP Functions." http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL
Also loading on index_3.php is get_opwire.php which just places a table. The beginning of get_opwire.php looks like:
<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();
sec_session_start(); is a custom session start located in functions.php
When I try to rearrange the order of the sec_session to the top or shift those around the table breaks. Index_3.php is just the main page, mostly html that has includes for a submit form and get_opwire.php
Would someone be able to help point me to the problem?
Edit: index_3.php (from line 1 through )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<meta name="Generator" content="Xara HTML filter v.6.0.1.335"/>
<meta name="XAR Files" content="index_htm_files/xr_files.txt"/>
<title>index_3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" type="text/css" href="index_htm_files/xr_main.css"/>
<link rel="stylesheet" type="text/css" href="index_htm_files/xr_text.css"/>
<link rel="stylesheet" type="text/css" href="index_htm_files/custom_styles.css"/>
<script type="text/javascript" src="index_htm_files/roe.js"></script>
<!--[if IE]><script type="text/javascript" src="index_htm_files/prs.js"></script><![endif]--><!--[if !IE]>--><script type="text/javascript" src="index_htm_files/prs3.js"></script><!--<![endif]-->
<script type="text/javascript">var xr_nextpage=""; var xr_transition=0; var xr_transitiontime=0;var xr_prevpage="index_2.htm"; var xr_btransition=0; var xr_btransitiontime=500;</script>
<style type="text/css">.xr_pbd {position: absolute; border:none; left: 50%; margin-left: -380px;}</style>
</head>
EDIT 2: somewhere inside index_3.php
<html>
<body>
<div style="width: 480px; height: 175px; overflow: auto;">
<?php include 'get_opwire.php'; ?>
</div>
</body>
</html>
Try removing the closing ?> tag from all your included php files.
Something somewhere before session start and session regenerate id is putting something on the page. If you are sure you're not echoing or printing anything to the page, it's probably a blank space at the top of one of the files.
EDIT 1
Some text editors/word processors save files with extra invisible characters at the top due to encoding. Try copying and pasting the text of the files into new files in a plain text editor or text editor recommended for coding, such as Notepad++.
EDIT 2
So, those were the most likely solutions.... It says
output started at /home2/mysite/public_html/mysubsite/index_3.php:7
which means that it's line 7 of index_3.php where the output starts at. There is probably a space there or maybe some kind of error that causes output to be sent.
If you really can't find that invisible output, you can use buffering with ob_start etc. to catch all output. Put ob_start before your session start and use ob_end_flush later to display the page. But that's not a real fix for the fact that you're sending output without knowing it.
EDIT 3: "No output" includes any and all HTML.
If there's something in any file that is not inside PHP brackets <?php ?> then that counts. Your index_3.php file starts with HTML. That is output. You have to do get_opwire.php first.
EDIT 4
Concerning how get_opwire.php has both page-start stuff and the table printout, it's an example of why it's a good idea to separate your display code from your functionality code. You have three options:
Have separate files. You would have a file like page_start.php that does includes and session_start that you include at the very top of index_3.php, and a file like display_table.php that displays your table that you include where the table goes.
Turn the table into a function. You would wrap the table output inside a function, include get_opwire.php at the very top of index_3.php, then call the function down where you want the table.
Use output buffering. Output buffering catches the stuff printed out so that you can use it later. It would go like this:
top of index_3.php:
ob_start();
include get_opwire.php;
$table = ob_get_contents();
ob_end_clean();
where the table goes:
echo $table;

Stray html tag- Netbeans warning issued

I have written a function where code related to the headers of a site are included. This is the function:
First of all, is there any problem with a coding such as the above-anything at all?
Secondly, Netbeans issues a warning in the html tag: Stray start html tag here.
I suppose this happens because the html tag is enclosed in a function and this functions does not contain the end tag-I assume.
function output_headers()
{?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Appointmetns24x7</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/admingeneral.css"/>
script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script>
</head>
<body> <?php
}
If there is nothing wrong with this coding scheme then I will just ignore netbeans warning and continue.
The actual error message is due to the <body> tag not being closed. Netbeans is picking this up and warning you that the HTML may be invalid... because it is invalid. Netbeans has no way of knowing just by looking at this function that it shouldn't be a complete HTML document.
If you must do things this way, you should avoid splitting individual HTML tags between different code blocks. Best practice would be to make sure that any function that outputs an HTML tag also outputs the corresponding closing tag.
However the way you're doing things is not ideal in any case.
You've split your template into a 'header' and (presumably) a 'footer' function. This sort of technique was quite common years ago, but these days it's not considered particularly good practice.
A better technique would be to have a separate template file, which contains all your HTML -- ie the header and the footer, with placeholders where you want the dynamic content to go. You then build the dynamic content bits as strings, and feed them into the template.
In its simplest form, this just means that the template is a plain HTML file with PHP blocks, for placeholders like <?php echo $mainBodyCode; ?> in the appropriate places. You then just need to make sure the placeholder variables are populated, and include it when you want to output the page.
Hope that helps.
I would sat the stray tag is the
xmlns="http://www.w3.org/1999/xhtml"
because you called <!DOCTYPE html> which isn't xhtml, so calling the xml namespace is invalid.
It seems that if you enclose root type html(html tag) elements inside a function and then close them in a place on the script outside the function the beginning tag was set-then this interpreted as error in Netbeans.
Someone can just choose to ignore it, else he should avoid coding this way, putting html header info in a function.

Categories