Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
在日常的工作中,有時候需要將部分信息保存到本地,也就是寫入到INI文件中,便于下次使用;在Qt中可以使用QSetting來進行INI文件的讀寫。
(1).void QSettings::beginGroup(const QString & prefix)
相當于在INI文件中添加了一個索引,根據索引的不同,可以讀取相對應的值
(2).void QSettings::endGroup()
將組重置為相應的eginGroup()調用之前的狀態。
(3).void QSettings::setValue(const QString & key, const QVariant & value)
向INI文件中寫入信息,信息以鍵值對的方式寫入到INI文件中,如果key值已經存在了,那么key對應的value值便會被新值覆蓋掉。
(4).QVariant QSettings::value(const QString & key, const QVariant & defaultValue=QVariant()) const
從INI文件中讀取信息,返回設置鍵的值,如果設置的鍵不存在,則返回defaultValue,如果未指定默認的值,則返回默認QVariant()。
(5).QString QSettings::group() const
返回當前的組名
Qt開發必備技術棧學習路線和資料
.NET 中,有多種配置文件格式和讀取機制可用于管理應用程序的設置和配置信息。這包括JSON、XML、INI格式的配置文件,以及環境變量。下面我們將分別探討如何輕松讀取這些配置文件和環境變量。
JSON(JavaScript Object Notation)是一種輕量級的數據交換格式,易于人閱讀和編寫,同時也易于機器解析和生成。在.NET 中,可以使用 System.Text.Json 命名空間下的類來讀取 JSON 配置文件。
例如,假設你有一個名為 appsettings.json 的配置文件,內容如下:
json{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
你可以使用以下代碼讀取該配置文件:
csharpusing System;
using System.IO;
using System.Text.Json;
public class AppSettings
{
public ConnectionStrings ConnectionStrings { get; set; }
public Logging Logging { get; set; }
}
public class ConnectionStrings
{
public string DefaultConnection { get; set; }
}
public class Logging
{
public LogLevel LogLevel { get; set; }
}
public class LogLevel
{
public string Default { get; set; }
public string Microsoft { get; set; }
public string MicrosoftHostingLifetime { get; set; }
}
class Program
{
static void Main()
{
string jsonFilePath="appsettings.json";
string jsonString=File.ReadAllText(jsonFilePath);
var appSettings=JsonSerializer.Deserialize<AppSettings>(jsonString);
Console.WriteLine(appSettings.ConnectionStrings.DefaultConnection);
}
}
XML(Extensible Markup Language)是一種標記語言,可以用來創建自定義的標記來描述存儲的數據。在.NET 中,可以使用 System.Xml 命名空間下的類來讀取 XML 配置文件。
例如,假設你有一個名為 app.config 的 XML 配置文件,內容如下:
xml<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="Setting1" value="Value1" />
<add key="Setting2" value="Value2" />
</appSettings>
你可以使用以下代碼讀取該配置文件:
csharpusing System;
using System.Configuration;
using System.Xml;
class Program
{
static void Main()
{
var setting1=ConfigurationManager.AppSettings["Setting1"];
var setting2=ConfigurationManager.AppSettings["Setting2"];
Console.WriteLine(setting1);
Console.WriteLine(setting2);
}
}
請注意,XML 配置文件通常需要添加在項目中的 App.config 或 Web.config 文件中,并且需要引用 System.Configuration 程序集。
htaccess文件是運行Apache Web Server的Web服務器的配置文件,對配置和重定向Apache Web Server文件系統很有用。
請記住.htaccess文件將采用隱藏格式。沒有人可以通過URL直接看到它。
.htaccess文件有很多用途。在這里,我將討論創建它們及其用途。
打開任何文本編輯器并使用名稱保存文件.htaccess。mod_rewrite在Apache Web服務器配置中的php.ini文件中啟用擴展。
1.禁用目錄列表
如果要禁用文件夾文件列表,請包括以下代碼。
#禁用目錄列表
Options All –Indexes
2.錯誤頁面
您可以為特定錯誤設置錯誤頁面。
示例:
errorDocument 400 http://www.yourdomain.com/error.html
errorDocument 401 http://www.yourdomain.com/error.html
errorDocument 404 http://www.yourdomain.com/error.html
errorDocument 500 http://www.yourdomain.com/error.html
如果要在Apache服務器中啟用“重寫規則”,則必須編寫:RewriteEngine on
如果要關閉此規則,請將值更改為關閉。
RewriteEngine on
3.域重定向
要將yourdomain.com永久重定向到www.yourdomain.com,請添加以下代碼:
RewriteCond%{HTTP_HOST} ^ yourdomain.com
RewriteRule(。*)http://www.yourdomain.com / $ 1 [R=301,L]
4.子域重定向
您還可以執行子文件重定向映射到該文件夾。這里www.yourdomain.com正在連接到該 website_folder 文件夾。
RewriteCond%{HTTP_HOST} ^ www \ .yourdomain \ .com $
RewriteCond%{REQUEST_URI}!^ / website_folder /
RewriteRule(。*)/ website_folder / $ 1
這里subdomain.yourdomain.com正在連接到該 subdomain_folder 文件夾。
RewriteCond%{HTTP_HOST} ^ subdomain \ .yourdomain \ .com $
RewriteCond%{REQUEST_URI}!^ / subdomain_folder /
RewriteRule(。*)/ subdomain_folder / $ 1
5.舊域重定向
用于將舊域(old.com)重定向到新域(new.com)的.htaccess代碼。
RewriteCond%{HTTP_HOST} ^ old.com
RewriteRule(。*)http://www.new.com/ [R=301,L]
RewriteCond%{HTTP_HOST} ^ www \ .old \ .com
RewriteRule(。*)http://www.new.com/ [R=301,L]
6.友好的URL
友好和信息豐富的URL有助于搜索引擎排名。URL是搜索引擎優化過程中非常重要的一部分。
配置文件參數[ a-zA-Z0-9_- ]會打開此輸入(有關更多幫助,請閱讀正則表達式):http://gurutechnolabs.com/profile.php?username=test成http:// gurutechnolabs.com/test
RewriteRule ^([a-zA-Z0-9 _-] +)$ profile.php?username=$ 1
RewriteRule ^([a-zA-Z0-9 _-] +)/ $ profile.php?username=$ 1
http://gurutechnolabs.com /friends.php?username=test到http://gurutechnolabs.com/friends/test
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)$ friends.php?username=$ 1
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/ $ friends.php?username=$ 1
這里第一個參數允許[a-zA-Z0-9_-],第二個參數只允許數字[ 0-9 ]
http://gurutechnolabs.com/friends.php?username=test&page=2至http://gurutechnolabs.com/friends/test/2
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/([0-9] +)$ friends.php?username=$ 1&page=$ 2
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/([0-9] +)/ $ friends.php?username=$ 1&page=$ 2
7.隱藏文件擴展名
假設您不想顯示網頁擴展名。
示例:
http://www.yourdomain.com/index.html至 http://www.yourdomain.com/index只需添加以下代碼:
RewriteRule ^([^ /。] +)/?$ $ 1.html
1.啟用壓縮:
壓縮對于減小網頁大小非常有用。
有兩種壓縮選項:
要啟用Deflate模式,請在.htaccess中添加以下代碼
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
</ifmodule>
啟用GZIP壓縮模式
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
2.添加過期標題:啟用瀏覽器緩存
Expire標頭用于緩存來自瀏覽器的數據。加速網頁是有幫助的,因為網頁可以從瀏覽器中檢索數據,因此無需從減少HTTP請求的服務器獲取數據。
<IfModule mod_expires.c>
ExpiresActive on
#如果要設置默認過期日期
ExpiresDefault“訪問加1個月”
#對于html文檔
ExpiresByType text / html“access plus 0 seconds”
#數據
ExpiresByType text / xml“access plus 0 seconds”
ExpiresByType application / xml“access plus 0 seconds”
ExpiresByType應用程序/ json“訪問加0秒”
# RSS訂閱
ExpiresByType應用程序/ rss + xml“訪問加1小時”
#Favicon(無法重命名)
ExpiresByType image / x-icon“訪問加1周”
#Media:圖像,視頻,音頻
ExpiresByType image / gif“access plus 1 month”
ExpiresByType image / png“access plus 1 month”
ExpiresByType image / jpg“access plus 1 month”
ExpiresByType image / jpeg“access plus 1 month”
ExpiresByType視頻/ ogg“訪問加1個月”
ExpiresByType音頻/ ogg“訪問加1個月”
ExpiresByType視頻/ mp4“訪問加1個月”
#CSS和JavaScript
ExpiresByType text / css“access plus 1 year”
ExpiresByType應用程序/ javascript“訪問加1年”
ExpiresByType text / javascript“access plus 1 year”
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
3.啟用Keep-Alive以減少HTTP請求
通過啟用Keep-Alive,您的Web服務器告訴Web瀏覽器不需要為它在站點上檢索的每個文件單獨請求。此外,請確保以減少HTTP請求的方式對其進行編碼。
<ifModule mod_headers.c> Header Set Connection Keep-alive <ifModule>
4.防止垃圾郵件機器人抓取您的網站
有時,您的網站的頁面加載速度可能會因主機方案中可用的帶寬而降低。有時垃圾郵件船會占用您的大部分帶寬,而您的網站也會變慢。
因此,防止垃圾郵件是有幫助的。
<ifModule mod_setenvif.c>
#將垃圾郵件發送者引薦為spambot
SetEnvifNoCase Referer spambot1.com spambot=yes
SetEnvifNoCase Referer spambot1.com spambot=yes
#添加任意數量的內容
Order allow,deny
Allow from all
Deny from env=spambot
<ifModule>
因此,我們可以將.htaccess文件用于多種用途。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。