GVKun编程网logo

php – file_put_contents()搞乱更新的数组值(php修改数组内容)

12

如果您对php–file_put_contents()搞乱更新的数组值和php修改数组内容感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解php–file_put_contents()搞乱更新的

如果您对php – file_put_contents()搞乱更新的数组值php修改数组内容感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解php – file_put_contents()搞乱更新的数组值的各种细节,并对php修改数组内容进行深入的分析,此外还有关于file_get_contents("php://input", "r")实例介绍_php技巧、file_put_contents 和 php://input 实现存储数据进图片中、file_put_contents以及file_get_contents、file_put_contents执行返回false,file_put_contents false(linux服务器httpd)的实用技巧。

本文目录一览:

php – file_put_contents()搞乱更新的数组值(php修改数组内容)

php – file_put_contents()搞乱更新的数组值(php修改数组内容)

如果标题有点令人困惑,请道歉.

我的结果指出了发生了什么.

这个问题变得非常广泛,所以我在这里强调这两件事:

有可能的原因,请参阅我在底部的更新.

整个档案:(见第86行)http://codepad.org/oIXaZZaB

这是发生了什么:

我有这个数组,每个语言代码包含计数整数.

$downloads = [
    'all' => [
        'nl-BE' => 0,
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ],
    'unique' => [
        'nl-BE' => 0,
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ]
];

从$_GET [‘langCode’]参数中检索到的密钥总是计入$downloads [‘all’]中的相应密钥.

我还检查访问者是否是唯一的,并且之前已经使用过相同的$langCode.如果他之前没有使用相同的$langCode,则计数也会添加到$downloads [‘unique’]中的相应键中.

这是通过以下代码完成的:

$uniqueVisitors = [
    '127.0.0.1' => [ 'en-UK' ]
];


if ($uniqueVisitors == null)
{
    $uniqueVisitors = [
        $ipNew => []
    ];
}



$countUnique = 1;


/*  Check Unique Visitors  */

if (isset($uniqueVisitors[$ipNew]))
{
    if (in_array($langCode, $uniqueVisitors[$ipNew]))
    {
        $countUnique = 0;
    }

    else $uniqueVisitors[$ipNew][] = $langCode;
}

else $uniqueVisitors[$ipNew] = [ $langCode ];



/*  Update Data  */

$downloads['all'][$langCode]++;

if ($countUnique)
{
    $downloads['unique'][$langCode]++;
}


/*  Save Data  */

file_put_contents('data/unique-visitors.json', json_encode($uniqueVisitors));
file_put_contents('data/downloads.json', json_encode($downloads));

现在奇怪的是,当我运行脚本时,有时会计算多个键,即使$langCode只包含一个键(例如’nl-NL’)

使用’en-UK’时,’en’也经常被计算在内.
“fr-FR”和“fr-BE”也是如此.
‘nl-NL’和’nl-BE’.

它主要发生在使用的langCode计数仍为0时
但它似乎也是以随机顺序发生的.

现在您可能认为这是由于langCodes用作键,但我也使用了零索引键,结果相同!

例如:

/*  Retrieved first from JSON file  */

$downloads = [
    'all' => [
        'nl-BE' => 0,
        'nl-NL' => 2,
        'fr-BE' => 1,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ],
    'unique' => [
        'nl-BE' => 0,
        'nl-NL' => 1,
        'fr-BE' => 1,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ]
];

$uniqueVisitors = [
    '127.0.0.1' => [ 'nl-NL', 'fr-BE' ]
];


/*  Result after running script with 'en-UK' langCode  */

$downloads = [
    'all' => [
        'nl-BE' => 0,
        'nl-NL' => 2,
        'fr-BE' => 1,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 1,
        'en'    => 1   // Why is this one counted ?
    ],
    'unique' => [
        'nl-BE' => 0,
        'nl-NL' => 1,
        'fr-BE' => 1,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 1,
        'en'    => 1   // Why is this one counted ?
    ]
];

$uniqueVisitors = [
    '127.0.0.1' => [ 'nl-NL', 'fr-BE', 'en', 'en-UK' ]  //  'en' stored as well ?
];

整个文件:(见第86行)http://codepad.org/oIXaZZaB

更新:

当我没有使用两个file_put_contents()函数将更新的数据保存回他们的文件,只是做一些测试和转储时,整个计数确实按预期工作!

因此,出现这两个file_put_contents()函数,用于保存(正确)更新的数据,以某种方式干扰,并在执行时更改一些计数.

但是如何?!

有人对这种意想不到的行为有了更好的理解吗?

更新2:

当我在我的file_put_contents()函数中省略json_encode()时,我得到“Array to String conversion”错误,两次用于保存我的计数数据($downloads):

Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.PHP on line 89

Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.PHP on line 90

Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.PHP on line 90
file_put_contents('data/unique-visitors.json', json_encode($uniqueVisitors));
file_put_contents('data/downloads.json', json_encode($downloads));  // This one is called twice!

那么为什么那个被执行两次?那里甚至没有一个循环.

更新3:

我的JSON编码$downloads数据正确显示更新的值.

但是一旦我使用file_put_contents()来存储更新的$downloads数据,它就会与更新的值混淆.即使我在使用file_put_contents之前/之后转储了更新的数据,它也显示出混乱.

初始$downloads数组:

$downloads = [
    'all' => [
        'nl-BE' => 0,
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ],
    'unique' => [
        'nl-BE' => 0,
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ]
];

使用$_GET更新值[‘langCode’] =’nl-BE’:

$downloads = [
    'all' => [
        'nl-BE' => 1,
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ],
    'unique' => [
        'nl-BE' => 1,
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ]
];

当我尝试使用file_put_contents()存储更新的数组时:

$downloads = [
    'all' => [
        'nl-BE' => 2,  // Why this increment?
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ],
    'unique' => [
        'nl-BE' => 1,
        'nl-NL' => 0,
        'fr-BE' => 0,
        'fr-FR' => 0,
        'de-DE' => 0,
        'it-IT' => 0,
        'en-UK' => 0,
        'en'    => 0
    ]
];

奇怪的是,当我使用file_put_contents时,更新的$downloads显示错误更新,当在同一个脚本运行时转储(在file_put_contents之前和之后).我甚至不必使用file_get_contents来检索错误保存的数据.

更新4 :(原因)

Safari 7出现了这个错误.

Firefox和Chrome都完美地运行此脚本.

我在哪里可以举报?

htaccess的:

没有htaccess文件仍然会发生同样的错误.

<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^(.+)/?$index.PHP?lang=$1 [QSA,NC,L]


</IfModule>

解决方法:

我也无法重现你的错误.在我看来,你的脚本被调用两次(参见’all’中的项目增加但不是’unique’.

file_put_contents()没有问题.您可以通过在脚本中添加以下内容来验证这一点:

<?PHP

mail('you@domain.tld', 'script executed at '.time(), '');

// your code ...

我建议复制你的脚本,删除所有的HTML代码并在带有虚拟数据的终端中执行它:

PHP yourscript.PHP

这是我的代码:http://codepad.org/XhS6uEz1

你会发现它的行为正确.

另外你可以禁用一些php module that could be responsible for such bugs,但我认为这不是问题.

file_get_contents(

file_get_contents("php://input", "r")实例介绍_php技巧

解释不清,直接上例子
index.html
复制代码 代码如下:

 

 

 

 
 


action.php
复制代码 代码如下:

$raw_post_data = file_get_contents(''php://input'', ''r'');
echo "-------\$_POST------------------
";
echo var_dump($_POST) . "
";
echo "-------php://input-------------
";
echo $raw_post_data . "
";
?>

 

file_put_contents 和 php://input 实现存储数据进图片中

file_put_contents 和 php://input 实现存储数据进图片中

<?php

/**
 *Recieve p_w_picpath data
 **/
error_reporting(E_ALL);

function get_contents()
{
    $xmlstr = file_get_contents("php://input");
    $filename = time() . ''.png'';
    if (file_put_contents($filename, $xmlstr)) {//将数据存储进图片格式中
        echo ''success'';
    } else {
        echo ''failed'';
    }
}

//get_contents();

var_dump(file_get_contents(''1565341389.png''));  //读取图片,获取图片中的数据

die();

  

 

本文同步分享在 博客 “lxw1844912514”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。

file_put_contents以及file_get_contents

file_put_contents以及file_get_contents

对数据的操作最基本的是增删改查,file_put_contents以及file_get_contents是对文件里的数据进行存入与取出。

先上代码:

?

<?php

 

$str = ''hello world'';

if(file_put_contents(''01.txt'',$str)){

    echo ''数据存入成功'',''<br />'';

}else{

    echo ''数据存入失败'',''<br />'';

}

//返回的是:数据存入成功

//原来相应的目录下是没有这个文件的

//现在有这个文件了

//从这里可以看出,如果没有这个文件的话

//调用file_put_contents方法会自动创建这样的一个文件

//然后把数据存入

echo file_get_contents(''01.txt''),''<br />'';

//返回hello world

//把这个文件里的数值读出来

//如果file_get_contents是要读一个不存在的文件

//那么会报错,要读的文件一定要存在的

if(file_put_contents(''01.txt'',''new data to be insert'')){

    echo ''数据存入成功2'',''<br />'';

}else{

    echo ''数据存入失败2'',''<br />'';

}

 

echo file_get_contents(''01.txt''),''<br />'';

//返回new data to be insert

//说明用file_put_contents方法只能对数据进行替换

//而不能在原来的基础上进行添加

 ?>

在用户登录或者查询数据等时候有些时候可能会有引号等对sql语句有影响的符号(sql注入攻击),这样等对他们进行数据库里的操作的时候会有影响,

这种情况应该怎么办呢?

:可以用addslashes 和 stripslashes

addslashes是可以使单引号(,),双引号("),反斜线(\)与NULL(NULL字符)加上反斜线进行转义

stripslashes是与addslashes相对的一个方法,是把这些转义过的,还原

如下代码:

?

<?php

 

$str = ''abcdfjaslffdfa"jflsadj'';

if(file_put_contents(''01.txt'',$str)){

    echo ''数据存入成功'',''<br />'';

}else{

    echo ''数据存入失败'',''<br />'';

}

echo file_get_contents(''01.txt''),''<br />'';

//返回abcdfjaslffdfa"jflsadj

 

$str =addslashes($str);

if(file_put_contents(''01.txt'',$str)){

    echo ''数据存入成功2'',''<br />'';

}else{

    echo ''数据存入失败2'',''<br />'';

}

echo file_get_contents(''01.txt''),''<br />'';

//返回 abcdfjaslffdfa\"jflsadj

//经过转义

 

echo stripslashes(file_get_contents(''01.txt'')),''<br />'';

//返回abcdfjaslffdfa"jflsadj

//对转义字符串进行还原

 ?>

这种情况很多都是在用户进行表单输入的时候,后台处理的时候用

有些PHP版本magic_quotes_gpc这个配置是有用的,即自动魔术引号,即如果这个配置开启的话,$_POST ,$_COOKIE,$_SESSION这些值会自动进行转义,就不需要我们来转义

这种情况下怎么办呢?

答:为了兼容性和移植性,我们要对他进行判断,

看如下代码:

?

<?php

 

$textarea = $_POST[''textarea''];

 

if(get_magic_quotes_gpc()){

    echo ''魔术引号以开启,$textarea不需要转义'',''<br />'';

}else{

    echo ''魔术引号未开启,$textarea需要转义'',''<br />'';

    $textarea = addslashes($textarea);

}

 

 ?>

  

 

 

 

我们来看下面一个例子:

这是在网站里经常碰到的

先是一个form表单里填写数据,然后提交到php页面进行处理,然后对数据进行显示

form表单代码如下

?

<html>

    <head>

         

    </head>

    <body>

        <form action="01.php" method="post">

            <div>

                <label for="name">Text Input:</label>

                <input type="text" name="name" id="name" value="" tabindex="1" />

            </div>

 

         

 

             

            <div>

                <label for="textarea">Textarea:</label>

                <textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>

            </div>

             

 

         

            <div>

                <input type="submit" value="Submit" />

            </div>

        </form>

    </body>

</html>

php处理页面,即01.php代码如下:

?

<?php

 

file_put_contents(''01.txt'', $_POST[''textarea'']);

echo file_get_contents(''01.txt''),''<br />'';

 

 

 

 ?>

  如果在form表单的textarea控件里面输入

?

<script type="text/javascript">

        while (true) {

            alert(''a'');

        };

    </script>

  就会无限弹出a,,而更有甚至利用这个漏洞,执行一些操作,如document.cookie等等

这种行为叫做XSS攻击

什么叫做XSS攻击呢?这种情况又该怎么办呢?

答:XSS攻击:跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆。

我们可以用htmlspecialchars方法,即html实体转义,即<>等这些html符号,都会被转化,那么当打印出来的时候,只会当他们是文字,而不是脚本了

?

<?php

 

file_put_contents(''01.txt'', htmlspecialchars($_POST[''textarea'']));

echo file_get_contents(''01.txt''),''<br />'';

//返回<script type="text/javascript"> while (true) { alert(''a''); }; </script>

 

 

 ?>

  与htmlspecialchars方法相对的是htmlspecialchars_decode

file_put_contents执行返回false,file_put_contents false(linux服务器httpd)

file_put_contents执行返回false,file_put_contents false(linux服务器httpd)

file_put_contents执行返回false,file_put_contents false(linux服务器httpd)

 

默认下selinux是开启的
查看SELinux状态:
1、/usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态
SELinux status:                 enabled
2、getenforce                 ##也可以用这个命令检查
关闭SELinux:
1、临时关闭(不用重启机器):
setenforce 0                  ##设置SELinux 成为permissive模式
                              ##setenforce 1 设置SELinux 成为enforcing模式
2、修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可

另外,如果以上还是不能解决的话要设置一下目录的权限策略
chcon -R -t httpd_sys_content_t /home/html

 

关于php – file_put_contents()搞乱更新的数组值php修改数组内容的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于file_get_contents("php://input", "r")实例介绍_php技巧、file_put_contents 和 php://input 实现存储数据进图片中、file_put_contents以及file_get_contents、file_put_contents执行返回false,file_put_contents false(linux服务器httpd)的相关信息,请在本站寻找。

本文标签:

上一篇php – JOINed表上的GROUP BY和ORDER BY – 复杂而缓慢(php数据表)

下一篇php – stackOverflow如何在很短的时间内将他们的页面提交给搜索引擎?(php页面传值的方法)