-
Notifications
You must be signed in to change notification settings - Fork 9
/
connectdb.php
35 lines (34 loc) · 1.25 KB
/
connectdb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
function connectdb($database = NULL)
{
if (getenv('DATABASE_URL')) {
// http://username:password@hostname:9090/path
$db_props = parse_url(getenv('DATABASE_URL'));
$host = $db_props['host'];
$port = $db_props['port'];
$username = $db_props['user'];
$password = $db_props['pass'];
$database = $database ? $database : substr($db_props['path'], 1); // remove the slash from '/path'
} else {
if ($database == '') {
$database = NULL;
} else {
$database = "inject_demodb";
}
$host = getenv('SQL_INJECTION_DB_HOST') ? getenv('SQL_INJECTION_DB_HOST') : 'localhost';
$port = getenv('SQL_INJECTION_DB_PORT') ? getenv('SQL_INJECTION_DB_PORT') : 3306;
$username = getenv('SQL_INJECTION_DB_USERNAME') ? getenv('SQL_INJECTION_DB_USERNAME') : 'sql_injection';
$password = getenv('SQL_INJECTION_DB_PASSWORD') ? getenv('SQL_INJECTION_DB_PASSWORD') : 'foobar';
}
try {
$db = mysqli_connect($host, $username, $password, $database, $port);
if (!$db) {
echo ("Connection failed: " . mysqli_connect_error());
}
return $db;
} catch (Error $e) {
echo ("Connection failed: " . $e);
} catch (Exception $e) {
echo ("Connection failed: " . $e);
}
}