$_POST loses data in included file - php

I'm making a simple CMS thingy in PHP, and I made a simple guestbook module for it.
In my index.php I include template.php (which has HTML) and calls a function which returns the content for the specific page.
<div id="content">
<?php content(); ?>
</div>
The content function is inside functions.php which is include before template.php inside index.php.
Inside content():
print_r($_POST); // the data is displayed :)
require_once(BASE_URL_MODULES.$tempMod."/".$tempMod.".php");
Inside the PHP file I included (with require_once) all $_POST data is lost. print_r($_POST) shows a empty array.
Am I doing something wrong a.k.a. is this normal behaviour? If so, does somebody has a solution?
I'm using the ancient 4.4.9 version of PHP. Can't upgrade it because the provider doesn't want to upgrade the stuff.

Related

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()

Can I assign an include_once file to a $var with PHP?

I am trying to roll my own MVC purely for learning purposes, but am hitting a snag when I try to load in the view files. Here's my problem:
Front controller grabs appropriate page controller, page controller include_once() the appropriate view. The view is a file containing HTML and PHP. When I return the include file from the controller, the included file content appear out of sequence of the PAGE layout intended. (example: I want to display HEADER CONTENT FOOTER, but when I include the view's contents, my controller spits out CONTENT, HEADER, 1, FOOTER).
In my front controller I am attempting to set my included file contents to a variable I then echo in between my HEAD and FOOTER in the PAGE template. But this does not work as intended. I can wrap my view file contents in: return "code" but I don't want the added headache of worrying about stray quotes/double quotes in my view code that could break the script. I have tried file_get_contents(), but this only spits out a string (albeit in the right place/sequence in my template) and does not the PHP in my view files correctly (ie at all).
My question is: Is there a way I can return my view file to my front controller using include_once() in a variable? I can't seem to find a adequate answer to this. I'd be happy to add my code if it would help.
You can assign a return value from an include(). A lesser-known feature of this function is that you can have a file like this:
<?php
// config.php
return array('database_host' => 'localhost');
and you can read it in thus:
<?php
$config = include('config.php');
However, you're wanting to do this:
<!-- text.html -->
<h1>Heading</h1>
<p>Hello</p>
and read it in with this:
$html = include('text.html);
You can try doing that, but as soon as the include() scans the file, the output will be sent to the web server. That's how PHP works. Since there is no explicit return value, moreover, the $html variable comes back empty.
Thus, we use the output buffering functions to modify PHP's behaviour: we start buffering, and then we fetch it and clear it. This is then not sent to Apache, unless of course you choose to turn off output buffering and echo what you have collected, in which case it goes back into the normal output buffer and is actually sent.

Is it possible to call PHP in html?

I have a PHP that returns some HTML code, for example, if I execute this in the browser,
http://www.myserver.com/myscript.php?size=300
this will return some html code like this,
'<div><img src="..." /></div>'
I am wondering if it's possible I can call this php script in my html code directly? For example, if I want to use the output from the PHP script in my Wordpress sidebar widget?
You can use file_get_contents() like this:
<div class="sidebar">
<?php
$html = file_get_contents("http://www.myserver.com/myscript.php?size=300");
echo $html;
?>
</div>
If your Current page's extension is not php, you cannot call php in it directly.
You have to use some javascript/jquery/ajax to run the php file externally and load the data into a class or id using one of those javascript based code.
If your current page is already a php file than,you can use include(); or require(); functions and it does the job.
above answers are correct, but that might be not concerned for wordpress, that are of core php functionality.
i have a different way for sidebar use, that's provide by wordpress already.
it is an inbuilt functionality, please take a look towards my answer.
Show active sidebar in Wordpress
How to make wordpress sidebar
you can found ::
try this one, it is different way of using sidebar
[1] put your all "left_bar"(sidebar) code in a new file named "sidebar-left_bar"
[2] save it with header.php, function.php and all files (saving location)
[3] now just use <?php get_sidebar( 'left_bar' ); ?> where you want to use
Thanks

OpenCart PHP Custom Function Not Working

I'm having a bit of difficulty including a custom php function in an opencart template. Here's what's going on.
In the header.tpl file, I'm inserting this code in the portion:
<?php
$blah = file_get_contents("my_url_goes_here");
?>
<?php
echo $blah;
?>
Then in the footer.tpl file, I'm inserting this code:
<?php if(!isset($blah)) die; ?>
The purpose of this is to detect whether my blah function in the header is still there, and if it's not there to exit the script.
However, it seems the die function is throwing me for a loop, because if I move it up to the header.tpl file, so all the code is together, it works perfectly as designed.
But if I move it to the footer.tpl file, where I want it to go, it kills the script no matter what, and I end up with a blank page - even if the blah variable is intact in the header.tpl file.
This has got me pretty confused, and I can't figure out why it's not reading my variable if it's in a different file, as both of those files should be included on every page.
Thanks in advance for any thoughts!
They are two separate entities, and as such data from one will not be in the other. I'm guessing this is some kind of detection script for reversed code like many do for wordpress templates. OpenCart is MVC based not linear and as such, $blah in the header is disregarded almost as soon as the code in it is completely executed

replacing file contents in php and saving to a new file

So I would have a template file called template.php and inside, it would have:
<html>
some other content
<?php $name ?>
</html>
Essentially, I want to pull the name from the database and insert it into $name and save that entire page as a new file.
So let say in the database, I had a name called Joe, the new file I create would have the following content inside:
<html>
some other content
Joe
</html>
The only issue I am having is finding the right functions to actually replace the variable from the template file, and being able to save it into a new file.
That's not how templates usually work. You should have a template file, substitute variables for their values, and display it to the user. You can cache these generated templates (the ones that are always the same) but you never make a new template file for each user and save it permanently. That makes managing and updating the application way more difficult than you want.
I recommend using a simple template system like RainTPL. That's what I use for most of my projects... it's very simple to use and provides all the basic functionality you would need.
You can also use just PHP for this... there are a few answers on SO that cover this so I won't get into it.
Using PHP as template engine
Using Template on PHP
Look at the answer I gave to this question a while back. There I explain how "variable parsing" usually works.
When I create a template loader I generally use output buffering with php include. This allows you to run the page as a php file without displaying its content before you are ready. The advantage to "parsing" your php files this way is that you can still run loops and functions.
Here's an example of how to use output buffering with PHP to create what you're wanting.
The Template template.php
<html>
some other content
<?php $name ?>
</html>
Where your database code and stuff is index.php
<?php
$name = 'John Doe';
if( /* some sort of caching system */ ) {
$parsed_template = file_get_contents('parsed_template.html');
}else {
ob_start();
include 'template.php';
$parsed_template = ob_get_clean();
file_put_contents('parsed_template.html', $parsed_template);
}
echo $parsed_template;
?>

Categories