与本地主机相比,PHP数组初始化在Live网站上不起作用


PHP Array Initialization Not Working On Live Site, Compared to Local Host

  • 问题是当FTP推送时,来自本地主机的结果与实时站点不匹配。这个下面的代码似乎让我相信数组没有被创建正确安装在服务器上。此外,它正在做一些时髦的事情,如下所示。第97行的错误为"无法将标量值用作数组",或$_SESSION["count"][$i]+=1。你明白原因了吗?还是我犯了另一个错误?

  • 总结一下代码,以下会话数组构成了购物车的核心:$_session['item_id'][$i],$_session['size'][$i],$_SESSION['price'][$i],$_SESSION['count'][$i]。。。并且它们都相互关联。因此,检查组合是否存在,如果存在,则附加计数,如果不存在,则首次添加项目组合。

本地主机

In Stock: 3
Distinct Item Count: 3
Array ( [0] => 1 [1] => 2 [2] => 2 ) Item Array: 1
Array ( [0] => S [1] => M [2] => L ) Size Array: 1
Array ( [0] => 15 [1] => 20 [2] => 20 ) Price Array: 1
Array ( [0] => 15 [1] => 35 [2] => 17 ) Count Array: 1
Quantity Increased to 18 
1 
S 
15
15

2 
M 
20
35

2 
L 
20 
18 
|3 ($1285) 

Live网站-首次添加

In Stock: 5
0

1
M
20
1
|1 ($20)

Live网站-第二次添加

In Stock: 5
Distinct Item Count: 1
1Item Array: 1
MSize Array: 1
300Price Array: 1
1Count Array: 1

Warning: Cannot use a scalar value as an array in /public_html/shop/helper/addtocart.php on line 97
Quantity Increased To
1
M
2

|1 ($0)  

代码

    if (isset($_SESSION["item_id"]) && is_array($_SESSION["item_id"])){
        echo "Distinct Item Count: " . count($_SESSION["item_id"]);
        echo "<br/>";
        echo "<br/>";
        echo "Item Array: ";
        print_r($_SESSION["item_id"]);
        echo "<br/>";
        echo "Size Array: ";
        print_r($_SESSION["size"]);
        echo "<br/>";
        echo "Price Array: ";
        print_r($_SESSION["price"]);
        echo "<br/>";
        echo "Count Array: ";
        print_r($_SESSION["count"]);
        echo "<br/>";
        echo "<br/>";
    }
    //check for current product combination in visitor's shopping cart content
    //find count in cart
    if (!isset($_SESSION["item_id"])){
      $count = 0;
      }else{
        $count = count($_SESSION["item_id"]);
      }
    echo $count;
    echo "<br/>";
    if ($count == 0){
        //declare arrays
        $_SESSION["item_id"] = array();
        $_SESSION["size"] = array();
        $_SESSION["price"] = array();
        $_SESSION["count"] = array();
        $_SESSION["total"] = array();
        //add first item to cart
        $_SESSION["item_id"][] = $item_id;
        $_SESSION["size"][] = $size;
        $_SESSION["price"][] = $price;
          //fundraiser and corporate
          if (($_SESSION["acctype"] == 2 || $_SESSION["acctype"] == 3) && isset($_SESSION['userid'])){
              //update count, add 100
              $_SESSION["count"][] = 100;
              }
          else{
              $_SESSION["count"][] = 1;
          }
    }else{
        $flag=0;
        $i=0;
        while ($i <= $count){
           if( ($_SESSION["item_id"][$i] == $item_id) && ($_SESSION["size"][$i] == $size) ){
           //fundraiser and corporate
              if(!isset($_SESSION["acctype"]) || $_SESSION["acctype"] == 1){
                 //update by one
                  $_SESSION["count"][$i] +=1;
                }
              elseif(isset($_SESSION['userid']) && ($_SESSION["acctype"] == 2 || $_SESSION["acctype"] == 3)){
                    //update count, add 100
                    $_SESSION["count"][$i] +=100;
                }
              else{
                  echo "Hmm";
                }
              //update cart stats
              echo notify('Quantity Increased To' . ' ' . $_SESSION["count"][$i]);
              //was there combination match?
              $flag = 1;
              }
        $i++;
        }
        if($flag == 0){
                  $_SESSION["item_id"][] = $item_id;
                  $_SESSION["size"][] = $size;
                  $_SESSION["price"][] = $price;
                    //fundraiser and corporate
                    if (($_SESSION["acctype"] == 2 || $_SESSION["acctype"] == 3) && isset($_SESSION['userid'])){
                        //update count, add 100
                        $_SESSION["count"][] = 100;
                    }else{
                    $_SESSION["count"][] = 1;
                    }
         }
    }

简短回答:检查是否使用var_dump(ini_get('register_globals'))phpinfo()在实时服务器上启用了register_globals。如果启用了register_globals,最好的解决方案是禁用register-globals——这会导致安全问题,并且从PHP 5.3开始就不推荐使用。

更长的答案:"不能将标量值用作数组"表示标量值(例如数字)存在于作为数组引用的变量中。我没有看到任何地方可以将标量直接分配给$_SESSION["count"]

然而,如果register_globals被启用,$count将成为对$_SESSION["count"]的引用,这意味着$count = 0将把$_SESSION["count"]设置为标量。同样,最好的解决方案是禁用register_globals,但解决方法是将$count变量重命名为其他变量。

我没有查看您的代码,但我遇到了类似的问题,这是我的FTP客户端,在传输过程中,它只是更改了一些文本,导致了如上所示的错误。

我停止使用FileZilla,开始使用CyberDuck,一切都很好,检查服务器上的代码,并验证它是否已经更改。