-2

I really need some help with PHP. I always get this error:

Notice: Undefined index: sUS_UserID in C:\xampp\htdocs\fazebook\inc\class.user.php on line 21

My "error" code:

$UserLoginQ = $DB->Query("SELECT * FROM `Users` WHERE `UserID` = '{$_COOKIE["sUS_UserID"]}'"); // LINE 21
if (intval($DB->Num($UserLoginQ)) > 0) {
    $UserLoginA = $DB->Arr($UserLoginQ);
    if ($_COOKIE["sUS_UserID"] == $UserLoginA["UserID"]) {
        if ($_COOKIE["sUS_Security"] == md5($UserLoginA["LastIP"])) {
            if ($_COOKIE["sUS_Password"] == md5($UserLoginA["Password"])) {
                $this->ID       = (int) $UserLoginA["UserID"];
                $this->Name     = $UserLoginA["Username"];
                $this->LoggedIn = true;
            }
        }
    }
}
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • The error means there is no "sUS_UserID" key in the `$_COOKIE` array. In other words, your cookie doesn't exist. – bfavaretto Jul 17 '13 at 22:00
  • 1
    @deceze I have mixed feelings about the Q/A you linked to. Are you aware if that was ever discussed on meta? Shouldn't it be split into separate question/answers, or moved to the tag wiki? – bfavaretto Jul 17 '13 at 22:01
  • @bfavaretto The rare double-whammy! – 000 Jul 17 '13 at 22:09

1 Answers1

1

You will need to ensure that the cookie exists and is named 'sUS_UserID'. The error message you received seems to indicate that you do not have a cookie defined with that name.

For clarity sake, you may wish to consider changing the way you interpolate the $_COOKIE data. This is not required, but may improve code clarity and syntax highlighting, and may help you to avoid difficult-to-find quoting errors.

$UserLoginQ = $DB->Query("SELECT * FROM `Users` WHERE `UserID` = '" . $_COOKIE['sUS_UserID'] . "'");

Finally, consider reading up on SQL injection vulnerabilities. Cookies are user-editable, and using unsanitized user content in SQL queries can put your data at risk.

George Cummins
  • 28,485
  • 8
  • 71
  • 90