Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter & PHP check if session exist

How can i simply check if cookies are enabled and user session too in PHP?

I need the really lighter piece of code of this world to do that, can anyone show me somenthing?

I'm on Codeigniter but i'm planning to use native PHP for this control.

my code:

if(session_start()){ echo 'started'; }

as i know Codeigniter doesn't uses native PHP session, how to do so?

like image 544
itsme Avatar asked Oct 12 '12 08:10

itsme


People also ask

What is CodeIgniter used for?

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

Is CodeIgniter still used?

CodeIgniter is still exist but the name of this framework has become synonymous with a low-quality solution that is why we advise you Laravel as a better alternative. Both Laravel and CodeIgniter are open-source PHP framework.

Is CodeIgniter front end or backend?

Front-end web development Expertise in creating high-quality CodeIgniter web applications and advanced portals that are fully functional and add value to a business enterprise.

Which is better Laravel or CodeIgniter?

Key Difference : Laravel is relational object-oriented and CodeIgniter is Object-oriented. Laravel provides authentication class features and CodeIgniter does not have built-in authentication features. Laravel has inbuilt unit testing tool and CodeIgniter does not have inbuilt unit testing tool.


2 Answers

Check for a valid session id:

$sid = session_id();

For example:

$sid = session_id();
if($sid) {
    echo "Session exists!";
} else {
    session_start();
}
like image 152
Brendan Bullen Avatar answered Sep 24 '22 10:09

Brendan Bullen


The first point is "Don't fight the framework" when your framework has some functions than use it. Normally in the Framework classes are functions to prevent injections.

Here is a Post when tells you how to check the cookie:

Check if cookies are enabled

And for the session

How to tell if a session is active?

like image 32
René Höhle Avatar answered Sep 25 '22 10:09

René Höhle