★ wanayoo — archive 1999 http://fr.php.net/manual/en/function.session-set-cookie-params.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previoussession_save_pathsession_set_save_handlernext
Last updated: Sun, 18 Aug 2002
view the printer friendly version or the printer friendly version with notes or change language to Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

session_set_cookie_params

(PHP 4 )

session_set_cookie_params --  Set the session cookie parameters

Description

void session_set_cookie_params ( int lifetime [, string path [, string domain [, bool secure]]])

Set cookie parameters defined in the php.ini file. The effect of this function only lasts for the duration of the script.

Note: The secure parameter was added in PHP 4.0.4.

User Contributed Notes
session_set_cookie_params
add a note about notes
nirvdrum@negativetwenty.net
16-Aug-2000 04:21

The idea of a session is that it ends when the user closes the browser (maybe even before hand). If you want a cookie to last longer, than use the setcookie() function.

-Kevin

rick@zix.nu
25-Jan-2001 08:36

If you want your login session to last longer, you should log the sessionid in a cookie and before you start your session you should load that sessionid from the cookie like this:

session_id($cookiesession);
session_start();
etc

junkmail@fluidideas.com
22-Feb-2001 05:33

ok. hope this works. first time posting

heres a quick little snippet I came up with to pass sessions between lots of sub domains or c names. it also contains an array walker for reading and outputting the contents of the current array(in this case i use it for http_session_vars)

ex:
www.fluidideas.com
chat.fluidideas.com
mypage.fluidideas.com
www.mypage.fluidideas.com
these will all have access to the session.

!!!BEGIN CUT!!!

session_set_cookie_params ( time()+9999999 , "" , ".fluidideas.com" );

if ( $HTTP_COOKIE_VARS[session_id_set] )
{ session_id( $HTTP_COOKIE_VARS[session_id_set] ); }
session_start ();
Function session_walker($Array)
{
If ( !Is_Array($Array) ) Return $Array;
Reset($Array);
While( List( $Key, $Value ) = Each( $Array ) )
{
If ( Is_Array( $Array[ $Key ] ) )
{
$Array[ $Key ] = session_walker( $Array[ $Key ] );
}
Else
{
echo "key -> " . $Key . "
\n";
echo "value -> " . $Value . "
\n";
}
}
Return $Array;
}

!!!END CUT FOR SESSION INCLUDE!!!
!!!BEGIN CUT!!!

$user_session_id = session_id();
setcookie("session_id_set","$user_session_id",time()+9999999,"", ".fluidideas.com", "");

session_register("user");
session_register("password");
session_register("email");

!!!END CUT FOR LOGIN!!!

now this example will allow the session to carry the data from sub domain to sub domain and it only has one flaw that i've noticed. NS 4.7 is the only browser I've seen this in (also use mozilla .8 and IE5.5) but if the domain doesnt contain a c name the sessions wont pass.

ex: fluidideas.com not www.fluidideas.com

If someone can come up with a fix to this please email me. Thanks

php@mike2k.com
09-May-2001 08:16

[Editor's Note:

Rasmus' Solution from the PHP-General list:

Just use a session cookie (by not providing an expiry time) and add the
server's expiry timestamp to the value of the cookie. Then when you get
that cookie sent to you, check it against your server's time and make the
decision on whether to accept the cookie or not based on that.

That way you are immune from people not having their system clocks set
right.

-Rasmus

--zak@php.net]

A couple things I noticed when using this. I think it only works if you set the session_set_cookie_params() function BEFORE the session_start() function.

Also, when you set the "lifetime" on the cookie, it takes the seconds offset from the SERVER. it sends the cookie encoded to timeout at the SERVER time. So if your server is +2 minutes ahead of the client, and you set the cookie to timeout after 30 seconds, the client actually has 2 minutes and 30 seconds before the cookie times out. I don't know if there's any way that this can be patched in future versions, and the only alternative I think is setting cookies in javascript, which is hardly the point when using all these specific session functions.

gavin_spam@skypaint.com
26-Feb-2002 08:58

The first argument to session_set_cookie_params is the number of seconds in the future (based on the server's current time) that the session will expire. So if you want your sessions to last 100 days:

$expireTime = 60*60*24*100; // 100 days
session_set_cookie_params($expireTime);

I was using time()+$expireTime, which is WRONG (a lot of the session_set_cookie_params() examples I found get this wrong, but probably don't care because they are just doing "infinite" sessions).

steve@saturn5.com
16-Jun-2002 09:54

session_set_cookie_params didn't work at all, no matter how many times we yelled at the computer. we wanted to update the session expiry time after login, so we used setcookie instead, right after session_start:

$expiry = 60*60*24*100; // 100 days

session_start();
setcookie(session_name(), session_id(), time()+$expiry, "/");

if you can't get session_set_cookie_params to work, i recommend using setcookie instead. (yes, you need time()+$expiry when using setcookie.)

shrockc@inhsNO.SPAMorg
19-Jun-2002 03:19

when setting the path that the cookie is valid for, always remember to have that trailing '/'.

CORRECT:
session_set_cookie_params (0, '/yourpath/');

INCORRECT:
session_set_cookie_params (0, '/yourpath');

no comment on how long it took me to realize that this was the cause of my authentication/session problems...

add a note about notes
previoussession_save_pathsession_set_save_handlernext
Last updated: Sun, 18 Aug 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Mon Aug 19 03:55:17 2002 CEST