-2

I am creating my login page and when I loaded it up I seemed to get this error saying:

Fatal error: Cannot redeclare email() (previously declared in /home/a4625512/public_html/core/functions/general.php:2) in /home/a4625512/public_html/core/functions/general.php on line 4

My host is 000WebHost so I don't know if it has something to do with them, and I have searched online everywhere for it.

general.php code:

function email($to, $subject, $body) {
    mail($to, $subject, $body, 'From: hello@points4u.com');
} 

Login Code:

include 'core/init.php';

if(empty($_POST) === false){
    $username = $_POST['username'];
    $password = $_POST['password'];
    if(empty($username) === true || empty($password) === true){
        $errors[] = 'You need to enter a username and password.';
    }else if(user_exists($username) === false){
        $errors[] = 'We cant find that username. Have you registered?';
    }else if(user_active($username) === false){
        $errors[] = 'You have not activated your account!';
    }else{
        $login = login($username, $password);
        if($login === false){
            $errors[] = 'That username/password is incorrect.';
        }else{
            $_SESSION['user_id'] = $login;
           header('location: index.php');
            exit();
        }
    }
    print_r($errors);
}

Eli Stone
  • 1,435
  • 3
  • 32
  • 56
  • The error message says it all. You cannot redeclare... – B001ᛦ Jan 20 '16 at 16:29
  • I do not know how to fix this – Brandon Russell Jan 20 '16 at 16:38
  • There are a few solutions; you could change the name of the function, or use namespaces to stop the confliction or since 'email' isn't reserved so you should be able to use it you can hunt down where the function `email()` has previously been declared (hint: `core/functions/general.php:2`) and remove it if it isn't in use – Mikey Jan 20 '16 at 16:41

1 Answers1

1

So to clarify what I commented:

The initial part of the error you should be looking at is: Fatal error: Cannot redeclare email()

This error is telling you that your function email() has previously been declared somewhere else, if you look at the rest of the error it is telling you exactly where you should start looking:

/public_html/core/functions/general.php:2

/public_html/core/functions/general.php on line 4

So your next steps should be to go to these files and find out why you declared the function name previously.

Some solutions:

Change the name of your email() function to something that hasn't previously been used. i.e. sendEmail(). This is a quick fix, but really should look into why you declared the function before, and whether you could perhaps use that one instead if it is doing the same thing.

You could utilise a namespace to differentiate the email function from your others by putting something like: namepace Example; at the top of your file, although just doing this as a one off to fix the issue is a little hacky and I wouldn't recommend it.

Or you could (and should) look why you declared it previously and decide what the best option forward is from there.

  • Could you use that function instead?
  • Is that function not being used? Could you remove it?

Bunch of stuff you can do really. To summarise the issue is that you have two function with the exact same name so when you attempt to use them PHP doesn't know which function you want to use and throws back the error.

Community
  • 1
  • 1
Mikey
  • 2,231
  • 1
  • 10
  • 19