问题描述
您好,有人可以告诉我在这里我是否有正确的想法来连接到数据库等... 我在配置和处理方面有一些帮助。我想将其全部保留在类范围内,以便我在主文件中没有连接变量,而只调用我需要的类函数。
这是我想出的。.
配置文件
command: |
"C:\Program Files\.......exe" /s /f c:\path\file.txt
处理文件
class siteDbc {
protected function cfg() {
return [
'host' => 'localhost','username' => '','password' => '','name' => '','charset' => 'utf8mb4','options' =>[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,PDO::ATTR_EMULATE_PREPARES => false
]
];
}
}
然后我将它们包含在主文件中
class siteDbh extends siteDbc {
protected function connect() {
try {
// Get the variable values used to connect and login
$dsn = "mysql:host={$this->cfg()['host']};dbname={$this->cfg()['name']};charset={$this->cfg()['charset']}";
// Attempt to connect to the SQL database using PDO(Prepared Database Object)
// Send the Database name,user and pass to the PDO class to try and connect
// Attribute "options" included
return new PDO($dsn,$this->cfg()['username'],$this->cfg()['password'],$this->cfg()['options']);
} catch (PDOException $e) {
die($e->getMessage());
}
}
}
然后是一类公共功能,用于在主文件中使用的http设置
require_once INCLUDES_BASEDIR.'_site_pdo/_sitePDO-dbc.cfg.php';
require_once INCLUDES_BASEDIR.'_site_pdo/_sitePDO-dbh.class.php';
然后在我的主文件中做这样的事情
class siteHTTP extends siteDbh {
// Select HTTP request
public function request() {
$sql = "SELECT http_request FROM settings WHERE 1";
if (!$stmt = $this->connect()->query($sql)) {
return false;
} else {
return $stmt->fetch()['http_request'];
}
}
//#
// Select Domain
public function domain() {
$sql = "SELECT site_domain FROM settings WHERE 1";
if (!$stmt = $this->connect()->query($sql)) {
return false;
} else {
return $stmt->fetch()['site_domain'];
}
}
//#
// Select Directory
public function directory() {
$sql = "SELECT site_url_dir FROM settings WHERE 1";
if (!$stmt = $this->connect()->query($sql)) {
return false;
} else {
return $stmt->fetch()['site_url_dir'];
}
}
//#
}
另一个在主文件中使用函数的示例,该函数检查真实会话。当然还有更多了。
require_once INCLUDES_BASEDIR.'_site_pdo/_sitePDO-http.class.php';
$siteHTTP = new siteHTTP;
// Get URL Data - Force HTTPS or HTTP
// SITE HTTP Array
define('SITE_HTTP',array(
'request' => $siteHTTP->request(),'domain' => $siteHTTP->domain(),'directory' => $siteHTTP->directory()
));
// Site URL - A combination of HTTP Request and Site Domain and Directory of site gives you a full URL string
define('SITE_URL',SITE_HTTP['request'].SITE_HTTP['domain'].SITE_HTTP['directory']);
//#
// Force HTTP HOST to be the actual HOST (yourdomain.com)
// HTTP HOST is a security. NOTE I only use this
// Server Global ONCE here to make sure domain.com is
// domain.com and not hackyourshit.com.
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] != SITE_HTTP['domain']) {
header('Location: '.SITE_HTTP['request'].SITE_HTTP['domain'].trim(htmlentities($_SERVER['REQUEST_URI'],ENT_QUOTES | ENT_SUBSTITUTE,'UTF-8')));
exit;
} else if (isset($_SERVER['HTTP_HOST']) == false) {
die('Access Denied!');
}
//#
// Force HTTPS - SSL TLS ON
if (SITE_HTTP['request'] === 'https://') {
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off") {
//header('HTTP/1.1 301 Moved Permanently');
header('Location: '.SITE_URL);
exit;
}
// Force HTTP - SSL TLS OFF
} else if (SITE_HTTP['request'] === 'http://') {
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
//header('HTTPS/1.1 301 Moved Permanently');
header('Location: '.SITE_URL);
exit;
}
}
//#
使用if语句作为句柄并返回false会更好,如果连接为false的话,我可以自己处理主文件中的错误?
这里完全是自学的,建议/帮助表示赞赏!! 谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)