全局php文件


Global php file

每个php页面都是这样开头的:

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name
// Connect to server and select database.
$con = mysqli_connect($host,$username,$password,$db_name); 
session_start();
?>

那么我怎么能做一个。php文件,保存这些变量,并包括在每个html页面的开始。如果我想用$con变量。如何使用

将此代码放入文件(例如:Config.php),并在所有其他PHP文件中包含/要求它

<?php
include 'config.php';
// here you could use the $con variable
PHP文档:

http://php.net/manual/en/function.include.php

http://php.net/manual/en/function.require.php

我认为最好使用require_onceinclude_once,因为如果你包括一个页面到另一个与include 'config.php';顶部-你得到一个消息

'A session had already been started - ignoring session_start()'

PHP文档:

http://php.net/manual/en/function.require-once.php

http://php.net/manual/en/function.include-once.php