为什么PHP投掷会抛出"Cannot Redeclare"(无法重新声明)?本地包含函数出错? [英] Why is PHP throwing throwing a "Cannot Redeclare" error at a local, included function?
问题描述
这是我的问题:我有一个包含本地功能的文件(VRC_Header.php).在这里:
Here's my problem: I have one file that contains a local function (VRC_Header.php). Here it is:
function sec_session_start() {
$session_name = 'sec_session_id'; //set a custom session name
$secure = false; //set to true if using https
$httponly = true; //This stops javascript being able to access the session id
ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies
$cookieParams = session_get_cookie_params(); //Gets currtent cookies params
session_set_cookie_params($cookieParams["lifetime"], $cookieParams["domain"], $secure, $httponly);
session_name($session_name); //Sets the session name to the one set above
session_start(); //Start the php session
session_regenerate_id(); //regernates the session, delete the old one
}
这很简单.我在每个公共页面上都包含此文件.例如,
It's simple enough. I include this file in every public page. For instance,
include_once('VRC_Header.php');
include_once('../Classes/VRC_MasterOO.php');
include_once('../Classes/VRC_Secure_Login.php');
//Start a secure session
sec_session_start();
我的问题发生在我的登录页面与 php
处理页面之间.通过发布 jQuery
函数进行提交.第二页包含与上面相同的代码:
My problem is occurring between my login page to a php
processing page. The submission occurs via a post jQuery
function. This second page contains code identical to the above:
include_once('VRC_Header.php');
include_once('../Classes/VRC_MasterOO.php');
include_once('../Classes/VRC_Secure_Login.php');
//Start a secure session
sec_session_start();
不幸的是,我的jQuery函数对此进行了回复:
Unfortunately, my jQuery function replies with this:
致命错误:无法在E:\ Additional Programs \ xampp \ htdocs_Vista_Ridge_System中重新声明sec_session_start()(先前在E:\ Additional Programs \ xampp \ htdocs \ Vista_Ridge_Territory_System \ PHP \ Scripts \ VRC_Header.php:14中声明)第24行的PHP \ Scripts \ VRC_Header.php
Fatal error: Cannot redeclare sec_session_start() (previously declared in E:\Additional Programs\xampp\htdocs\Vista_Ridge_Territory_System\PHP\Scripts\VRC_Header.php:14) in E:\Additional Programs\xampp\htdocs\Vista_Ridge_Territory_System\PHP\Scripts\VRC_Header.php on line 24
请注意,我的jQuery函数会给出答复-这意味着我认为问题出在第二个文件中.因此,无法重新声明".为什么会这样呢?本地函数以前没有这个问题.
Note that my jQuery function presents the reply - meaning that I believe the problem resides in the second file. Hence, the "cannot redeclare." Why is this happening? I've not had this issue before with local functions.
任何输入表示赞赏.
注意:我已经删除了两个文件中的include函数.如果这样做, PHP
会引发另一个错误:" sec_session_start()
"未定义."
Note: I have removed the include function in both files. If I do, PHP
throws another error: "sec_session_start()
" is not defined."
中间jQuery函数:
The intermediate jQuery function:
$(document).ready(function () {
$('.login-form').submit(function (e) {
e.preventDefault();
$('#reply').remove();
var formData = $(this).serialize();
$("input").prop("disabled", true);
request = $.post('VRC_LoginProcess.php', formData, loginMessage);
request.fail(function() {
$('.header').append("<span id=\"reply\">Login Failed. Please try again in a few minutes.</ span>"); });
function loginMessage(data) {
$('.header').append("<span id=\"reply\">" + data + "</ span>");
$("input").prop("disabled", false);
}
});
});
推荐答案
关键是以下错误消息:
致命错误:无法在E:\ Additional Programs \ xampp \ htdocs_Vista_Ridge_System中重新声明sec_session_start()(先前在E:\ Additional Programs \ xampp \ htdocs \ Vista_Ridge_Territory_System \ PHP \ Scripts \ VRC_Header.php:14中声明)第24行的PHP \ Scripts \ VRC_Header.php
Fatal error: Cannot redeclare sec_session_start() (previously declared in E:\Additional Programs\xampp\htdocs\Vista_Ridge_Territory_System\PHP\Scripts\VRC_Header.php:14) in E:\Additional Programs\xampp\htdocs\Vista_Ridge_Territory_System\PHP\Scripts\VRC_Header.php on line 24
发生此错误的唯一方法是,如果您两次包含 VRC_Header.php
.这与jQuery无关,与会话或重新生成会话ID无关.两个不同的行号只是指向函数的开始/结束.
The only way for this error to occur is if you have included VRC_Header.php
twice. This has nothing to do with jQuery, and nothing to do with sessions or regenerating session ids. The two different line numbers are just pointing to the start/end of the function.
尝试在函数上方放置一个 echo()
命令,您应该看到回声发生了两次.更好的是,将以下代码放在函数上方,它将显示一条更有用的错误消息,包括回溯,可以帮助您第二次查看如何包含文件:
Try putting an echo()
command above the function, and you should see the echo happening twice. Even better, put the following code above your function, and it'll display a more useful error message, including a backtrace that will help you see how the file is being included a second time:
if (function_exists('sec_session_start')) {
debug_print_backtrace();
die('Trying to define sec_session_start() a second time');
}
这篇关于为什么PHP投掷会抛出"Cannot Redeclare"(无法重新声明)?本地包含函数出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!