创建配置文件(config.php)php的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29763358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Way to create configuration file(config.php) php
提问by CodeMan
I am creating a database configuration file for my project, but I am not sure if my config.php is secure.
我正在为我的项目创建一个数据库配置文件,但我不确定我的 config.php 是否安全。
How would I modify this script for a secure connection?
我将如何修改此脚本以实现安全连接?
config.php
配置文件
<?php
$username="root";
$password="";
$host="localhost";
$database="practise";
?>
Index.php
索引.php
<?php
include 'config.php';
$con=mysql_connect("$host","$username","$password") or die("Server Error");
mysql_select_db("$database") or die("Database error");
if($con==true)
{
echo "Success";
}
else
{
mysql_close($con);
}
?>
采纳答案by CodeMan
i find best way to create config.php file for my project
我找到了为我的项目创建 config.php 文件的最佳方法
index.php
索引.php
<?php
include 'config.php';
try
{
$host=$config['DB_HOST'];
$dbname=$config['DB_DATABASE'];
$conn= new PDO("mysql:host=$host;dbname=$dbname",$config['DB_USERNAME'],$config['DB_PASSWORD']);
//new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
}
catch(PDOException $e)
{
echo "Error:".$e->getMessage();
}
?>
config.php
配置文件
<?php
$config=array(
'DB_HOST'=>'localhost',
'DB_USERNAME'=>'root',
'DB_PASSWORD'=>'',
'DB_DATABASE'=>'gobinath'
);
?>
回答by Manoj Kumar
1) create a config.php
1) 创建一个 config.php
define('DBUSER','username');
define('DBPWD','password');
define('DBHOST','localhost');
define('DBNAME','database name');
2) db.php
2) db.php
<?php
include('config.php');
class db extends mysqli {
// single instance of self shared among all instances
private static $instance = null;
// db connection config vars
private $user = DBUSER;
private $pass = DBPWD;
private $dbName = DBNAME;
private $dbHost = DBHOST;
//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects.
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
private function __construct() {
parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
if (mysqli_connect_error()) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
parent::set_charset('utf-8');
}
public function dbquery($query)
{
if($this->query($query))
{
return true;
}
}
public function get_result($query)
{
$result = $this->query($query);
if ($result->num_rows > 0){
$row = $result->fetch_assoc();
return $row;
} else
return null;
}
}
?>
3) uses
3) 用途
require 'db.php';
$query="select * from tbl_session";
$sockets = db::getInstance()->get_result($query);
or any other query
或任何其他查询
$query="insert into `tbl_chats` (coloum_name) values('".$val."')";
$wisherID = db::getInstance()->dbquery($query);
回答by Ilija
I prefer to use constants for configuration options instead of variables, for three reasons:
我更喜欢在配置选项中使用常量而不是变量,原因有以下三个:
- They are global, so there's no need to inject them into functions as parameters or using a
global
keyword, - They can't be changed by the app itself (which can happen by accident if you are not careful and cause some awkward bugs),
- Good editors offer code completion and can navigate to the line where constant was declared. This makes work with large projects that have a lot of options a bit easier. This applies to global variables as well, but constants are a bit "cleaner" (rule of thumb is to keep the global scope as clean as possible).
- 它们是全局的,因此无需将它们作为参数或使用
global
关键字注入到函数中, - 它们不能被应用程序本身改变(如果你不小心并导致一些尴尬的错误,这可能会偶然发生),
- 好的编辑器提供代码补全功能,并且可以导航到声明常量的行。这使得处理具有很多选项的大型项目变得更加容易。这也适用于全局变量,但常量有点“干净”(经验法则是保持全局范围尽可能干净)。
Example:
例子:
<?php
const DB_HOST = 'localhost';
const DB_USER = 'user123';
const DB_PASS = '';
const DB_NAME = 'test';
Index:
指数:
<?php
require_once 'config.php';
$link = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
回答by CodeMan
This is correct methos for my config.php
这是我的 config.php 的正确方法
<?php
include 'config.php';
try
{
$host=$config['DB_HOST'];
$dbname=$config['DB_DATABASE'];
$conn= new PDO("mysql:host=$host;dbname=$dbname",$config['DB_USERNAME'],$config['DB_PASSWORD']);
//new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
}
catch(PDOException $e)
{
echo "Error:".$e->getMessage();
}
?>
config.php
配置文件
<?php
$config=array(
'DB_HOST'=>'localhost',
'DB_USERNAME'=>'root',
'DB_PASSWORD'=>'',
'DB_DATABASE'=>'gobinath'
);
?>