Php Include function causes whitespace and ignores style - php

I have a page that uses another page via the include() function. The page that requests and uses this external page has some html embeded as well as some output using the echo function. Here is an example of what I am referring to.
page Apple.php contains:
<html>
<head></head>
<body>
<h1>I have some content here.</h1>
<div id="format" style=" width:20px; height:20px;"><?php include("Orange.php") ?></div>
<h2>Some more content here.</h2>
</body>
<html>
page Orange.php contains:
<?php
$astringA='blue';
$astringB='berry';
include("someExternalPage.php");
echo "fruit is ".$astringA.$astringB.$externalVariable;
?>
Whenever the Apple.php is viewed in browser, only the top part of its body is shown(the part above the include function) and everything below the div box is lost and filled with whitespace. In addition the included file ignores all style that the div box has. The same is true for the Orange.php; all output below the include() function is lost. How can I fix this so that the included file will conform to the style I want for it and also to continue to output the rest of the content on the pages?
I also notice that even if I try outputing the rest of the page through php such as
echo ">h2>Some more content here."; (I deliberately inverted the first ">" in the line above so that I illustrate the code here)
the included still takes precedence over the current page and thus the output is never shown.

Smells like you have a hidden fatal error here.
Try using error_reporting(E_ALL) and run the page to see if you have an error...

There's something wrong either with someExternalPage.php or $externalVariable.

If someExternalPage.php is actually an external file (ie you are including a file such as http://example.com/test.php) you may need to check that you have enabled allow_url_include in your php.ini
Alternatively, please make sure someExternalPage.php exists.

Looks like you're missing a semicolon after the include in Apple.php. Can we see the output when you try to load Orange.php? It's helpful to see exactly where the flow is breaking.

Related

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

Can't locate output affecting HTML and file output

Below are screenshots of the output within Chrome dev tools, and one of my controller methods.
The second screenshot is of an empty controller that simply die();'s.
The third is the controller that is outputting the second image.
I've trawled all files in my app in an attempt to locate the cause of the erroneous output, with no success.
It appears to be an accidentally commented <?php tag, but I can't see how that would cause the doctype error?
I should note that this is happening globally, and I've already checked my existing doctype declaration and opening php tags.
At my wit's end, thanks for any help.
It turns out the extra output was caused by an empty Helper file, that only contained <?php and nothing else.
This appears to have resulted in the <?php not being parsed as PHP, and output as text in an HTML comment somewhere down the line.
try to remove the comment
and i you want use php comment it should be inside php tags
<?php /* comment */ ?>
and if you want use html comment it should be inside html tags
<html>
<!--comment -->
</html>

Execute the Code in a PHP Page And Echo the HTML Output

I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()

Having Trouble Stopping PHP, Inserting HTML and JS, Then Restarting PHP

I'm trying to insert some HTML and JS into an existing dynamic PHP page.
I felt that a switch() statement would be my best alternative.
Allow me to explain;
I have a switch statement embedded within a PHP page and when a certain condition is met I'm including an include().
Here's what my code looks like. (abbreviated)
Within my PHP file that displays the page;
switch($color)
{
case "Red":
include ('red_widgets.php');
break;
}
Then red_widgets.php looks like;
?>
<div class="Shadow">
<script language="JavaScript" src="AScriptThatDisplaysWidgets"></script></div>
<?php
But when I run the page, I'm getting "?>" outputted and not the JS.
This may be obvious to some of you but if you could share with me what I'm doing wrong I'd be most appreciative and I thank you all in advance.
When you include, you're in HTML-mode. Just omit the leading ?> and the trailing <?php and that will work fine.
You do not need to "stop PHP". Just update the appropriate PHP/JS/HTML file and the next time you request it from the server it will deliver the updated file/script output to your browser.
As to not receiving the correct output you have an error in your code.
Is the include file surrounded by <?php and ?> ?

structure of a php file from a facebook button redirect

I am running a php page which i get to via a redirect from facebook api button:
<fb:login-button class="fb-login-button" scope="user_birthday,offline_access,publish_stream'"
onlogin="window.location='xxxxx.php';">Connect with Facebook</fb:login-button>
I my target page I don't have any html, I just have
<?php
code.....
echo '';
if (xxx)
header("Location: http://xxxx.com/xxx.php");
?>
my question is, is this correct?
Where would the echo data output go to if I don't have any html tags?
Should I add html,head,body tags to this page?
My page is not running correct, that is way I am asking.
It depends on what you want to echo. it doesn't have to be html, it can also be plain text.
What you definitely can't do is echo before sending headers: (php header)
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include(), or require(),
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.

Categories