Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-18-2012, 10:27 AM   PM User | #1
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
Sessions and cookies to log into a website

Does anyone know any good tutorials or examples on using cookies to store session ids.

i want to store username and password in a session and want to store the session id in a cookie and when a user comes back to the site i want the website to remember the session.

At the moment i am only using sessions and when i close the browser i have to re enter my user name and password to gain access to the site.

Any ideas on how to do this?
kevinkhan is offline   Reply With Quote
Old 12-18-2012, 02:29 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Cookies are already used by default when specifying the session_start.
If the browser is closed you cannot re-invoke the session. After 24 minutes there is a 1% chance that it will purge the old session records.
For this you need to create database managed sessions. You can use the session_set_save_handler and give it an SessionHandlerInterface object (5.4+) or use each function configured to handle each part of the session (which can also be an object but you need to specify the methods individually). Then you simply do nothing in the gc (garbage collection) function/method.
After this, you specify the session_set_cookie_params and give it the number of seconds it will be valid for. That should let it use a persisting cookie.
See here: http://ca3.php.net/manual/en/functio...ve-handler.php
and http://onlamp.com/pub/a/php/2001/05/10/sessions.html for more information. The latter is old, but the principles are the same. They globalize, but you can get around that by using an object (since you likely won't want to use the old mysql library and opt for either the MySQLi (assuming MySQL in use) or PDO) which can be instantiated with a database connection object and stored locally in a variable.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
kevinkhan (12-18-2012)
Old 12-18-2012, 09:30 PM   PM User | #3
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Cookies are already used by default when specifying the session_start.
If the browser is closed you cannot re-invoke the session. After 24 minutes there is a 1% chance that it will purge the old session records.
For this you need to create database managed sessions. You can use the session_set_save_handler and give it an SessionHandlerInterface object (5.4+) or use each function configured to handle each part of the session (which can also be an object but you need to specify the methods individually). Then you simply do nothing in the gc (garbage collection) function/method.
After this, you specify the session_set_cookie_params and give it the number of seconds it will be valid for. That should let it use a persisting cookie.
See here: http://ca3.php.net/manual/en/functio...ve-handler.php
and http://onlamp.com/pub/a/php/2001/05/10/sessions.html for more information. The latter is old, but the principles are the same. They globalize, but you can get around that by using an object (since you likely won't want to use the old mysql library and opt for either the MySQLi (assuming MySQL in use) or PDO) which can be instantiated with a database connection object and stored locally in a variable.
This seems very complicated. Is there any easier way of doing this?
kevinkhan is offline   Reply With Quote
Old 12-18-2012, 09:55 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by kevinkhan View Post
This seems very complicated. Is there any easier way of doing this?
Nope.
You can maybe find a pre-existing project to work with. HTTP doesn't generate persisting sessions (its a protocol limitation), so your only options are to either make use of the overriding capability such as the using the session_set_save_handler, or write something completely customized. Cookies can be manually assigned if you do that.
Fou-Lu is offline   Reply With Quote
Old 12-19-2012, 01:34 AM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
This is what I found and use. I've removed my code so you can just put yours into the event functions:

PHP Code:
<?php
class FileSessionHandler
   
{
   protected 
$savePath;
   protected 
$sessionName;

   function 
open($savePath$sessionName)
      {
      
$this->savePath $savePath;
      
$this->sessionName $sessionName;
      }

   function 
close()
      {
      
//
      
}

   function 
read($id)
      {

      }

   function 
write($id$data)
      {

      }

   function 
destroy($id)
      {

      }

   function 
gc($maxlifetime)
      {
      
//
      
}
   }

$handler = new FileSessionHandler();
session_set_save_handler
   
(
   array(
$handler'open'),
   array(
$handler'close'),
   array(
$handler'read'),
   array(
$handler'write'),
   array(
$handler'destroy'),
   array(
$handler'gc')
   );

// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');
?>
Save it as a file and simply include it into your script BEFORE calling session_start() but after opening your database connection. You'll also need yourself a table for your sessions (named sessions would be sensible) and at least two columns - one for serialized data and one for the session id. Just including the file will do everything you need automatically so the moment you call session_start(), it will read out from the database (once you've written that code in) and make everything available in the $_SESSION array as normal.

The gc function is for garbage collection. You probably don't want that but if you did and you had a date column you could delete session records that were over a year old etc.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.