douwei1904 2015-11-19 11:41
浏览 32
已采纳

当用户登录[重复]时,PHP不会设置cookie

This question already has an answer here:

I'm trying to write a simple login page for PHP which sets a cookie with the email address value when the user logs in.

setcookie() doesn't seem to be working though... and I do not know why.

Here's the login form:

<?php include 'header.html' ?>
  <div id="maincontent">

    <h3>Please enter your login information below</h3>

    <FORM method="post" action="login.php">

      Salesperson Email: <INPUT TYPE="TEXT" name="email" size="20">
      Password: <INPUT TYPE="PASSWORD" name="password" size="20">
      <INPUT TYPE="submit" name="submit" value="Login">

    </form>
    <?php print_r($_COOKIE); ?>
  </div>
<?php include 'footer.html' ?>

And here's the login file:

<?php
  $username = "root";
  $password = "root";
  $hostname = "localhost";
  //connection to the database
  $conn = @mysql_connect($hostname, $username, $password)
    or die("Unable to connect to MySQL");
  echo "Connected to MySQL<br>";
  if (mysql_select_db("w_c_a", $conn)) {
    echo ("<p>Database selection successful</p>");
  }
  else {
    die ("Could not locate w_c_a database" .mysql_error());
  }
  // Select salesperson that matches both username and password
  $sql = "SELECT * FROM salesperson WHERE email= '$_POST[email]' AND password = '$_POST[password]'";
  $result = mysql_query($sql,$conn) or die(mysql_error());
  // if there's a value returned, assign the values to variables.
  if (mysql_num_rows($result) == 1){
    $name = mysql_result($result, 0, 'name');
    $email = mysql_result($result, 0, 'email');
    $id = mysql_result($result, 0, 'salesperson');
    // set a cookie with the email as a value which expires in a day
    setcookie('logged_in', $email, time() + (86400 * 30));
    //spit out cookie value
    echo $_COOKIE['logged_in'];
  } else {
      echo ("<p>Incorrect username or password</p>");
      echo ("<p>Please try <a href=employee_login.php>logging in again</a></p>");
    exit;
  }
?>

<?php include 'header.html' ?>
  <div id="maincontent">

    <?php
    print_r($_COOKIE);
    echo ("name" . $name . "email" . $email . "id" . $id . "<br>")
    ?>
  </div>
<?php include 'footer.html' ?>

From what I can gather searching around, setting the cookie with the email value should be fine. Nothing is displaying though, even when I try to set it with dummy data.

I've tried clicking through to the next page etc. No dice.

I have the print_r($_COOKIE) value down the bottom to check all cookies.

What am I missing?

</div>
  • 写回答

2条回答 默认 最新

  • drzablspw01655860 2015-11-19 11:47
    关注

    Looks like you're printing successful data connection before calling setcookie which works by sending Set-Cookie header. You can't send headers after setting/sending/printing any content. Try removing those 2 echo lines

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog