Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session without cookies

Is there a way that I can initiate a persistent session in PHP without the placement of a session cookie? Are there other ways of maintaining a session across pages, such as an IP address-based solution?

My reason for asking is, is that although most users have cookies on, I want to see if there's a way for a login system to work for those with it disabled (even though I think disabling cookies is just unnecessary paranoia, personally).

like image 450
Delan Azabani Avatar asked Sep 18 '10 08:09

Delan Azabani


People also ask

Can PHP session work without cookies?

You can also login without Cookies only by Session Id and Time, but you have to write them both in your Database direct after Successful Login. I have in index. php something like this that will always generate a new session id based on time and the old session id if conditions are not verified.

Are PHP sessions cookies?

PHP SessionsSessions are an alternative to cookies. A session is usually a file or database record on the server side which contains the small pieces of data which the server wants to store for each user.


2 Answers

I don't think it's too much to ask your users to enable cookies. I find it silly when people turn them off entirely.

Otherwise, you can set your session.use_only_cookies to "0" to force the appendage of a session ID to URLs within your php. This approach, however, has several draw backs. Mainly that of keeping the state within the URL, as opposed to the Cookie header. If a user were to copy and paste the URL of the page they were on, and someone else were to click on it, they would both be using the same session.

<?php      ini_set("session.use_cookies", 0);      ini_set("session.use_only_cookies", 0);      ini_set("session.use_trans_sid", 1);      ini_set("session.cache_limiter", "");      session_start(); 
like image 150
PureForm Avatar answered Sep 22 '22 20:09

PureForm


You can set the ini-Value of session.use_trans_sid to true in order to activate appending the session id to every URL. Have a look at this.

For security purposes you should then limit the session to the IP that created the session. This is not perfectly secure though, as someone with the same IP (behind a proxy e.g.) could reuse that very same session.

like image 21
halfdan Avatar answered Sep 22 '22 20:09

halfdan