...

View Full Version : Fatal error: Cannot redeclare logincheck() (previously declared in /home/fxuup/public



fxuup
08-23-2010, 09:01 PM
Fatal error: Cannot redeclare logincheck() (previously declared in /home/fxuup/public_html/includes/functions.php:87) in /home/fxuup/public_html/functions.php on line 103


iv been having this problem in my functions.php . i just cant figure whats wrong with it.



unction logincheck(){


if (empty($_SESSION['username'])){
echo "
<SCRIPT LANGUAGE='JavaScript'>
window.location='index.php';

</script>
";
exit();
}}

////UPDATE ONLINE
$time = time() + (60 * 10);
mysql_query("UPDATE users SET online='$time' WHERE username='$username'");
///FINSH UPDATING ONLINE

function makecomma($input)
{

if(strlen($input)<=3)
{ return $input; }
$length=substr($input,0,strlen($input)-3);
$formatted_input = makecomma($length).",".substr($input,-3);
return $formatted_input;
}

Fou-Lu
08-23-2010, 09:18 PM
You cannot create a function called logincheck() in both /home/fxuup/public_html/functions.php and /home/fxuup/public_html/includes/functions.php. Given the error, /home/fxuup/public_html/functions.php is including /home/fxuup/public_html/includes/functions.php into it, and has imported any of its function definitions (such as logincheck()). PHP does not support function overloading or overriding, so you cannot actually rewrite the logincheck in /home/fxuup/public_html/functions.php. If it functions equivalently you won't need to worry about it since it already exists in the scope of /home/fxuup/public_html/functions.php once its been included in. If its different, you need to rename the logincheck in /home/fxuup/public_html/functions.php to something different and modify the accessing calls appropriately.

fxuup
08-23-2010, 09:22 PM
Alright thank you i didnt notice i had a duplicated functions.php file. but i have deleted the one i don't need. and than i still get this as well


Fatal error: Cannot redeclare logincheck() (previously declared in /home/fxuup/public_html/includes/functions.php:88) in /home/fxuup/public_html/includes/functions.php on line 96

Fou-Lu
08-23-2010, 09:28 PM
Then the same problem exists in /home/fxuup/public_html/includes/functions.php itself as well. That file has two definitions for the logincheck function. Delete the second one starting at line 96.

met
08-23-2010, 09:28 PM
it's telling you what the problem is

you have two instances of "logincheck" in that file

you could do



if(!function_exists('logincheck'))
{
function logincheck()
{
/* code */
}
}


as a prevention

also note if you're using this within a class you should use method_exists as, a function is a method in OOP.

fxuup
08-23-2010, 10:01 PM
so somthing like this?


if(!function_exists('logincheck'))
{
function logincheck()
{
if (empty($_SESSION['username'])){
echo "
<SCRIPT LANGUAGE='JavaScript'>
window.location='index.php';

</script>
";
exit();
}
}

Fou-Lu
08-23-2010, 10:08 PM
That works yes, but what it means is that if the logincheck() function has previously been declared, this one will not overwrite it (what you have here will not be called when logincheck() is called).

There are only two ways to redeclare functions in PHP. The first is by using PHP5.3+ namespaces to create different functions. These are then called by customNamespaceName\sameNamedFunc();, so its a semi-override/overload, but not a real one, and the other way is adding in the advanced PHP debugger which allows you to call override_function and rename_function. This is exceedingly handy when using debugging functionality, but should not be used for production work since APD is not a common installation.

Class methods can be overridden, but still not overloaded (late static binding is a new feature too... 5.3 I think is when it came into effect?). Using polymorphism you can switch at runtime the called method based on the object present.

fxuup
08-23-2010, 10:11 PM
Sorry im fairly new to php coding. Could you show me a example?
But when i put that code in i get

Parse error: syntax error, unexpected $end in /home/fxuup/public_html/includes/functions.php on line 838


But line 838 in the last line of the functions.php file.



?>


Thats all it contains.

met
08-23-2010, 10:17 PM
line numbers are only approximate

that error means that your curly bracers are unbalanced

correct formatting helps with this considerably:



<?php
if(!function_exists('logincheck'))
{
function logincheck()
{
if (empty($_SESSION['username']))
{
echo "<SCRIPT LANGUAGE='JavaScript'>window.location='index.php';</script>";
exit();
}
}
}
?>

Fou-Lu
08-23-2010, 10:20 PM
Can't believe I missed that >.<
Also, don't bother with a Javascript redirection, just use a header('Location: index.php'); instead. The exit/die function is still required when using a header otherwise it will continue to process the remaining page.

fxuup
08-23-2010, 10:23 PM
Alright well that work perfect but now i got another error.

Fatal error: Cannot redeclare makecomma() (previously declared in /home/fxuup/public_html/includes/functions.php:105) in /home/fxuup/public_html/includes/functions.php on line 113


function makecomma($input)
{

if(strlen($input)<=3)
{ return $input; }
$length=substr($input,0,strlen($input)-3);
$formatted_input = makecomma($length).",".substr($input,-3);
return $formatted_input;
}

met
08-23-2010, 10:31 PM
there's being new to something and there's being just plain stupid

it's the same error as the previous one, work it out

fxuup
08-23-2010, 10:38 PM
i can't work this out im not the greatest.

i got this so far


if(!function_exists('makecomma'))
{
function makecomma($input)
{
if(strlen($input)<=3)
{ return $input; }
$length=substr($input,0,strlen($input)-3);
$formatted_input = makecomma($length).",".substr($input,-3);
return $formatted_input;
}
}

Nevermind. Im gett the hang of functions now. Thanks guys for all the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum