mysql_connect正在设置php头文件

时间:2015-03-07 16:19:06

标签: 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,不要与addserver.php混淆,这是HTML表单页面)

<?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');
?>

1 个答案:

答案 0 :(得分:0)

尝试删除这些php文件中的结束php标记(?&gt;)。我见过一些主人对待他们的方式不同。