我在注册页面上收到此错误:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/signup/index.php:60) in /Applications/XAMPP/xamppfiles/htdocs/signup/index.php on line 75
这是我的代码,您可以查看一下:
<?php
include '../core/init.php';
logged_in_redirect();
if (empty($_POST) === false) {
$required_fields = array('username', 'password', 'first_name', 'last_name', 'email');
foreach ($_POST as $key => $value) {
if(empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'All fields are required';
break 1;
}
}
if (empty($errors) === true) {
if (user_exists($_POST['username']) === true) {
$errors [] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.';
}
if (preg_match("/\\s/", $_POST['username']) == true) {
$errors[] = 'Your username must not contain any spaces';
}
if (strlen($_POST['password']) < 5) {
$errors[] = 'Your password must be at least 5 characters';
}
if (email_exists($_POST['email']) === true) {
$errors [] = 'Sorry, the email \'' . $_POST['email'] . '\' is already in use.';
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title> Sem titulo </title>
<link href="./resets.css" rel="stylesheet" type="text/css" />
<link href="./style.css" rel="stylesheet" type="text/css" />
<script src="js/ajax.js"></script>
<script src="js/main.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="wrapper">
<div class="content">
<div class="header">
<div class="logo"><a href="./index.html"><img src="./images/ogo.png" border="0" /></a></div>
<div class="login">Já tens login? <a href="../login"><span>Login </span></a></div>
</div>
<div class="intro">
<div class="title"> Junta-te a nós! </div>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
echo 'You\'ve been successfully registered';
} else {
if (empty($_POST) === false && empty($errors) === true) {
$register_data = array(
'username' => $_POST['username'],
'password' => $_POST['password'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email']
);
register_user($register_data);
header ("Location: index.php?success");
exit();
} else if (empty($errors) === false) {
echo output_errors($errors);
}
?>
<br>
<br>
<form method='post' action='' >
<input type="text" class="sign-up-input" placeholder="Primeiro Nome" name="first_name" required pattern="^[a-z,A-Z]+$+~"><br>
<input type="text" class="sign-up-input" placeholder="Último Nome" name="last_name" required pattern="^[a-z,A-Z]+$"><br>
<input type="text" class="sign-up-input" placeholder="O teu Email" name="email" required pattern="([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-z,A-Z]{2,4}))"><br>
<input type="text" class="sign-up-input" placeholder="Username" name="username" required><br>
<input type="password" class="sign-up-input" placeholder="Password" name="password" MinimumLength="6" required pattern="^[\s\S]{6,256}$"><br>
<input type="submit" value="Regista-te!" class="sign-up-button">
</form>
<?php
}
?>
<br>
</div>
</div>
</div>
<div class="footer">
<?php include '../core/include/copyright.php'; ?>
<ul>
<li><a href="">Contacto </a></li>
<li><a href="./businesses/index.html">Informação </a></li>
<li><a href="./privacy/index.html">Politica de Privacidade </a> </li>
<li><a target="_blank" href="#">Termos e Condições </a></li>
<li><a target="_blank" href="">Empregos</a></li>
</ul>
</div>
</html>
有人可以帮助我吗?提前致谢
答案 0 :(得分:3)
在输出任何其他内容之前需要设置标题。
您在发送html代码后设置标题。
移动if else语句if (empty($_POST) === false && empty($errors) === true) {
在<!DOCTYPE HTML>
之前
答案 1 :(得分:0)
输出上面提到的其他内容后,您无法修改标题信息。您可以尝试输出缓冲:http://php.net/manual/en/function.ob-start.php