tags:

views:

123

answers:

4

I have a function(this is exactly how it appears, from the top of my file):

<?php
//dirname(getcwd());
function generate_salt()
{
    $salt = '';

    for($i = 0; $i < 19; $i++)
    {
     $salt .= chr(rand(35, 126));
    }

    return $salt;
}
...

And for some reason, I keep getting the error:

Fatal error: Cannot redeclare generate_salt() (previously declared in /Applications/MAMP/htdocs/question-air/includes/functions.php:5) in /Applications/MAMP/htdocs/question-air/includes/functions.php on line 13

I cannot figure out why or how such an error could occur. Any ideas?

+3  A: 

This errors says your function is already defined ; which can mean :

  • you have the same function defined in two files
  • or you have the same function defined in two places in the same file
  • or the file in which your function is defined is included two times (so, it seems the function is defined two times)

To help with the third point, a solution would be to use include_once instead of include when including your functions.php file -- so it cannot be included more than once.

Pascal MARTIN
but neither line 5 nor line 13 declare any function...
deftonix
Hu ; I suppose the line numbers that are indicated are not "precise" ; ; or, just as a precaution : are you sure you're looking at the "right" file ?
Pascal MARTIN
This is the only file that has a generate_salt() function.
deftonix
which means that it's probably being included more than once
Pascal MARTIN
Yes, I think that's it -- how to fix it, I don't know, as I've set this project up pretty badly. I think should probably restart.
deftonix
using include_once instead of include (and require_once instead of require) often helps, in that kind of situation : this way, it's PHP itself that makes sure a file is not included more than once
Pascal MARTIN
+1  A: 

You're probably including the file functions.php more than once.

codaddict
A: 

I'd recommend using get_included_files - as Pascal says you're either looking at the wrong file somehow or this function is already defined in a file that's been included.

require_once is also useful if the file you're attempting to include is essential.

middaparka
A: 

Since the code you've provided does not explicitly include anything, either it is being incldued twice, or (if the script is the entry point for the code) there must be a auto-prepend set up in the webserver config / php.ini or alternatively you've got a really obscure extension loaded which defines the function.

C.

symcbean