gpt4 book ai didi

php - mysql_connect 正在设置 php header

转载 作者:行者123 更新时间:2023-11-29 22:50:44 29 4
gpt4 key购买 nike

我的 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com