PHP system function strange behavior - php

I have a strange behavior with PHP system function. In this php script there are only 2 instructions (the rest being pure html):
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
<?php echo system('cgi-bin/gallery2/galleryview.cgi'); ?>
The first cgi just returns a single line as you can check here
http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi
It returns
My Gallery
But the whole php script returns My Gallery Twice:
My Gallery
My Gallery
http://reboltutorial.com/gallery2.php
Is there a reason (I don't use My Gallery in second cgi script of course see http://reboltutorial.com/cgi-bin/gallery2/galleryview.cgi) and how to prevent this ?
Thanks.

Update: The system function will do two things. The first is, it will run a command and pass its output through to the browser and/or output buffer. The second is, it will return the last line of output. So when you're saying
echo system('/...');
You're saying "Hey system, output the results of this command" and then "Hey ehco, output whatever system returns". Removing the echo
system('/...');
will fix your problem.
A few other things to check
Are you sure its galleryheaderview.cgi that's returning things twice? Comment out the include to make sure its actually the script that's echoing My Gallery twice
Is your PHP page/program included/constructed in such a way that galleryheaderview.cgi is being called twice?
Are you sure that calling the URL http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi is calling the same command line as cgi-bin/gallery2/galleryheaderview.cgi?
If you've checked out the three items above, you'll need to drop into the source of galleryheaderview.cgi and see why its outputting the header twice.

Are you absolutely sure that nothing else is outputting the My Gallery before this line? You should try removing it, and see if it goes completely away or if there is still is one "My Gallery"
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
If this doesn't bring you any further, maybe you have included some php file twice?

Related

Str_Replace is replacing php syntax

functions/SkriptParser.php
<?php
$text = file_get_contents(basename($_SERVER['PHP_SELF']));
preg_match_all("/{(.*?)}/", $text, $matches);
var_dump($matches[0]);
echo str_replace($matches[0],"Test",$text);
?>
Is my current code, this is called from my index page which is here:
index.php
require_once 'functions/SkriptParser.php';
When i open index.php it replace the correct strings [ It'll replace any string inside {} however, it seems to be replacing <?php and I have no clue why.
Any ideas?
I'm not sure I fully understand what you are trying to do.
This line $text = file_get_contents(basename($_SERVER['PHP_SELF']));resolves to a local filename which will be the name of the script it's included in.
It won't load the page which that script would output, it will load the file without executing the code. The contents of $text will be a string containing the pure php content.
When I run your code from the command line and use "Here is a test of {your code}" as my string the output is:
{your code}<?php
require_once 'includetest.php';
print_r(basename($_SERVER['PHP_SELF']));
echo "Here's a test of Test";
If I run it from a browser and view the source I see that too, but the browser escapes the php so it isn't rendered on the page. So (for me at least), your concept kinda works. However, the fact the code isn't executed means it will never do what you intend.
Here's what I don't really understand though. In essence within index.php what you are telling your code to do is load a script which will load another copy of index.php and replace content within it.
If you want to change the behaviour of index.php then alter the code within index.php, don't generate unsuitable output and then load another script in an attempt to parse it before returning it. Just output it the way you want the first time.
Where is the content within the {} that you want to remove coming from? Why do you want/need to remove it? If you can clarify exactly what your aim is then it will be easier to suggest a solution.

Manually determine the last thing PHP does before it renders a page?

I'm trying to figure out the best way to unset a php session variable, after all pages have loaded and the page will be rendered to the browser. I have a page that includes & requires several pages to be rendered. I want to know if there is a built in php function that will tell php to do something right before the page is completed. Or what would the best way/practice to do this?
EDIT##
Here's what I added...
function unsetHIST_ID(){unset($_SESSION['Hist_CID']);}
register_shutdown_function('unsetHIST_ID');
Am not sure why you want this but if you want a function to be executed after script execution finishes or exit() is called then you should look at register_shutdown_function
Example
function shutdown() {
echo "Am dead anyway";
}
register_shutdown_function('shutdown');
echo "<pre>";
echo "Hello am Alive",PHP_EOL;
Output
Hello am Alive
Am dead anyway <---------------This would always run last
There's the .ini directive auto_append_file, which is parsed last by PHP if specified. It's basically treated as if you'd put require('somefile.php') as the very last command in a file yourself.
You need something like
register_shutdown_function ('my_atexit_function');
or maybe you can do this with
ob_start('content_handler');
The first function will be called immediately before exiting, which is not exactly what you asked for; and the second just before output, but that allows you to do something with an output that's already made static.
I'd go with register_shutdown_function though.

Why does PHP not show HTML code before going into the for loop?

I have two different servers and a script like this:
echo "<div style=\"some style\">whatever</div>";
for($i=0;$i<150000;$i++)
{ ... }
Now, I realized that on my first server the PHP-script will directly show me the whatever and then start going through the for loop. The second server though doesn't show the div, it directly starts with the loop. But I need the script to show first the HTML code and then do the rest of the script.
Is there a possibility to change this?
Thank you!
Check your php.ini for output buffering
You should try flushing right after the echo before the for-loop.
What happen swhen you got to the website:
Browser to server, hey dude, can I have this file?
Server handles the request, searches for the file
Server notices, hey, got a PHP script, lets parse it.
The script goes into an infinite loop.
The script needs to finish before it can send it back to the browser, thus your output will be empty.

Unbelievable PHP script won't echo out string

I have a test.php script which contains this:
<?php echo 'test'; ?>
When I point to it via my browser, it works and prints out "test" as expected.
I have another script which I am trying to test but however, it is ignoring all my echos! I thought I would put an echo right at the top as this will surely work, but when I get to this script via POST request from a form. It does not even echo out what is at the top of the line:
<?php echo 'START START START';
error_reporting(E_ALL);
I even point to from my browser and still no output?
What the hell is going on?
Update
The error log shows this :
PHP Parse error: syntax error, unexpected T_ECHO in /var/www/tester/convertor.php
But the echo is at the top of the line. I am going to set diaplay errors.
You have a parse error, so the script isn't even executed. So it's expected that it won't output anything.
The parse error can be for any echo in the script. It may be because of an "echo" statement after a line missing a semicolon, for example.
Things to try:
Check your error log
Check the headers (is it coming back 404?)
Make sure you view-source: don't just look in the browser
Delete everything except the echo. If it works, add things back a bit at a time until it breaks.
Load your script in a hex editor and make sure there aren't any weird characters in there
Are you including this file from another file? If so, check the other file too. Watch out for output buffering.
Put exit; after the echo start start start and see if that works. Look in the apache error log for clues. Comment out the rest of the PHP code in the file and see if it works then...
UPDATE
I have had this when copy pasting white space from a browser sometimes - e.g. copy/pasting some code from a web page. Sometimes weird control characters get embedded invisibly in the white space, and I find that deleting the whitespace and re-entering it fixes it. Did you copy paste anything into this file?
Are you hosting this page on a server with PHP installed?
If you just have a .php file on your hard drive somewhere and open it in a web browser, it won't work. You need to be running a web server with PHP extensions and access the file using the HTTP or HTTPS protocols.
On a similar note to this i have seen alot of scripts throw errors like this when they have come from developers on windows (i'm on linux).
I have to go to the start of the php file and hit delete a couple of times to get rid of some invisible characters before the script will run.
You're missing the ending ?>
<?php
echo 'START START START';
error_reporting(E_ALL);
?>

So Echo isn't echoing

So I've got all of this really neato PHP code and I've started doing some reuse with functions out of necessity. I'm debugging, trying to figure out why I can't delete comments on my website while I'm deleting folder (because who wants orphaned comments?)
So I have a call to deletefolder( $parent) inside a file called deletefolder.php. This a function that will recursively traverse my tree structure.
I've include another file inside deletefolder.php. The file is call helpers.php, and it contains the deletefolder function.
The deletefolder function calls deletecomments (kills all the comments per file) and delete file (which kills the file itself).
Now, all of it is just slathered with echo statements to help me figure out what's going on. When I call this combination of functions from other locations I don't seem to have a problem getting messages. But when I call them from the deletefolder.php page I don't get any. Does anybody know why this would be the case?
A few things you might want to verify.
Check the source of the output. You might be echoing straight in a middle of a HTML comment or a tag which is hiding the output.
Are you using output buffering (ob_start()) ? You might be clearing the buffer at some point in your code and forgot all about it.
Different files with the same name but not in the same directory. Do a die() in your function to make sure it actually reaches your code. You might be editing/including a copy of your file (happened to me quite a few times).
Well, I seriously doubt you've found a bug in the echo command, so the problem is with your program logic somewhere. Without seeing your code, it's impossible to say really. Perhaps there's some variable being set or unset unexpectedly, or you're not actually include()ing the files properly.

Categories