{"id":294,"date":"2025-09-24T06:15:15","date_gmt":"2025-09-24T06:15:15","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=294"},"modified":"2025-09-24T06:16:31","modified_gmt":"2025-09-24T06:16:31","slug":"mongodb-with-php-crud-operationsession","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=294","title":{"rendered":"MongoDB with Php crud operation[session]"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 Check Your PHP Environment<\/h2>\n\n\n\n<p>Open a command prompt and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -v\n<\/code><\/pre>\n\n\n\n<p>Note the <strong>PHP version<\/strong>, <strong>architecture<\/strong> (x64 or x86), and whether it\u2019s <strong>Thread Safe (TS)<\/strong> or <strong>Non-Thread Safe (NTS)<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 Download the Correct Extension DLL<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the official PECL page:<br><a>https:\/\/pecl.php.net\/package\/mongodb<\/a><\/li>\n\n\n\n<li>Choose the latest stable release (e.g. <code>mongodb-1.x.x<\/code>).<\/li>\n\n\n\n<li>Download the zip matching:\n<ul class=\"wp-block-list\">\n<li>Your PHP version (e.g. 8.2, 8.1, 7.4\u2026)<\/li>\n\n\n\n<li>Your architecture (x64 or x86)<\/li>\n\n\n\n<li>TS or NTS as reported by <code>php -v<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>Inside the zip you\u2019ll find <strong>php_mongodb.dll<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 Install the Extension<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Copy <code>php_mongodb.dll<\/code> into: <code>C:\\xampp\\php\\ext<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4\ufe0f\u20e3 Enable in <code>php.ini<\/code><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open: <code>C:\\xampp\\php\\php.ini<\/code><\/li>\n\n\n\n<li>Add (or uncomment) this line: <code>extension=mongodb<\/code><\/li>\n\n\n\n<li>Save the file.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5\ufe0f\u20e3 Restart Apache<\/h2>\n\n\n\n<p>Use the XAMPP Control Panel:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stop Apache<\/li>\n\n\n\n<li>Start Apache again<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6\ufe0f\u20e3 Verify Installation<\/h2>\n\n\n\n<p>Create a file <code>info.php<\/code> in <code>htdocs<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php phpinfo();\n<\/code><\/pre>\n\n\n\n<p>Open <code>http:\/\/localhost\/info.php<\/code> and search for <strong>mongodb<\/strong>.<br>You should see something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MongoDB support =&gt; enabled\n<\/code><\/pre>\n\n\n\n<p>If you don\u2019t see it, re-check that the DLL matches your PHP version and that you edited the correct <code>php.ini<\/code> (XAMPP has both <code>php\\php.ini<\/code> and <code>apache\\bin\\php.ini<\/code>\u2014edit the one used by Apache, usually <code>C:\\xampp\\php\\php.ini<\/code>).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7\ufe0f\u20e3 Use the New Driver in Code<\/h2>\n\n\n\n<p>After the extension loads, use Composer\u2019s library in your PHP code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require mongodb\/mongodb\n<\/code><\/pre>\n\n\n\n<p>Then in <code>register.php<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>require 'vendor\/autoload.php';\n\n$client = new MongoDB\\Client(\"mongodb:\/\/localhost:27017\");\n$db     = $client-&gt;selectDatabase('yourdb');\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Important:<\/strong><br>Do not use the legacy <code>MongoClient<\/code> or <code>new Mongo()<\/code> syntax.<br>The current driver works with <code>MongoDB\\Client<\/code> (from Composer) or <code>MongoDB\\Driver\\Manager<\/code> (low-level).<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 <code>register.php<\/code> (with session start)<\/h2>\n\n\n\n<p>This script registers the user <strong>and<\/strong> starts a session so you can store the username.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nsession_start();  \/\/ Start or resume the session\n\nif (isset($_POST&#91;'submit'])) {\n    $username = trim($_POST&#91;'username']);\n    $password = $_POST&#91;'password'];\n\n    if ($username === '' || $password === '') {\n        exit(\"Username and password required.\");\n    }\n\n    try {\n        $m = new MongoDB\\Driver\\Manager('mongodb:\/\/localhost:27017');\n    } catch (MongoDB\\Driver\\Exception\\Exception $e) {\n        exit(\"MongoDB connection failed: \" . $e->getMessage());\n    }\n\n    \/\/ Optional: create collection if not exists\n    $command = new MongoDB\\Driver\\Command(&#91;\"create\" => 'e1']);\n    try {\n        $m->executeCommand(\"login\", $command);\n    } catch (MongoDB\\Driver\\Exception\\CommandException $e) {\n        if ($e->getCode() != 48) { \/\/ 48 = namespace exists\n            exit(\"Error creating collection: \" . $e->getMessage());\n        }\n    }\n\n    \/\/ Insert new user\n    $bulk = new MongoDB\\Driver\\BulkWrite;\n    $bulk->insert(&#91;\n        \"username\" => $username,\n        \"password\" => password_hash($password, PASSWORD_BCRYPT)\n    ]);\n\n    try {\n        $m->executeBulkWrite('login.e1', $bulk);\n\n        \/\/ Store username in session\n        $_SESSION&#91;'username'] = $username;\n\n        header('Location: page2.php');\n        exit;\n    } catch (MongoDB\\Driver\\Exception\\BulkWriteException $e) {\n        exit(\"Error inserting document: \" . $e->getMessage());\n    }\n}\n?>\n&lt;!DOCTYPE html>\n&lt;html>\n&lt;body>\n&lt;center>&lt;h1>Registration Form&lt;\/h1>&lt;\/center>\n&lt;form action=\"\" method=\"post\">\n    &lt;label>Username :&lt;\/label>\n    &lt;input type=\"text\" name=\"username\">&lt;br>\n    &lt;label>Password :&lt;\/label>\n    &lt;input type=\"password\" name=\"password\">&lt;br>&lt;br>\n    &lt;input type=\"submit\" value=\"Register\" name=\"submit\">\n&lt;\/form>\n&lt;\/body>\n&lt;\/html>\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 <code>page2.php<\/code> (protected page)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nsession_start();\n\n\/\/ Check if user is logged in\nif (!isset($_SESSION&#91;'username'])) {\n    header('Location: register.php'); \/\/ or a login page\n    exit;\n}\n?&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;body&gt;\n&lt;h1&gt;Welcome, &lt;?php echo htmlspecialchars($_SESSION&#91;'username']); ?&gt;!&lt;\/h1&gt;\n&lt;p&gt;This is a protected page.&lt;\/p&gt;\n&lt;a href=\"logout.php\"&gt;Logout&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 <code>logout.php<\/code> (destroy session)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nsession_start();\nsession_unset();   \/\/ remove all session variables\nsession_destroy(); \/\/ destroy the session\nheader('Location: register.php');\nexit;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> How It Works<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>session_start()<\/code> must be the <strong>first line<\/strong> (before any output) on every page that uses sessions.<\/li>\n\n\n\n<li>After successful registration, we store <code>$_SESSION['username']<\/code>.<\/li>\n\n\n\n<li>Any protected page checks <code>isset($_SESSION['username'])<\/code>.<\/li>\n\n\n\n<li>Logging out simply calls <code>session_destroy()<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>With these changes, your MongoDB-based registration now supports full PHP session management.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\ufe0f\u20e3 Check Your PHP Environment Open a command prompt and run: Note the PHP version, architecture (x64 or x86), and whether it\u2019s Thread Safe (TS) or Non-Thread Safe (NTS). 2\ufe0f\u20e3 Download the Correct Extension DLL Inside the zip you\u2019ll find php_mongodb.dll. 3\ufe0f\u20e3 Install the Extension 4\ufe0f\u20e3 Enable in php.ini 5\ufe0f\u20e3 Restart Apache Use the XAMPP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294"}],"version-history":[{"count":1,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":295,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions\/295"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}