Jump to content

Is this the correct way?


newbtophp

Recommended Posts

Hi,

 

Is this the correct way to go by setting cookies for my login 'remember' functionality?, I've posted the relevant login and logout code.

 

Login:

 

<?php
session_start();

//some code...

if (isset($_POST['remember'])) {

$expire = time() + 1728000; // Expire in 20 days

$site_domain = 'phpfreaks.com'; //example

$_SESSION['username'] = $_POST['username']; //don't worry this has been validated 

$username = $_SESSION['username'];

//some sql query to fetch code..

$code = $row['code'];

setcookie('username', $username, $expire, NULL,  ".$site_domain", NULL, TRUE);

setcookie('code', $code, $expire, NULL, ".$site_domain", NULL, TRUE);

}
?>

 

Logout:

 

<?php
session_start();

if (isset($_COOKIE['username'])) {

$expire = time() - 1728000; // Expire in 20 days

$site_domain = 'phpfreaks.com'; //example

$username = $_SESSION['username'];

//some sql query to fetch code..

$code = $row['code'];

setcookie('username', $username, $expire, NULL, ".$site_domain", NULL, TRUE);

setcookie('code', $code, $expire, NULL, ".$site_domain", NULL, TRUE);

}

unset($_SESSION['username']);

$_SESSION = array();

session_destroy();
?>

 

Please tell me if it looks good to go and/or any improvements.

 

PS:

 

I want it accessible on all areas of the domain (including subdomains and paths etc.), and HTTP ONLY (so js can't access it) - I believe I've set the right parems?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.