4

For quite a while now we experience a very weird problem with our hosting server. Once a while (seems randomly) variables in PHP become NULLs.

In general everything works perfectly fine, but once a while it happens. All accounts on the server are affected and all PHP apps (including PHPMyAdmin, Wordpress our own scripts). We contacted our hosting company, but they are unable to find any solution.

I had few ideas, the most promising one was an issue with Suhosin. But I do not get any message in the log directly from it.

We made a simplest possible script to reproduce the error:

<?php
class Example
{

    protected $stringVar = 'this is a string value';

    public function accessParameter()
    {
        $error = false;
        if (isset($this->stringVar) && !is_null($this->stringVar)) {
            echo "string var : " . $this->toStringWithType($this->stringVar) . "\n";
        } else {
            echo "string var is not set\n";
            $error = true;
        }

        if ($error) {
            $logfile = dirname(__FILE__)."/random_bug_log.log";
            file_put_contents($logfile, date('Y-m-d H:i:s')."\n", FILE_APPEND);
            file_put_contents($logfile, $this->toStringWithType($this->stringVar) . "\n", FILE_APPEND);
        }
    }

    public function toStringWithType($var)
    {
        $type = gettype($var);
        return "($type) '$var'";
    }

}

$e = new Example();
$e->accessParameter();

Normal output:

string var : (string) 'this is a string value' 

Output when the weird thing happens:

string var is not set

I open to any ideas or suggestions how to solve this problem. I guess the ultimate solution is to change the hosting company. I did not manage to create this issue on localhost or any other server.


Test piece that have been made, including your suggestions:

<?php
class Example
{
  protected $stringVar = 'this is a string value';

  public function accessParameter() {
    $error = false;
    if(isset($this->stringVar) && !is_null($this->stringVar)) {
      echo "string var : "
    .$this->toStringWithType($this->stringVar)
    ."\n";
    } else {
      echo "string var is not set\n";
      $error = true;
    }

    if($error) {
      $logfile = dirname(__FILE__)."/random_bug_log.log";
      file_put_contents($logfile, date('Y-m-d H:i:s')."   ", FILE_APPEND);
      file_put_contents($logfile, 
            $this->toStringWithType($this->stringVar) . "\n",
            FILE_APPEND);
    }

  }

  public function writeParameter() {
    $this->stringVar="variable assigned";
    if(isset($this->stringVar) && !is_null($this->stringVar)) {
      echo "string var : "
    .$this->toStringWithType($this->stringVar)
    ."\n";
    } else {
      echo "string var is not set\n";
      $error = true;
    }
  }

  public function toStringWithType($var)
  {
    $type = gettype($var);
    return "($type) '$var'";
  }


}

$e = new Example();
$e->accessParameter();
$e->writeParameter();

The output while the thing happens:

string var is not set
string var is not set
4

3 回答 3

0

如果出现这些类型的问题,请检查您在 if 条件中的值并在 else 中执行您想要的操作。就像你的情况一样:

 if(isset($this->stringVar) && ($this->stringVar == "this is a string value")) {

 }else{
  // your code here...
 }
于 2014-03-26T10:55:56.550 回答
0

这是一个很奇怪的问题。

这可能不是一个解决方案,但值得一试;

protected $stringVar;

function __construct() {
    $this->stringVar = 'this is a string value';
}
于 2013-05-15T11:53:02.070 回答
0

我建议使用 !== 而不是 is_null 来查看变量是否实际上为空。

if (isset($this->stringVar) && ($this->stringVar !== null)) {

或者

if (isset($this->stringVar) && (!empty($this->stringVar)) {

也应该做这项工作。

于 2013-05-16T00:51:42.390 回答