php - mysql_connect 正在设置 php header

标签 php mysql http-headers

我的 PHP 脚本最终将插入到 MySQL 数据库中,到目前为止,我已经验证了变量,并且出于某种原因,使用 header() 将用户重定向回 addserver.php 会报告以下错误:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/includes/mysql.php:2) in /Applications/MAMP/htdocs/scripts/addserver.php on line 61

这是addserver.php的内容(位于/scripts/addserver.php,不要与HTML表单页面addserver.php混淆)

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
session_start();
// must be logged in
if(!isset($_SESSION['loggedin'])) {
    header('location: /login.php?from=/addserver.php');
    exit();
}

// initialise errors array
$errors = array();

// connect to mysql
require('../includes/mysql.php');

// check name
if(empty($_POST['name'])) {
    array_push($errors, 'The server name field is required');
}
if(!strlen($_POST['name'])>=5 || !strlen($_POST['name'])<=30) {
    array_push($errors, 'Server name must be 5-30 characters long');
}
// check ip
if(empty($_POST['ip'])) {
    array_push($errors, 'IP address field is required');
}
if(!filter_var($_POST['ip'], FILTER_VALIDATE_IP) && !preg_match("/^([-a-z0-9]{2,100})\.([a-z\.]{2,8})$/i", $_POST['ip'])) {
    array_push($errors, 'IP address must be in valid x.x.x.x format and submitted without port.');
}
$ip = strip_tags(mysql_real_escape_string($_POST['ip']));
if(mysql_num_rows(mysql_query("SELECT id FROM servers WHERE ip='$ip'"))) {
    array_push($errors, 'Server IP has already been registered');
}
// check port
if(empty($_POST['port'])) {
    array_push($errors, 'Port is a required field');
}
if(!$_POST['port']>=1024 || !$_POST['port']<=65535) {
    array_push($errors, 'Port number must be 1024-65535');
}
if(!is_integer(intval($_POST['port']))) {
    array_push($errors, 'Port must be an integer (whole number)');
}
// check website link (not required)
if(!empty($_POST['website'])) {
    if(!filter_var($_POST['website'], FILTER_VALIDATE_URL)===false) {
        // try adding the http on
        $website = 'http://'.$_POST['website'];
        // try validation again
        if(!filter_var($website, FILTER_VALIDATE_URL)===false) {
            array_push($errors, 'Website URL must be valid');
        }
    }
}
// do banner upload later

// if errors are set, send back with errors array as a session var.
if(count($errors)!=1) {
    $_SESSION['adderrors'] = $errors;
    header('location: /addserver.php');
    // end script
    exit('Some variables were invalid.');
}
?>

这是mysql.php的内容:

<?php
mysql_connect('localhost','root','root');
mysql_select_db('findmcservers');
?>

最佳答案

尝试删除这些 php 文件中的结束 php 标记 (?>)。我见过一些房东以不同的方式对待他们。

关于php - mysql_connect 正在设置 php header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28916993/

相关文章:

java - 开发 JDBC 驱动程序

sql - 获取 SQL 查询行数的最便宜的方法是什么?

apache - 俄语搜索中毒?返回哪个 HTTP header ?

php - 完全在服务器端运行 PHP 脚本

php - 如何从 PDO 对象打印 SQL 查询的结果

python - 尝试访问 MySQL 时 AWS Python Lambda 超时

silverlight - SharePoint 2010/IIS 7.5 字节范围请求响应整个文件

javascript - 使用基于id的Angular JS合并数组中的多个对象

php - 使用 PDO 获取行和计数

delphi - 如何通过 idHTTP 获取(即读取)https 响应。通常是 REST 授权