0

I have this code which sets the cookies:

    $root = $_SERVER['DOCUMENT_ROOT'] . '/account_share';
    include_once $root . '/php/objects/user.php';

    if (session_status() == PHP_SESSION_NONE) 
    {
        session_start();
    }

    $user = unserialize($_SESSION['user']);

    setcookie('email', $user->getEmail(), time()+3600*24*365); //year
    setcookie('pass', encrypt($user->getPassword()), time()+3600*24*365); //year

    $response = Array();

    if(isset($_COOKIE['email']) && isset($_COOKIE['pass']))
    {
        $response['response'] = 'success';
    }
    else 
    {
        $response['response'] = 'error';    
    }

    echo json_encode($response);

Here the respone is 'success' - the cookies are set.

And then I have this code which I try to run after the cookies are set:

    if (session_status() == PHP_SESSION_NONE) 
    {
        session_start();
    }

    var_dump($_COOKIE);

Here i'm getting this result after the dump:

array(1) { ["PHPSESSID"]=> string(26) "6i2n4tptlhi39f0mglc9v8ab23" }

I have also checked my cookies in the chrome settings and could not find them.

NOTE:

I am using (for now) xampp on my local pc.

So what is wrong with what I have done?!

vlio20
  • 8,955
  • 18
  • 95
  • 180
  • It's not clear. What do you expect, what are you trying to achieve? I'm afraid you're confusing cookies with session somehow. – dkellner Jan 05 '14 at 13:28
  • I don't think you want to save the password in a cookie. No mather wat reason. – Niels Jan 05 '14 at 13:28
  • If you're just trying to keep your user logged in, you should just put some login flag in $_SESSION. For what purpose are you using the cookies? – dkellner Jan 05 '14 at 13:31
  • I am using also session, but also I want to store a cookie for users which don't what to reenter their login credentials each time the session is closed (remember me). – vlio20 Jan 05 '14 at 13:34
  • followed this http://stackoverflow.com/questions/244882/what-is-the-best-way-to-implement-remember-me-for-a-website for better practice. tnx – vlio20 Jan 05 '14 at 14:10

1 Answers1

1

It seems you do not select the domain for which you want to use the cookie.

If you're trying to activate the cookie in the entire domain, modify your setcookie code in:

setcookie('cookiename', value, time, '/');
GiamPy
  • 3,543
  • 3
  • 30
  • 51
  • I tried to put this: `setcookie('cookiename', value, time, '/', 'url');` and it didn't work. this seems working fine – vlio20 Jan 05 '14 at 13:37
  • 1
    The 5th parameter is the **domain**, not the **path** - they are different. – GiamPy Jan 05 '14 at 13:40