与本地主机相比,PHP 数组初始化在实时站点上不起作用
问题是通过 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)
直播站点 - 首先添加
In Stock: 5
0
1
M
20
1
|1 ($20)
直播站点 - 第二次添加
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;
}
}
}
The issue is the results from local host are not matching up with the live site when pushed by FTP. The
below code seems make me believe the array is not be created
properly on the server. In addition, it is doing some funky stuff, as seen below. The error is "Cannot use a scalar value as an array" on line 97, or $_SESSION["count"][$i] +=1. Do you see the reason why? Or is there a different mistake I made?To summarize the code, the following session arrays make up the core of the shopping cart: $_SESSION['item_id'][$i], $_SESSION['size'][$i],
$_SESSION['price'][$i], $_SESSION['count'][$i] ... and they all correlate to one another. Therefore a check is done to see if the combination exists, if so it appends the count, if not, adds the item combination for the first time.
Local Host
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 Site - First Add
In Stock: 5
0
1
M
20
1
|1 ($20)
Live Site - Second Add
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)
Code
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短回答:检查register_globals是否已启用在您的实时服务器上使用
var_dump(ini_get('register_globals'))
或phpinfo()
。如果启用了 register_globals,最好的解决方案是禁用注册全局变量——这会导致安全问题,并且从 PHP 5.3 开始已被弃用。更长的答案:“不能将标量值用作数组”意味着标量值(例如数字)存在于您作为数组引用的变量中。我没有看到您在任何地方直接将标量分配给
$_SESSION["count"]
。但是,如果 register_globals 启用
$count
将变为对$_SESSION["count"]
的引用,这意味着$count = 0
会将$_SESSION["count"]
设置为标量。同样,更好的解决方案是禁用 register_globals,但解决方法是将 $count 变量重命名为其他名称。Short answer: Check to see if register_globals is enabled on your live server using
var_dump(ini_get('register_globals'))
orphpinfo()
. If register_globals is enabled, the preferable solution is to disable register globals—it leads to security problems and as of PHP 5.3 is deprecated.Longer answer: 'Cannot use a scalar value as an array' means that a scalar value (e.g. number) exists in the variable that you are referencing as an array. I do not see anywhere that you assign a scalar directly to
$_SESSION["count"]
.However, if register_globals is enabled
$count
would become a reference to$_SESSION["count"]
, which means$count = 0
would be setting$_SESSION["count"]
to a scalar. Again, the preferable solution would be to disable register_globals, but a workaround would be to rename the$count
variable to something else.我没有看你的代码,但我有一个类似的问题,它是我的 FTP 客户端,不知怎的,在传输过程中它只改变了一些文本,导致了像你上面所示的错误。
我停止使用 FileZilla 并开始使用 CyberDuck,一切都很好,检查服务器上的代码并验证它是否已更改。
I didn't look at your code but I had a similar issue and it was my FTP client, somehow along the transfer it was changing only some of the text causing errors like you have shown above.
I stopped using FileZilla and started using CyberDuck and everything was good, inspect the code on your server and verify if it has been changed at all.