Home |
Last modified: 16-06-2020 |
Code Igniter is one of the MVC-based frameworks to write web applications in PHP. I'll assume that you run the Apache web server, and you uploaded Code Igniter at the root of your web server, ie. index.php and license.txt live at the root, and not in some sub-directory.
Create /security/application/controllers/hello.php with the following code:
and hit http://www.acme.com/hello .
To make a controller the default controller and not having to show it in the URL, edit /system/application/config/routes.php to change $route['default_controller'] = 'MyCtrl';
Besides CI's own session class, there are other, enhanced alternatives: native session, PHPsession, and DB session.
If you want a whole solution to check that the user is legit when calling any controller, CI doesn't come with a built-in user authentication system, but there are some solutions:
Here's some code from Trini to create a session:
function sess_create()
{
$sessid = '';
while (strlen($sessid) < 32)
{
$sessid .= mt_rand(0, mt_getrandmax());
}
$this->userdata = array(
'session_id' => md5(uniqid($sessid, TRUE)),
'ip_address' => $this->object->input->ip_address(),
'user_agent' => substr($this->object->input->user_agent(), 0, 50),
'last_activity' => $this->now
);
$this->object->db->query($this->object->db->insert_string($this->session_table, $this->userdata));
// Write the cookie
$this->sess_send_cookie();
}
// END sess_create()
"Plugins work almost identically to Helpers. The main difference is that a plugin usually provides a single function, whereas a Helper is usually a collection of functions. Helpers are also considered a part of the core system; plugins are intended to be created and shared by our community."
It's safer to keep non-code stuff outside the CI directory. You could create /CSS at the same level as CI's /system directory, and use the URL helper to create a link to the stylesheet dynamically in your views:
or if the server allows the short-hand version of PHP:
base_url() returns the variable of the same name in config.php, making your site location-independent; base_url() must be loaded prior to loading a view through $this->load->helper('url'); . Make sure the .htaccess file doesn't rewrite the URL when it contains references to the CSS and images directories.
Make sure that the shared server you're using actually supports URL rewriting through .htaccess.
Here's what to use in the URL Rewriting section of version 2.4, assuming you installed CI under /igniter/ :
This filter doesn't rewrite the URL when referencing stylesheets and images, ie. /igniter/css/style.css will be ignored, while /igniter/main will be turned into /igniter/index.php?/main .
The online tutorial uses the short PHP tags <?= . If your setup doesn't allow this, you'll have to change this to the usual <?php , and add echo() to actually display data sent by controllers.
To check whether support for short tags is enabled on the web server, check the output of phpinfo() for "short_open_tag".
Two solutions:
and...