我该如何解决? cookie没有设置

时间:2019-05-07 22:57:57

标签: php sql setcookie

我正在尝试将用户登录为cookie值时保存用户名。无论用户是否登录,我都需要此cookie来更改菜单选项。

$cookie_name = "user";
$cookie_value = $results;
setcookie($cookie_name, $cookie_value);

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($conn, $_POST['username']);
  $password = mysqli_real_escape_string($conn, $_POST['password']);

if (empty($username)) {
  array_push($errors, "Username is required");
  }
if (empty($password)) {
array_push($errors, "Password is required");
}

if (count($errors) == 0) {
  $password = md5($password);
  $query = "SELECT * FROM tblmember WHERE Username='$username' AND Password='$password'";
  $results = mysqli_query($conn, $query);
if (mysqli_num_rows($results) == 1) {
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    //set username as a cookie & then use that to access the user pages.
      echo "Cookie '" . $cookie_name . "' is set!<br>";
      echo "Value is: " . $_COOKIE[$cookie_name];
    header('location: profile.php');
   }else {
      array_push($errors, "Wrong username/password combination");
      echo "Cookie named '" . $cookie_name . "' is not set!";
    }
  }
}

我希望将cookie设置为登录的用户名。

0 个答案:

没有答案