• support@answerspoint.com

php set session with cookies

2335
$_SESSION['user_id'] = $login;  
setcookie('user_id', '$login' , time()+86000);
header('Location: userindex.php');

function logged_in() {
return (isset($_SESSION['user_id']) || isset($_COOKIE['user_id']) ? true : false;
}

 

I have SESSION but I would not include COOKIE too but I don't know how to restart SESSION with COOKIE. I don't have a idea how I can get that. I create COOKIE but can't logout and have problem with SESSION somebody can help me to fix my problem???? And in every page on top I have logged_in function for check if user is logged in or not I wonna these logged_in function to check if user has cookie to auto login to user cookie. I think it is in logged_in function must get write some code and...

  • PHP

  • asked 9 years ago
  • Gul Hafiz

2Answer


0

I will note that this is not secure, as any one can create the cookie, using something like firebug.

@session_start();

function logged_in() {
    if(!isset($_SESSION['user_id']) && isset($_COOKIE['user_id'])) {
        $_SESSION['user_id'] = $_COOKIE['user_id'];
    }
    return isset($_SESSION['user_id']);
}

function logout() {
    unset($_SESSION['user_id']);
    setcookie("user_id", "", time() - 3600);
    header("Location: http://".$_SERVER['HTTP_HOST']);
    exit;
}

Edit: Added logout() - will remove both session and cookie 'user_id', then redirect to homepage

  • answered 8 years ago
  • B Butts

0

First: you should set it with:

setcookie('user_id', $login , time()+86000);

So $login without quotes. And also maybe set path variable if this cookie should be seen in different pages.

Removing cookie is done with setting negative time value:

setcookie('user_id', '' , time()-86000);
  • answered 8 years ago
  • Gul Hafiz

Your Answer

    Facebook Share        
       
  • asked 9 years ago
  • viewed 2335 times
  • active 9 years ago

Best Rated Questions