Entalpia
Entalpia

Reputation: 787

Too many redirects - cookies JS + php implementation

I have a simple website where you need only a password to access the contents. Then there are 3 fields where user inputs data, which are then stored in cookies. In the end - there is a logout script that resets the session and unsets cookies.

Please find the relevant code below:

Login page (index)

<?php
session_start();

$password = '';
$wrongPassword = '';
if (isset($_POST['sub'])) {
$password = $_POST['login_passcode'];
if ($password === 'PASSCODE') {
    $_SESSION['login'] = true;
    header('LOCATION:/personal.php');
    die();
} else {
    $wrongPassword = true;
}
}

if (isset($_COOKIE['m_username'])) {
header('LOCATION:/personal.php');
die();
}
?>

The page with contents, where user inputs name, department and start date

<?PHP
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header("Location:/index.php");
die();
}
?>

and the logout script:

<?PHP
session_start();
if (isset($_COOKIE[session_name()])):
    setcookie(session_name(), '', time() - 7000000,'/');
endif;
if (isset($_COOKIE['m_username'])):
    setcookie('marriott_username', '', time() - 7000000,'/');
endif;
if (isset($_COOKIE['m_startdate'])):
    setcookie('marriott_startdate', '', time() - 7000000,'/');
endif;
if (isset($_COOKIE['m_department'])):
    setcookie('m_department', '', time() - 7000000,'/');
endif;
$_SESSION = array();
session_destroy();
header ("Location:/index.php");
die();
?>

jQuery to create cookies below:

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

Cookies do expire (at least on chrome), however after trying to access website after a few hours or days, I get the error about too many redirections. I believe this might be due to some differences between session expiration time and cookies expiration time (5 days for cookies), but I don't really know where to start fixing these...

Also, on Internet Explorer (IE8) the redirects problem occurs even when I go through logout directly.

Will be grateful for any help, E.

Upvotes: 0

Views: 694

Answers (1)

HenryTK
HenryTK

Reputation: 1287

You are correct in thinking different cookie expirations are behind the too many redirects problem.

If isset($_COOKIE['m_username']) is true in the index page, then you are redirected to the personal page, in which if if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) is also true, it sends you back to the index, therefore creating a loop. This would be caused by the session cookie expiring before the cookies you set.

The $_COOKIE and $_SESSION superglobals refer to two different sets of cookies. One solution is to use just the PHP session and store all your session data in the $_SESSION superglobal.

For example:

$_SESSION['m_username'] = 'whatever_value';

This will however generate an overhead in extra memory usage. If you still want to use your own cookies then just make sure any logic determining redirects is based on the session, not the presence of cookies you set.

For example:

// When logging in
$_SESSION['logged_in'] = true;

// On every page that requires login
if(!$_SESSION['logged_in']) // Redirect

Upvotes: 2

Related Questions