2

My login does not seem to be creating a cookie. The form gets all the way to the cookie creation portion of the script and even echos that a cookie was made but it does not actually create one.

Here is the cookie portion of my code:

if (!$error) {

    if (isset($_POST['rememberme'])) {
        setcookie('guruemail', $loginemail, time() + 86400 * 365, '/', NULL);
        setcookie('gurupassword',  md5($loginpassword), time() + 86400 * 365, '/', NULL);
echo "Long-term cookie made";
        } else {
        setcookie('guruemail', $loginemail, false, '/', NULL);
        setcookie('gurupassword',  md5($loginpassword), false, '/', NULL);
echo "Short-term cookie made";
    }

}

The login can be visited at http://protein.guru/signin.phtml

The cookie test can be viewed at: http://protein.guru/testcookie.php

Here is the cookietest code:

<?php
echo "Value is: " . $_COOKIE[$guruemail];
echo "Value is: " . $_COOKIE[$gurupassword];
?>

For the sign-in:

I am using the email: tester3651@outlook.com

Password is: meatloaf

Note:Possible newbie mistake? -- I do not have a session_start(); anywhere in either code. Not sure if I would need that for a straight cookie login.

Any feedback would be appreciated. Thanks everyone.

jmcgee
  • 35
  • 7

2 Answers2

4

As mentioned in the comments: Access the $_COOKIE arrays with strings, instead of a variables.

<?php
    echo "Value is: " . $_COOKIE['guruemail'];
    echo "Value is: " . $_COOKIE['gurupassword'];
?>
jakub_jo
  • 1,494
  • 17
  • 22
  • 3
    It took me a few glances to see what was different in your answer. FWIW, it may be useful to mention replacing the variable with a string – Jeff Feb 16 '16 at 23:29
  • Works perfectly. Thank you so much.. can't believe it was so simple. – jmcgee Feb 17 '16 at 00:25
1

You'll need quotes around the cookie variable

<?php
echo "Value is: " . $_COOKIE['guruemail'];
echo "Value is: " . $_COOKIE['gurupassword'];
?>

Actually it would be much more secure to use $_SESSION instead for users login as users can manually set $_COOKIE.

More details at the following answer: Making login more secure

Panda
  • 6,955
  • 6
  • 40
  • 55
  • +1 for the info. I'll look into sessions but I check the cookies against a database with salted passwords etc. Should be fine for what I'm doing but I do have some concern for people that have cookies disabled. Thanks – jmcgee Feb 17 '16 at 00:28