Understanding PHP code includes - php

I am pretty new to PHP. I am going through a website and am trying to find a source of some of the content and have come across what looks like where the source is, but I cannot decipher the code. Can someone help me understand what this means?
<?php
if (is_file($office_file))
include($office_file);
echo do_shortcode(ob_get_clean());
?>
Thanks!

I have copied your code and added comments that (hopefully) explain what is going on.
<?php
if (is_file($office_file)) // check if $office_file is a valid file
include($office_file); // the check passes, so now include the contents of that file inline
echo do_shortcode(ob_get_clean()); // not enough context to know really what this does, but it looks like its printing the result of parsing 'shortcode' from an output buffer
?>

Related

PHP eval code and store the result into a variable

I have continued my voyage into creating a extremely simple template engine.
Because I wanted to add logic to my template I eventually got back to the point that I allowed PHP tags into my code which I enabled by evalling the code.
Maybe not the best solution but when looking at the WordPress templates I noticed that the idea itself may not be that bad.
But now there still is one small problem left.
And that is that I want to translate the generated code.
But it has been evalled already. Hence parsed.
I thought of solving this problem by using ob_get_contents().
But this brought one more question and in case of errors it shows a white screen. (memory usage etc.)
Plus it still did not take away the problem of eval that it parsed the contents when evalled.
In short the class logic is:
Loading template files
Adding the contents
Compiling the template
Eval the code (but unfortunately also displaying the code)
Translate the code so I can translate the code parsed by a PHP script
I would love something like:
$code = eval('?>'.$tpl.'<?php');
$code = translate($code);
WriteCache($code);
SetDocumentHeader();
echo $code;
Would anyone know how to achieve this?
Thanks in advance!
$code = eval($tpl);
Check this out.

Why Do Single Line PHP Comments Break Page?

I apologize if this has been answered, but I haven't been able to find anything about it. I've been having trouble with a certain block of PHP code on my server and have finally realized that it doesn't like single line comments. With this code, the method get_all_articles is never called.
<?php
$article_class = new Articles();
// Fetch all articles
$articles = $article_class->get_all_articles();
?>
However, removing the comment or converting it to a block comment allows the page to render perfectly. I realize that I've already figured out what's causing the problem, but what I'm looking for is why or how I might be able to fix it. Any help would be greatly appreciated. Thanks!
Maybe the formatting is getting lost on upload to where line breaks are being deleted? Try downloading the PHP file after you've uploaded it and see if the line breaks are still intact.
This can be frustrating... One time I had an if statement that would ALWAYS execute, no matter what the values were...
Started out as this, where $x was equal to 5 (I verified this with debugging)
if($x > 10);
{
....
}
Eventually, I had it down to this:
if(false);
{
echo("This should never happen");
echo("but it does!!!!!!!");
}
After much loss of hair, I realized that I had a semi-colon at the end of the if() line, therefore translating into:
if(false)
/*do nothing*/;
{
//Nice block that always executes
}
The moral of this story is that while the problem you percieve is actually giving you a problem, it is not a PHP problem. Try to find out the root cause by first verifying that the actual code that is executing is EXACTLY what you typed. Re-download the file, publish with different protocol, publish as binary, check sha1sum() on files to make sure the same... Look and look and you will find it.
Let us know.

PHP Exits code from =>

I have a chunk of PHP given to me to put into a web page... the code looks like this:
<?php
$SVQuerystring = "";
foreach (($_GET) as $SVGetKey => $SVGetValue) {
....
?>
Problem is, everything after the '>' in the 3rd line gets printed as html text, so the php is being exited there. I can't find a similar issue anywhere, and I have never worked with PHP before. Any suggestions on what is wrong, or where to look? Any help would be greatly appreciated :)
I would seriously question whether the PHP is getting executed in the first place.
Try this code to make sure it is executing without any of your code.
<?php
phpinfo();
?>
If you see a bunch of output pertaining to PHP and your server then there is some other problem. If you don't see any new output, then PHP is not correctly installed on your server.

PHP echo in Javascript

I have this code:
document.getElementById('root').style.left = '<?php echo $page_position[$info["os"]][$info["browser"]][0]; ?>';
document.getElementById('root').style.top = '<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>';
Why won't this work like this?
Can anybody point me in the right direction?
EDIT:
<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>
echoed "top:300px;"
Sorry guys, very stupid error of mine :/
If you put it in a .js file, it won't work because it must be in a page that is parsed for PHP (.php).
Things to check:
Check to make sure those variables are available. print_r($info) will help you out.
Make sure that the code is actually being filled with those variables (symptom of #1). View source and check that they are there.
Ensure you don't have any JavaScript errors preceding your code. Open your console (like Firebug) and check to make sure you don't.
Ensure that a root element exists. Again, use your console.
My money goes on #3, you probably have a JavaScript error somewhere that is stopping execution of the script.

Using file_get_contents() on data files - PHP code not wanted

With my data files I use with sites I usually include some PHP code in them to prevent them being directly accessed, such as below..
<?php
if (defined("VALID")) {
?>
html code here
<?php
} else {
die('Restricted Access.');
}
?>
Now this works fine when I do a simple include..... however I am using one of these files to do some replacements in & hence need to make use of file_get_contents(); however when using this, not only do I get the HTML code, I obviously also get the PHP code returned with it..... this ends up going in the source, which I do NOT want.
Is there any way around this? Perhaps stripping the PHP code? Any better ways/suggestions?
If you want to make replacements on an output of a script try using output buffering.
Instead of file_get_contents('your-php-script.php') do this:
ob_start();
include('your-php-script.php');
$contents = ob_get_clean();
// do your replacements on a $contents
echo preg_replace("~<\?php(.*?)\?>~", "", $contents);
This should work to erase the PHP code in the file.
Why dont you use a hashed string in a session cookie to check it? I think its the best solution. So add to the cookie a hashed value, then check for that value on the file you need to check if its valid and voila!
Hope it helps!

Categories