Simple Counter App - Codeigniter Typing CST Test
Loading…
Simple Counter App — Codeigniter Code
Demonstrates a simple CodeIgniter controller and routes for a counter using session for state persistence.
<?php
// application/controllers/Counter.php
class Counter extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
if (!$this->session->has_userdata('count')) {
$this->session->set_userdata('count', 0);
}
}
public function index() {
$count = $this->session->userdata('count');
echo "<h2>Counter: $count</h2>";
echo "<form method='post' action='/counter/increment'><button type='submit'>+</button></form>";
echo "<form method='post' action='/counter/decrement'><button type='submit'>-</button></form>";
echo "<form method='post' action='/counter/reset'><button type='submit'>Reset</button></form>";
}
public function increment() {
$count = $this->session->userdata('count') + 1;
$this->session->set_userdata('count', $count);
redirect('/counter');
}
public function decrement() {
$count = $this->session->userdata('count') - 1;
$this->session->set_userdata('count', $count);
redirect('/counter');
}
public function reset() {
$this->session->set_userdata('count', 0);
redirect('/counter');
}
}Codeigniter Language Guide
CodeIgniter is a powerful PHP framework with a small footprint, designed for developers who need a simple and elegant toolkit to build full-featured web applications quickly.
Primary Use Cases
- ▸Developing custom PHP web applications
- ▸Creating RESTful APIs quickly
- ▸Rapid prototyping for startups
- ▸Building internal enterprise tools and dashboards
- ▸Integrating with third-party APIs or legacy systems
Notable Features
- ▸Lightweight and fast performance
- ▸MVC architecture for organized code
- ▸Built-in security and XSS/CSRF protection
- ▸Comprehensive libraries and helpers
- ▸Flexible URL routing and easy configuration
Origin & Creator
Created by EllisLab in 2006, CodeIgniter was designed to offer a simple and lightweight alternative to larger PHP frameworks while maintaining flexibility and speed.
Industrial Note
CodeIgniter is particularly useful for rapid prototyping, small to medium enterprise web applications, and performance-sensitive PHP projects where simplicity and speed are priorities.