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.
Related
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.
My php project is now working on Apache with mod_php. I want to open OpCache to accelerate my code. But I'm worry about if it's safe to update the code.
The validate_timestamps should be 0. Because the code may expire when you update new version, and this can make the code of new version call the code of old version.
But even if I set validate_timestamps=0, this can also happen. When I update the code file, one of the code file that hasn't been cached is called. And thus this code file is in new version while the caller is in old version. Maybe I can load all the code files into cache before the server start?
If I find a way to make sure all the files in the project dir are loaded into cache before the Apache start to serve requests, I can update new version code first, and then clear all the OpCache. But does it have an impact on those working requests? Will they run an old logic and then be switch into a new one?
Besides, how to clear all the OpCache in an atom time? If it takes time to clear all the OpCache, the problem is still there, right?
Can anyone help me? Thank you very much!
Let me show an example.
I have two files on my directory, and the Apache's working directory is also there.
The entry file is index.php.
<?php
echo "verson 1: index";
// do some work
sleep(1);
// not all APIs will call this library
// I just use rand to represent a probability
if (rand(0,100) == 1) {
require_once BASEPATH.'lib.php';
t = new Tester();
t->func1();
}
And the other php file is lib.php.
<?php
echo "---- version 1: lib";
class Tester {
function __construct() {
echo "---- new Tester in lib.php";
}
public function func1() {
echo "---- Tester.func1()";
}
}
Now I'm going to upload my new version code.
In the new version, I declare a new function func2() in Tester and delete the old function func1(). I call it in the index.php.
index.php
<?php
echo "version 2: index";
// do some work
sleep(1);
// not all APIs will call this library
// I just use rand to represent a probability
if (rand(0,100) == 1) {
require_once BASEPATH.'lib.php';
t = new Tester();
t->func2();
}
lib.php
<?php
echo "---- version 2: lib";
class Tester {
function __construct() {
echo "---- new Tester in lib.php";
}
public function func2() {
echo "---- Tester.func2()";
}
}
I close the OpCache. I upload the index.php first and then the other one lib.php. At the moment between I finish to upload the index.php and begin to upload lib.php, a request is running into the "rand case". Then I will get an error about "func2() not defined", because the lib.php is still the old one at that time.
If I open the OpCache, it's the same if I don't set validate_timestamps to 0.
After I set validate_timestamps to 0, the opcode will not expire. So I must call opcache_reset manually after I finish to upload index.php and lib.php. At that moment, a request also runs into the "rand case". If the opcache_reset operation takes a period of time, and it doesn't block the request running, the error "func2() not defined" may also occur.
How to make all the requests safe when I upgrade my code version?
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 am making a php/c# console app/c# soap service and I have created many functions within the web service, but I am unable to call one of them.
The function I am calling is a function which gets a string value from the database. The call works fine on Windows (using localhost) but when putting it onto a Linux server running under mono I get an exception stating the following:
Function ("getLastResetTime") is not a valid method for this service
The strange this is though, from the Linux server I can access the test form by going to asmx file and run the getLastResetTime function and it returns what is expected, it just seems to be the PHP that can't make the call.
Below is the code that I am using in PHP to call the script
function getLastResetTime()
{
include ("../../config.php");
include ("../../includes/get-settings.php");
include ("../../includes/general.php");
try
{
$client = new SoapClient("http://192.168.1.74/EmailServer/EmailSoapService/EmailSoapService.asmx?WSDL", array("trace" => 1, "exception" => 0));
$result = $client->__soapCall("getLastResetTime", array());
echo "Last Reset: " . $result->getLastResetTimeResult;
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
The screenshot below proves that the web method is working under Mono and what it returns
Below is the code of the web service function
[WebMethod(Description = "Gets the time the Email Server last reset")]
public string getLastResetTime()
{
SoapHandler soapHandler = new SoapHandler();
return soapHandler.getLastResetTime();
}
and below is the code that the web service calls
public string getLastResetTime()
{
try
{
using (ConnectDb db = new ConnectDb(appSettings))
{
string query = "SELECT * FROM settings";
using (MySqlCommand cmd = new MySqlCommand(query, db.conn))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
if (reader.GetString("setting") == "app_lastRestart")
{
return reader.GetString("value");
}
}
}
}
}
return "N/A";
}
catch (MySqlException ex)
{
return ex.Message;
}
}
I don't understand why this isn't working, I'm guessing I've probably missing something really simple but can't find it.
Thanks for any help you can provide.
I've just found out the reason, but I don't understand why this doesn't affect the methods.
Basically, I was setting the WSDL to be the IP Address of the server (even though it was local to the server) so I was putting http://192.168.1.74/MySoapService.asmx but if I change it to http://localhost/MySoapService.asmx it works fine.
Why would I need to make it localhost instead of IP address for these services to work.
Is it because I am running several soap requests one after the other so the browser is stopping it, I did notice while I was trying to work out the problem, that Chrome was stating that it was throttling, if this is the issue then I would have thought the connection would time out, not say that the service is invalid.
UPDATE
I've now found out why changing the url from the IP to localhost fixed the issue, I realised as another function started having the same problem and later realised that php by default caches the wsdl, if this option is disabled in the php.ini file and restart the apache server, it then works fine. Probably quite useful in a production environment but a bit of a pain when development.