Jump to content

Archived

This topic is now archived and is closed to further replies.

tjhilder

$_COOKIE question

Recommended Posts

Hi,

til now I've been using $_SESSION but I want to change it to $_COOKIE so that the user's login doesn't expire when the browser is closed.

right now I have it like

[code]$_SESSION['member_id'] = $row[0];[/code]

etc, was wondering if it would be a case of doing it like

[code]$member_id_cookie = $row[0];
setcookie("site_member_id", $member_id_cookie, time()+3600, "/", ".mysite.com", 1);[/code]

then I'd find the info by changing this:

[code]if (isset($_SESSION['member_id']))
{
    if ($_SESSION['something_else'] <= 4)
        {
[/code]

to:

[code]if (isset($_COOKIE['site_member_id']))
{
    if ($_COOKIE['something_else'] <= 4)
        {
[/code]

would this work? if not, if you could give me some info that would help me that would be great!

thanks in advance.

TJ

Share this post


Link to post
Share on other sites
im no cookie expert,

but it looks good enough to me, instead of asking if it would work, why dont ya give it a quick whirl, then if it doesnt work, ask for help :)

Share this post


Link to post
Share on other sites
ok so I tried doing that, ran it, and nothing happened, no cookies stored.

[code]
                $username_cookie = $row[1];
                $member_id_cookie = $row[0];
                $rank_cookie = $row[3];
                setcookie("v3_user", $username_cookie, time()+3600, "/", "www.tjhilder.co.uk", 1);
                setcookie("v3_id", $member_id_cookie, time()+3600, "/", "www.tjhilder.co.uk", 1);
                setcookie("v3_rank", $rank_cookie, time()+3600, "/", "www.tjhilder.co.uk", 1);
[/code]

any ideas?

Share this post


Link to post
Share on other sites
ultimately, totally replacing $_SESSION with $_COOKIE is floored. not saying it wouldnt work, but for some it wouldnt.
imagine if a user had cookies turned off....

the best way to keep a user logged in would be a bit of a mix of both methods. carry on using sessions, but when the user logs on, set the relevent cookies if they choose to (via a 'remember me' box or something).

the easiest way then would be to use the cookie info instead of presenting the login page. just make sure that when 'logout' is used that the cookies are also destroyed.

at least this way, users who have cookies turned off will still be able to use your site.

cheers
Mark

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.