3

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(); 

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(); 

Unfortunately, my jQuery function replies with this:

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

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.

Any input is appreciated.

Note: I have removed the include function in both files. If I do, PHP throws another error: "sec_session_start()" is not defined."

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); } }); }); 
14
  • what is line 14 and what is line 24? Commented Jun 29, 2013 at 4:53
  • Are you sure you used include_once and not include? Commented Jun 29, 2013 at 4:54
  • 1
    @YogeshSuthar Yep...That was my first thought - but it didn't work. I don't get it, why does it believe that I am wanting to redeclare the function? Commented Jun 29, 2013 at 4:54
  • @Manatax Line 14 is specifically $session_name = 'sec_session_id';. Directly before that is the start of the function. Line 24 is session_regenerate_id();. That is right before the end of the function. Commented Jun 29, 2013 at 4:56
  • @Mlagma Remove the includes from jquery called function and try. If didn't work try vice-versa. Commented Jun 29, 2013 at 5:00

1 Answer 1

4

The key is this error message:

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

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.

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'); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.