After so much trouble I find out that when I use the flush function in my PHP mail script then I get garbage or dump characters on browser like below.
The code is below
if ($mail->Send()) {
echo "<br><font color=darkgreen>[$num successful send to $to]</font> ";
// flush();
return true;
}
If I comment that flush line then out is simple English but I uncomment that the whole page the text looks like garbage.
Now is that a PHP problem, browser problem or server problem?
If I use the same script from the shell, I mean execute inside the shell terminal then I can see the HTML output. But it does not work in browsers.
I found the answer to my own question. I had to turn
zlib_compression off
in my php.ini settings file.
(What does that mean and why did it work?. I had been trying this for 1 year but could not resolve the problem but now it worked.)
Related
This question already exists:
PHP's white screen of death [duplicate]
Closed 7 years ago.
I'm calling a php script with shell_exec that runs fully operational WHEN called from the terminal (php somescript.php). However, using shell_exec I am able to check the "echo test" I'm issuing inside the script but the database queries with $mysqli are not being commited. Here is the script (again, works well when called from terminal):
//CALLED PHP SCRIPT
include_once("../mysqlconnection.php");
$query = "some query";
echo "this gets printed via shell_exec"
$mysqli->query($query); //this does not execute...
and mysqlconnection.php:
//connects well
$mysqli = new mysqli($db_ip, $db_user, $db_password, $db_database);
What exactly could be the problem? I call the script with shell_exec('php some_script.php');. Thank you very much for your help.
EDIT:
As I said, everything works well via terminal. A couple of things:
I have a debug function that is NOT being called although it is in mysqlconnection.php. Worst of it, if I completely mess up the code trying to provoke variable erros (like removing the include) it still "runs". Does nothing.
If I echo in the original php script it gets printed (as in the first code example echo "this gets printed via shell_exec" EVEN if the rest of the code is messed, semicolons missing and etc. I don't really know what might be off, it's odd. It only points all the flaws if ran by terminal although it also echoes the message if called from a php script
root
/folder/called php script
/script that calls the other php script
/mysqlconnection.php
My code is basically exactly how it is here, no need to change anything...
What exactly could be the problem?
Lack of error reporting.
Having errors properly reported, you will see what is wrong with your include and mysqli query.
I'm debugging a PHP script that runs a couple SQL queries and emails a set of users. I'm sure this is a very basic thing, but every time I try to echo, print, or print_r it doesn't appear while running the script.
So say I have this in the script:
print("This should print");
echo "on the command line";
When I run the script via command line php script.php it doesn't actually print anything to the command line while running the script.
Is there a way of having PHP print to console? I feel like I'm missing something extremely basic here.
Thats the method of doing it.
Things to check for are output buffering
http://php.net/manual/en/function.ob-flush.php
Is that code actually being run ? Make sure it doesnt branch before it gets there
A good approach could be:
function log($message)
{
$message = date("H:i:s") . " - $message - ".PHP_EOL;
print($message);
flush();
ob_flush();
}
It will output to your terminal each line you call. Just use :
log("Something");
log("Another line");
The following code working fine for me.
<?php
print("This should print");
echo "on the command line";
?>
with tags.
I know it is old, but I am going post my own solution to this just in case if someone would run into similar situation. I had a problem that a legacy command line PHP script wouldn't print or echo anything to the terminal when DSN is incorrectly configured and the script got stuck for very long time (not sure how long, never waited for it to terminate by itself). After putting ob_end_flush() in the entry line of the script, the output came back to the terminal. So it turned out that all output was buffered and since the script stuck at some point, the output stayed buffered and so never went to the terminal.
I have a PHP file with the following contents:
1. <?php
2. $string = 'Hello world!';
3. echo $string;
4. ?>
I put a break on line 2 and line 4. I run the debugger. The browser output opens, the script has halted on line 2, and the browser output is empty. Makes sense! Then I jump to the next breakpoint at line 4, after the echo command. AT THIS POINT, should the browser update and display 'Hello world'? Or do debuggers not update the browser output step-by-step, and thus are NOT useful for browser display and are only useful for inspecting the inner-code itself?
Thank you!
The answer is a bit complicated here. In general, Xdebug will not stop output to the browser, and PHP will not do so either unless you have output buffering or automatic compression turned on.
However, the web server might buffer data up to a certain point before it sends it onwards to the browser. You can force the web server to flush its buffers by calling flush(): http://php.net/flush. Be aware though that some browsers, also wait until they have had enough data to actually show anything.
I am very new to php and I tried to write this function. Now it seems like the function is not Defined. Nothing happens when I open the php file and if I try to use console to run it. It gives an error --
contentcheck('ex1.php','Bajestani')
ReferenceError: contentcheck is not defined
The Code is below.
<?php
if(contentcheck('ex1.php','Bajestani')===true)
echo 'Got it';
function contentcheck($filename,$phrase)
{
$content = shell_exec('C:\xampp\htdocs\docman\pdftotext '.$filename.' -');
if (strpos($content,$phrase) !== false)
{
return true;
}
else
return false;
}
if(contentcheck('ex1.php','Bajestani')===true)
echo 'Got it';
?>
Thanks In advance
You state that you try to run the function from the console.
In addition, ReferenceError: contentcheck is not defined is a Javascript error, not a PHP error.
Both of these facts lead me to the conclusion that you are trying to run the PHP code from inside the browser.
Please note that PHP code is not available from within the browser -- the function will indeed be undefined if you run it in the console, because PHP is run on the web server, not in the browser. The browser will never see your PHP functions; it simply sees the output of the PHP program (eg the HTML code, etc that is printed from by your PHP program). The PHP code itself is never seen by the browser.
It's not entirely clear what your program is supposed to be doing but what is clear is that the way you're trying to run it is not going to work. You're going to have to re-think this one completely, and possibly learn a bit more about how client/server systems work, and PHP in particular.
I have a PHP script that creates other PHP files based on user input. Basically, there are files containing language specific constants (define) that can be translated by the user. In order to avoid runtime errors, I want to test newly written files for parse errors (due to "unusual" character sequences). I have read several posts here on SO (like PHP include files with parse errors) and tried a function that uses
$output = exec("php -l $filename");
to determine whether a file parses correctly. This works perfectly on my local machine, but at on the provider's machine, the output of calls to exec("php ...") seems to be always empty. I tried a call to ls and it gives me output, leading me to the assumption that PHP is somehow configured to not react to command line invocations or so. Does anyone know a way around this?
EDIT: I forgot to mention, I had already tried shell_exec and it gives no result, either. In response to sganesh's answer: I had tried that too, sorry I forgot to mention. However, the output (second argument) will always be an empty array, and the return value will always be 127, no matter if the PHP file to test has syntax errors or not.
I had the same problem. The solution that worked for me was found in running-at-from-php-gives-no-output. I needed to add output redirection.
$output = exec("php -l $filename 2>&1");
You can try with exec second and third arguments.
second argument will have the output of the command.
third argument will have the return value.
And exec will return only last line of the command.
$filename = "a.php";
$output = exec("php -l $filename",$op,$ret_val);
print $output."\n";
print $ret_val."\n";
var_dump($op);
By executing shell_exec(), you can see the output as if you executed that file via command line. You can just see if there is an error right here.
<?php
if (strpos(shell_exec('php -l file.php'), 'Syntax Error')) {
die('An error!');
}
There may also be a possibility that shell_exec() or exec() may be disable by your host.
Nice idea to check the file validity :-)!
Now, from the PHP manual for exec():
Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have components in the path to the executable.
Can you check if this is not the case for you?
Also, can you check by providing the full path of the PHP interpreter in the exec() instead of only php. Let me know how you fare.
Pinaki
the correct way is to add >2&1 as tested on a windows system using imagemagick!
I worked around my original problem by using a different method. Here is what I do now:
Write a temporary file with contents <?php include "< File to test >"; echo "OK"; ?>
Generate the correct URL for the temporary file
Perform HTTP request with this URL
Check if result equals "OK". If yes, the file to test parses without errors.
Delete temporary file
Maybe this could be done without the temporary file by issuing an HTTP request to the file to test directly. However, if there is a parse error and errors are suppressed, the output will be empty and not discernible from the output in the case of a file that gives no parse errors. This method is risky because the file is actually executed instead of just checked. In my case, there is only a limited number of users who have access to this functionality in the first place. Still, I'm naturally not entirely happy with it.
Why the exec() approach did not work, I still do not know exactly. pinaki might be right by suggesting to provide the full path to the PHP executable, but I cannot find out the full path.
Thank you everyone for answering, I upvoted you all. However, I cannot accept any of your answers as none of your suggestions really solved my problem.