Trying to se $_COOKIE in PHP but having trouble ....

Posted on 2013-06-27
Last Modified: 2013-06-27
Hey all.

I have a PHP program (PHP is not my forte) and am having trouble when trying to use a set cookie for authentication.

In the following code, the first time through the user ID and password is used to authenticate. If authenticated, a cookie is set on the user's machine.

The way it "should" work is that for future calls, the cookie should be used first to authenticate and if it's not valid, then check for user ID / password.

I have verified that the cookie is in fact being created in the correct domain, but it is not being used for authentication due to it not "being found". Again, the cookie is on my machine but the PHP code, when checking if it's "empty", does *not* find it.

In the "try" block, I've tried using the incoming id / password as well as this:

$_COOKIE['loginapp.token_key'] = $loginapp->authenticatePrincipal(

If the cookie "is found", should it not use that and *not* the id / password for authentication?

Any help would be greatly appreciated.

 * This file demonstrates using Atlassian loginapp for Single Sign On.
header('Content-Type: application/json');
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
$loginapp_app_name = 'loginappapp';
$loginapp_app_password = 'loginapppass';
$loginapp_url = 'loginapp_url';

$loginappuserid = (isset($_REQUEST['myuserid']) ? $_REQUEST['myuserid'] : '');
$loginapppass = (isset($_REQUEST['mypassword']) ? $_REQUEST['mypassword'] : '');
$loginapptoken = (isset($_REQUEST['mytoken']) ? $_REQUEST['mytoken'] : '');

session_set_cookie_params(0, '/', '');

$username = NULL;
$groups   = NULL;


$is_authenticated = FALSE;
if (!empty($_COOKIE['loginapp.token_key']))      <---- although the cookie exists, this check fails
      // If the user already had a loginapp token, we need to verify that it's still valid
      $is_authenticated = $loginapp->isValidPrincipalToken(
if (!$is_authenticated)
      if (!isset($_SERVER['PHP_AUTH_USER'])) {
            header('WWW-Authenticate: Basic realm="loginapp Login"');
            header('HTTP/1.0 401 Unauthorized');
            $username = 'Forbidden';

                **** when using ID and password, the cookie is set ****
            $_COOKIE['loginapp.token_key'] = $loginapp->authenticatePrincipal(

            setcookie('loginapp.token_key', $_COOKIE['loginapp.token_key'], time() + 3600, '/', '');
            print 'COOKIE: '.$_COOKIE['loginapp.token_key'].'<br>';
            $is_authenticated = TRUE;
      catch (Exception $e)
            // I have no idea why, but instead of throwing an
            // invalid username or password exception, we get
            // an exception with the username provided if either is wrong.
            if ($e->getMessage() == $_SERVER['PHP_AUTH_USER'])
                  // todo: prompt for login again
            //throw $e;
            $username = 'Forbidden';

Question by:Webspeeder
LVL 34

Expert Comment

ID: 39281341
Add this after $is_authenticated = FALSE;

  echo "Cookie loginapp.token_key exists: ";
LVL 55

Accepted Solution

Julian Hansen earned 500 total points
ID: 39281355
try adding this to the start of your script to see what is in $_COOKIE




enclose in <pre> tags to better see the output if required.

This might shed some light

Author Closing Comment

ID: 39281417
Well, that help to fix the issue but I'm still confused.

The cookie was being set with a ".", 'loginapp.token' but when using the command you gave, it came back with loginapp_token. The period was an underscore.

I changed all the periods to underscores and now it works. On my machine, even now when looking, I see the "period" version, not the underscore. I deleted the cookie and it doesn't find it anymore.

I'll get it recreated, but if it is being set with an underscore, why does the name (in Google Chrome) show as a period?
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39281421
Also this is an age old issue

  setcookie('loginapp.token_key', $_COOKIE['loginapp.token_key'], time() + 3600, '/', '');

There is NOTHING in the $_COOKIE at this point BECAUSE $_COOKIE gets its value from the REQUEST and you have not yet sent that request back to this script from the client

            print 'COOKIE: '.$_COOKIE['loginapp.token_key'].'<br>'; // empty until next time

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question