gpt4 book ai didi

google-maps-api-3 - Google PageSpeed 建议我指定一个缓存验证器,但我不知道如何

转载 作者:行者123 更新时间:2023-12-03 14:52:33 24 4
gpt4 key购买 nike

我在这个页面上运行了谷歌的 PageSpeed 工具:
http://classifieds.your-adrenaline-fix.com/detail.php?fatherID=10&ListingID=7&TypeID=42

结果表明我应该指定一个缓存验证器 - Last-Modified 或 ETag header 。

我已经彻底阅读了这个:
https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching

但我不知道该怎么办。

我确实知道我遇到的问题(通过 Google 的 PageSpeed 工具出现)源于以下显示 Google map 的代码,但此代码是这里有人帮助我的代码。 (尽管我还没有学到任何关于 PDO 的知识,也不太了解代码)

我希望有人可以查看下面提供的代码,让我知道所有看起来都是正确的,并与我分享如何指定缓存验证器以根据 Google 的 Page Speed 建议改进页面。

任何帮助都将不胜感激,我提前感谢大家。

斯图尔特·K

编辑:此外,我在 .htaccess 中有以下代码:(以下是 Map 代码)

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 year"
# Favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>









$TypeID = isset($_GET['TypeID']) ? $_GET['TypeID'] : '';
$ListingID = isset($_GET['ListingID']) ? $_GET['ListingID'] : '';
$allowed_tables = array('tt_42', 'tt_43');//Array of allowed tables to sanatise query
//Define table name
$table ="tt_".$TypeID;
//Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$db", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
//Check if table name is in allowed list
if (in_array($table, $allowed_tables)) {
//Prepare query
$query = "SELECT * FROM `$table` WHERE `ID` = ? AND `ExpireDate` > NOW()";
}//end if
// Prepare statement
$stmt = $dbh->prepare($query);
// Assign parameter
$stmt->bindParam(1,$ListingID);
//Execute query
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();

//New code
$Address = $listing['Address'];
$City = $listing['City'];
$State = $listing['State/Province'];
$Zip = $listing['Zip/Postal'];
$Country = $listing['Country'];
//End new code

echo '<h2>Map of Surrounding Area With Navigational Aides</h2>';
echo '<div class="CalloutBoxBlue">Hover over map and scroll, or use + and - in LH corner of map to zoom or expand viewing area. Alternatively, hold down mouse to manually move the map to a different geographical location, or drag the person icon to a specific location on the map for a street view.</div>';
echo '<div id="GoogleMap">';
//New code
echo '<iframe scrolling="no" style="width:480px; height:300px; border:0px;" frameborder="0" src="googlemap.php?Address='.$Address.'&amp;City='.$City.'&amp;State='.$State.'&amp;Zip='.$Zip.'&amp;Country='.$Country.'"></iframe>';
//End new code
echo '</div>';
}//End try


catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", mapSelect.php, ". $e->getMessage()."\r\n", FILE_APPEND);
}


}

最佳答案

您可以使用 Cache-Control 指定缓存验证器标题。确保在回显或打印任何内容之前设置它。

header('Cache-Control: <amount of seconds e.g. 3600 or 86400, "no-cache" for none>');

我还对您的 PDO 代码进行了一些更改。我移动了 if 语句来检查表是否被允许并添加了一些代码来实际检索所选数据。
header('Cache-Control: 900');   // 15 minutes

$TypeID = isset($_GET['TypeID']) ? $_GET['TypeID'] : '';
$ListingID = isset($_GET['ListingID']) ? $_GET['ListingID'] : '';
$allowed_tables = array('tt_42', 'tt_43');//Array of allowed tables to sanitise query
// Define table name
$table = "tt_" . $TypeID;
if (in_array($table, $allowed_tables)) {
try {
// Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$db", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Prepare query
$query = "SELECT * FROM $table WHERE ID=? AND ExpireDate>NOW()";
// Prepare statement
$stmt = $dbh->prepare($query);
// Assign parameter
$stmt->bindParam(1, $ListingID);
// Execute query
$stmt->execute();

// Fetch all results
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (!empty($data)) {
$listing = reset($data);

// New code
$Address = $listing['Address'];
$City = $listing['City'];
$State = $listing['State/Province'];
$Zip = $listing['Zip/Postal'];
$Country = $listing['Country'];
// End new code

echo '<h2>Map of Surrounding Area With Navigational Aides</h2>';
echo '<div class="CalloutBoxBlue">Hover over map and scroll, or use + and - in LH corner of map to zoom or expand viewing area. Alternatively, hold down mouse to manually move the map to a different geographical location, or drag the person icon to a specific location on the map for a street view.</div>';
echo '<div id="GoogleMap">';
// New code
echo '<iframe scrolling="no" style="width: 480px; height: 300px; border: 0px;" frameborder="0" src="googlemap.php?Address=' . $Address . '&amp;City=' . $City . '&amp;State=' . $State . '&amp;Zip=' . $Zip . '&amp;Country=' . $Country . '"></iframe>';
// End new code
echo '</div>';
} else {
// Show "no addresses found" message
}
} catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that." . $e->getMessage(); // Remove or modify after testing
file_put_contents('PDOErrors.txt', date('[Y-m-d H:i:s]') . ", mapSelect.php, " . $e->getMessage(). "\r\n", FILE_APPEND);
}
}

关于google-maps-api-3 - Google PageSpeed 建议我指定一个缓存验证器,但我不知道如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com