How to declare a variable as global variable so it can be use inside any function. I want the db connect variable $connect
. In my function the $connect variable is not working as they are in the same page.
Here is my db connect code
$localhost = "localhost";
$username = "root";
$password = "";
$dbname = "xxxxxxxx";
// create connection
$connect = new mysqli($localhost, $username, $password, $dbname);
// check connection
if($connect->connect_error) {
die("connection failed : " . $connect->connect_error);
}
and here is my Function
function secureInput($var){
global $connect;
$output = '';
if (is_array($var)){
foreach($var as $key=>$val){
$output[$key] = secureInput($val);
}
} else {
$var = strip_tags(trim($var));
if (function_exists("get_magic_quotes_gpc")) {
$output = mysqli_real_escape_string($connect,get_magic_quotes_gpc() ? stripslashes($var) : $var);
} else {
$output = mysqli_real_escape_string($connect,$var);
}
}
if (!empty($output))
return $output;
}
want to use that function as like bellow to secure input. If there is any other solution please help me to fix this.
$username = secureInput($_POST['username']);
$password = secureInput($_POST['password']);