I have a PHP program that stores the HTML/JavaScript contents of a webpage in a mySQL database. The contents are obtained using cURL, and are then subjected to $mysqli->real_escape_string()
before being stored in the table as a longtext.
我有一个PHP程序,它将网页的HTML / JavaScript内容存储在mySQL数据库中。内容是使用cURL获得的,然后在作为longtext存储在表中之前经过$ mysqli-> real_escape_string()。
Later, I have to once again use cURL to get the HTML/JavaScript contents of the webpage and retrieve those which I stored in the database.
稍后,我必须再次使用cURL来获取网页的HTML / JavaScript内容并检索我存储在数据库中的内容。
At this point, I need to compare them to see whether changes have been made or not in the code. I've tried using:
此时,我需要比较它们以查看代码中是否进行了更改。我尝试过使用:
if ($saved == $content)
return true;
else
return false;
however this always returns false, even when no changes have been made to the code. Upon using cURL the second time, I am not escaping the string, so that isn't the issue. I've compared the two pieces of code, but I can't visually discern any differences. How can I compare the two strings so that it will accurately return whether any change has been made or not? I should also mention that both files execute just fine, except that the second one always returns false.
但是,即使没有对代码进行任何更改,它也始终返回false。在第二次使用cURL时,我没有转义字符串,所以这不是问题。我比较了两段代码,但我无法直观地辨别出任何差异。如何比较两个字符串,以便准确返回是否进行了任何更改?我还应该提到两个文件都执行得很好,除了第二个文件总是返回false。
First PHP file:
第一个PHP文件:
$url = "www.example.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = $mysqli->real_escape_string(curl_exec($ch));
curl_close($ch);
$query = "INSERT INTO saved (url, content)
VALUES ('$url', '$content')";
$mysqli->query($query)
Second PHP file:
第二个PHP文件:
$url = "www.google.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
$query = "SELECT content
FROM saved
WHERE url = '$url'";
$result = $mysqli->query($query);
$data = $result->fetch_assoc();
$saved = $data['content'];
if ($saved == $content)
echo '1';
else
echo '0';
2 个解决方案
#1
3
md5 the contents and save it. Now just check the fingerprints
md5内容并保存。现在只需检查指纹
if($saved['fingerprint'] == $content['fingerprint'])
#2
2
If you just need to compare content, my suggestion would be to create a checksum of the data (using md5 or similar) and compare the data based on the checksum.
如果您只需要比较内容,我的建议是创建数据校验和(使用md5或类似)并根据校验和比较数据。
#1
3
md5 the contents and save it. Now just check the fingerprints
md5内容并保存。现在只需检查指纹
if($saved['fingerprint'] == $content['fingerprint'])
#2
2
If you just need to compare content, my suggestion would be to create a checksum of the data (using md5 or similar) and compare the data based on the checksum.
如果您只需要比较内容,我的建议是创建数据校验和(使用md5或类似)并根据校验和比较数据。