Skip to content

Commit 705b45d

Browse files
committed
Add a cookie-based server select dropdown.
Users may specify an array of servers in the $server variable at the top of the file. If $server is a string, or an array with a single value, it will function the same as before. But if $server contains multiple values, the user will be presented with a dropdown to switch between servers.
1 parent 2d634ff commit 705b45d

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

mongodbadmin.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
header('Pragma: no-cache');
2121

22-
$server = 'mongodb://localhost:27017';
22+
$server = array(
23+
'mongodb://localhost:27017',
24+
// 'mongodb://example.org:27017',
25+
);
26+
2327
$options = array(
2428
'connect' => true
2529
);
@@ -30,14 +34,30 @@
3034
}
3135
try
3236
{
33-
$mongo = new Mongo($server, $options);
37+
$mongo = new Mongo(getServer($server), $options);
3438
}
3539
catch (MongoConnectionException $ex)
3640
{
3741
error_log($ex->getMessage());
3842
die("Failed to connect to MongoDB");
3943
}
4044

45+
46+
/**
47+
* Get the current MongoDB server.
48+
*
49+
* @param mixed $server
50+
* @return string $server
51+
*/
52+
function getServer($server)
53+
{
54+
if (is_array($server)) {
55+
return (isset($_COOKIE['mongo_server']) && isset($server[$_COOKIE['mongo_server']])) ? $server[$_COOKIE['mongo_server']] : $server[0];
56+
} else {
57+
return $server;
58+
}
59+
}
60+
4161
/**
4262
* Render a document preview for the black code box with referenced
4363
* linked to the collection and id for that database reference.
@@ -450,7 +470,22 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
450470
<body>
451471

452472
<div id="content">
453-
<h1>PHP MongoDB Admin - <?php echo $server ?></h1>
473+
<h1>
474+
PHP MongoDB Admin -
475+
<?php if (is_array($server)): ?>
476+
<?php if (count($server) > 1): ?>
477+
<select id="server" onChange="document.cookie='mongo_server='+this[this.selectedIndex].value;document.location.reload();return false;">
478+
<?php foreach ($server as $key => $s): ?>
479+
<option value="<?php echo $key ?>"<?php if (isset($_COOKIE['mongo_server']) && $_COOKIE['mongo_server'] == $key): ?> selected="selected"<?php endif; ?>><?php echo $s ?></option>
480+
<?php endforeach; ?>
481+
</select>
482+
<?php else: ?>
483+
<?php echo $server[0] ?>
484+
<?php endif; ?>
485+
<?php else: ?>
486+
<?php echo $server ?>
487+
<?php endif; ?>
488+
</h1>
454489
<?php if (isset($_REQUEST['error'])): ?>
455490
<div class="error">
456491
<?php echo $_REQUEST['error'] ?>

0 commit comments

Comments
 (0)