标题位置PHP无法正常工作?

时间:2016-11-25 07:42:16

标签: php

我正在尝试使用PHP进行登录和注册系统,并且当我使用header("Location: index.php")时我不断收到错误代码,这是我得到的错误:

  

警告:无法修改标头信息 - 已在C:\ Users \ Omg中发送的标头(在C:\ Users \ Omg \ Desktop \ XAMPP \ htdocs \ Websites \ Social-Network \ register.php:43中开始输出)第141行上的\ Desktop \ XAMPP \ htdocs \ Websites \ Social-Network \ _register.php   的index.php

这是我的PHP代码:

if(!empty($_POST)) {
  if(empty($_POST['username'])){
    echo("Please enter a Display Name<br />");
  }
  if(empty($_POST['password'])){
    echo("Please enter a password<br />");
  }

  if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
    echo("Please enter a valid email address<br />");
  }

  $query = "
      SELECT 
          1
      FROM users
      WHERE
          username = :username
  ";

  $query_params = array(
    ':username' => $_POST['username']
  );
  try{
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
  }
  catch(PDOException $ex) {
    die("Failed to run query: ");
  }
  $row = $stmt->fetch();

  if($row){
    echo("This name is taken by another person<br />");
  }
  $query = "
      SELECT 
          1
      FROM users
      WHERE
          email = :email
  ";

  $query_params = array(
    ':email' => $_POST['email']
  );
  try{
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
  }
  catch(PDOException $ex) {
    echo("Failed to run query: ");
  }

    $row = $stmt->fetch();

  if($row){
    echo("This email is already taken!<br />");
  }

  $query = "
      INSERT INTO users (
        username,
        password,
        salt,
        email
      ) VALUES (
        :username,
        :password,
        :salt,
        :email
      )
  ";

  $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));

  $password = hash('sha256', $_POST['password'] . $salt);

  for($round = 0; $round < 65536; $round++) {
    $password = hash('sha256', $password . $salt);
  }

  $query_params = array(
    ':username' => $_POST['username'],
    ':password' => $password,
    ':salt' => $salt,
    ':email' => $_POST['email']
  );

  try{
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
  }catch(PDOException $ex) {
    echo("Failed to run query: ");
  }

  header("Location: index.php");



  die("index.php");

}

我正在使用一个网站来帮助我,我也有一个数据库连接,但它没有提到。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:2)

实际上错误是非常明确的:标题必须是你发送到客户端的第一件事,在正文之前。

所有这些回声都是身体的一部分。

此外,它看起来像是ajax调用和标准调用之间的混合。你究竟想要实现什么目标?