I have a very strange problem.
When I open the script below in the browser and execute it, everything works as expected. Even if I call the script in the command line, I have the expected result.
But if I run the script in a background job, the script aborts in the "preg_match" function without any error. I don't have an error message for this anywhere, neither in the logs nor anywhere else. I also checked if the function exists and it does.
The script just seems to abort at preg_match and doesn't show any message about possible errors. So i'm getting only the output "CHECKPOINT 1".
I have checked the PHP versions and everywhere the same version (same path). Also the settings of pcre are identical.
Is it possible that I have different modules or functions for preg_match? Is there any way I can check what happens in the function?
with try & catch i don't get any information about the problem.
<?php
class pcre_check
{
public function setUp() {
}
public function perform() {
echo "CHECKPOINT 1";
preg_match("/[0-9]{4}/", "6876 ABCDEFGH", $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
echo "CHECKPOINT 2";
}
public function tearDown() {
}
}
$test = new pcre_check();
$test->setUp();
$test->perform();
$test->tearDown();
?>
Systeminfos:
Apache server
PHP 7.4.2
The same script works online on the production server. Only on my local server I have this problem.
It seems there is an issue with regular expressions. The function preg_replace isn't working too but str_replace with replacement of a normal string is working.
It seems that it is working now. I didn't find the problem, but after the update to macOS Big Sur it seems to work now.
Related
PHP 5.6, Windows Server 2016, IIS 10
The error is familiar to anyone in PHP:
PHP Fatal error: Cannot redeclare mainContent() (previously declared in [the controller file]:12) in [some included file] on line 29
Here's the thing: this error just appears. A program that has been working for days and hasn't been touched just starts throwing the error. When I restart IIS the error goes away and everything goes back to working normally.
This is similar to this bug: https://bugs.php.net/bug.php?id=30075
So here's what it's not:
The function isn't declared twice, anywhere in the program. The function name in question "mainContent" is reserved and used once and only once in any program.
Every include/require is include_once or require_once, no exceptions, so it's not a double-include.
I am sure of these two facts because I'm the only developer, so there's no one else to make changes, let alone break the code without me knowing.
When I search for a solution, all I find whether I'm on Google / Bing / Yahoo / stackoverflow / reddit is the usual explanations of double-include or declared the function twice. The error just shows up on an untouched program that's been working for days without this error, then goes away with an IIS restart. It's not in the code!
I can't be the only person who's ever encountered this problem, but I can't find a solution.
(I intend to upgrade the site to PHP7, but it's not a 1 <=> 1 transition and the client's daily business tolerates no down-time so I'm stuck here for a while longer.)
Here's a simple example to reproduce the problem you have reported:
<?php
function foo(int $a) {
echo $a;
echo "\n";
}
function bar(int $b) {
echo $b;
echo "\n";
}
echo "I'm running. gonna redeclare\n";
function foo(int $x) {
print "i'm the redeclared foo\n";
}
foo(23);
echo "finito\n";
You can save it to a file named "test.php" and execute it with
php "c:\path\to\test.php"
The error message is telling you that you a function with the same name has been declared twice.
Mind you, if this is a web app, then the concept of "running for days" is not exactly correct because each request to a specific PHP script in a web application is a new execution of the script in a sort of clean-slate environment. Some state may be shared such as the Session, and out of band data like files and database objects, but that's about it.
Unless it's some kind of long-running script, then the number of days is not relevant.
It's entirely possible that this code-path simply only gets traversed in some specific conditions.
Without any additional context, this is the best I can offer.
I'm trying to write a custom wrapper in PHP using the Stream Wrapper class. What I have now is pretty much simple and straight forward.
class Stream
{
public $resource;
public static function wrap()
{
stream_wrapper_unregister(self::PROTOCOL);
stream_wrapper_register(self::PROTOCOL, __CLASS__);
}
public static function unwrap ( )
{
stream_wrapper_restore(self::PROTOCOL);
}
public function stream_open ( $path, $mode, $options, &$openedPath )
{
$this->unwrap();
// Open memory
$this->resource = fopen('php://memory', 'rb+');
$code = file_get_contents ( $path );
// Write code to memory
fwrite($this->resource, $code);
rewind($this->resource);
$this->wrap();
return $this->resource !== false;
}
// Left out the other methods that the stream wrapper needs
// to keep this example simple
// ...
}
In the beginning of my code I open the stream by calling: Stream::wrap().
Then it basically reads any file that is require'ed or include'ed throughout my application. The code is then put into php://memory and that's it. Nothing special yet.
Bottom line is, is that this piece of code should work. But it throws an Internal Server Error when I try to run my application. I have a Linux hosting provider (PHP 5.4.21) where my code is currently hosted.
But when I move my code from my current hoster to some other Linux hosting company (PHP 5.5.x), then everything works fine.
When I even move the code to my local computer (PHP 5.4.6) and run it, then everything works fine as well.
So obviously the problem lies at the hosting company that I currently have. I know I have to mail them. But I have no clue what is causing the problem exactly. It would be good if I could at least point them in a certain direction. All I have now is
my code isn't working on your servers which is working fine on some
other server
I'm afraind that that isn't enough information for them.
I have checked the php://memory limit which is set to 256M. That should be more than enough. So I have no clue what else to look for. The Apache log didn't had any information either.
So I was hoping anyone here had an idea of what could be causing this problem. Perhaps some permission issue somewhere or something?
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'm using MAMP with PHP 5.4.10 and I have a problem with the following MWE:
<?php
trait T {
public function hello() { echo 'hello'; }
}
class A {
use T;
}
$a = new A();
$a->hello();
?>
The page shows 'hello' on the first load. But then, when I hit refresh, I get an Error 500.
If I modify the file (just by adding an empty line somewhere for instance) and refresh again, 'hello' shows up again. Hit refresh again, and the Error 500 is back.
Any clue where this might be coming from?
Update:
This shows up in the PHP errors log (nothing in the Apache errors log): PHP Fatal error: Call to undefined method A::0?
()
(the 0 doesn't always have the same name when I repeat the operation).
Xcache might be the problem here, try turning caching off (or at least xcache) and try it again
I had the same problem, and thanks to the #Leon Weemen i focused on the XCache. I found this bug (which is fixed in XCache 3.0.1) to be exactly what causes the problem (my version of XCache was 2.0.0). They suggest you to set in your php.ini the following values to solve the problem;
xcache.mmap_path = "/tmp/xcache"
xcache.readonly_protection = on
However, this workaround does not solve the problem for me. The only way I was able to disable the XCache was by using the ini_set() PHP method. The following snippet at the very begginning of my application solves the problem and is ready to use XCache as soon as it is updated:
try{
$xCache = new ReflectionExtension('xcache');
if(version_compare($xCache->getVersion(), '3.0.1', '<')){
ini_set('xcache.cacher', 0);
}
} catch(ReflectionException $e){
// xCache not installed - everything should work fine
}
I'm using Wampserver on Windows XP and Apache 2.2.21.
This code crashes when executed:
<?php
class Tax {
public static function load($id)
{
echo $id;
}
}
$tax = Tax::load(1);
?>
This code DOES NOT crash when executed:
<?php
class Tax {
public static function load($id)
{
echo $id;
}
}
$tax = Tax::load(10);
?>
Made simple, if I pass a single digit number to the function load, Apache crashes. However if I change the function name for anything else than load, it works fine. Also, I'm aware the function does not return anything, but it should still compile at the very least.
This code works too:
Tax::load(5);
I am kind of lost right now as I have no clue why this code would cause a crash. Help me please.
EDIT
I'm using PHP 5.3.10
There are no errors in the Apache error logs
Changing the variable name $tax for anything else (e.g. $a) works. I'm even more confused.
I know apache crashes because I get a windows error saying so. The Apache server restart automatically and is back up a minute or so after it crashes.
If Apache is really crashing then you should look through the following file to see what happened:
[WAMP directory]/apache/logs/error.log
Try to change the name of $tax (the variable).
just because it echos and is not return shouldn't crash apache.