Setting and Destroying SESSION in PHP

Good day everyone! Especially for those who are beginners in PHP. In This tutorial I will teach you Setting and Destroying SESSION in PHP

Are you wondering how login system works in a web based system? We’ll I’ll give you a very simple and meaningful idea on how it works, SESSION using PHP.

So SESSION, is a way to store information (in variables) to be used across multiple pages. This is just like the COOKIE. But unlike the cookie, the information is not stored in the users computer. Meaning when you destroy it. I cannot be retrieved again otherwise start new one.

So let’s start now the first part of this tutorial. I will now teach you in setting a session. Commonly session is used for the login system and for other purposes.

Now create a page for the login form. Then put the html tags needed in the page then inside the body tag. Put this codes for the login.

<form method="post" action="login_action.php">
<label for="username">Username:</label><br>
<input type="tex" name="username" required /><br><br>
<label for="password">Password:</label><br>
<input type="password" name="password" required /><br><br>
<input type="submit" value="login" />
</form>

Inside you login form page before the  “<html>” begin tag, put the PHP code.

<?php session_start(); ?>

This PHP code should be the very first thing before the HTML tags.

When we pass the session variable in the login form it does not mean that it would pass individually in every page of your website. Instead, they are retrieved from the session we open at the beginning of each page. (session_start())

Meaning to say, the “session_start()” should be included in all pages before the HTML tags in order to work the session.

Now we are in the second part of this tutorial. In destroying the session/s.

Create a page, name it as “logout.php” then put the following codes.

[php]&lt;?php session_start(); if ($_session_destroy()) { header("Location: index.php"); }  ?&gt;[/php]

The codes “session_destroy()” means for destroying the session. Before this php code. The “session_start()” must be the first be written. Then we put the “session_destroy()” in a conditional statement meaning. If the session has been destroyed, the page would be redirect to “index.php”.

That would be all.

If you have questions regarding this tutorial entitled “Setting and Destroying SESSION in PHP”. Feel free to contact us by commenting below or visit on our contact page. Thank you.

Frequently Asked Questions

How does this PHP project work?

Built with vanilla PHP (no framework) and MySQL backend. Standard structure: form HTML, PHP script handlers, MySQL via PDO or mysqli, sessions for auth, Bootstrap for responsive layout. Ready to extend for BSIT capstone scope.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Leave a Comment