Page 1 of 2 12 LastLast
Results 1 to 10 of 17
Like Tree6Likes

Thread: Cannot Redeclare Function() (previously declared in......) in ....... on line 15

  1. #1
    New Registered User Shahlin's Avatar
    Join Date
    Aug 2011
    Posts
    35

    Default Cannot Redeclare Function() (previously declared in......) in ....... on line 15 13 Jan 2012 @ 11.22

    Fatal error: Cannot redeclare loggedin() (previously declared in C:\xampp\htdocs\php\login\core.php:10) in C:\xampp\htdocs\php\login\core.php on line 15

    This error always comes up! Please help!! This is my index page. I Already made a function in 'core.php' and the functions name is 'loggedin'.

    PHP Code:
    <?phperror_reporting (E_ALL E_NOTICE) ;require 'config.php;require 'core.php;echo '<h1>Index</h1>' ;
    if (
    loggedin()) {$firstname getuserfield ('firstname') ;$surname getuserfield ('surname') ;    echo '<u>Homepage</u><br><br>You are logged in as ' .$firstname.' '.$surname.'!<br><br><a href="profile.php">Go To Your Profile!</a><br><br><br><a href="logged_out.php">Logout</a>' ;} else {include 'loginform.php;}
    ?>
      Reply With Quote

  2. #2
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 13 Jan 2012 @ 12.09

    Apparently you have declared a function called "loggedin" twice. You can't have two functions with the same name (in the same namespace).
      Reply With Quote

  3. #3
    New Registered User Shahlin's Avatar
    Join Date
    Aug 2011
    Posts
    35

    Default 13 Jan 2012 @ 12.15

    I didn't us it twice!
    I made a file called 'core.php' where all the functions are there.
    and I included the file (core.php) in the index page and used the function.
      Reply With Quote

  4. #4
    Administrator Frinkky's Avatar
    Join Date
    Dec 2008
    Posts
    1,817
    Blog Entries
    1

    Default 13 Jan 2012 @ 12.24

    Double-check you're only including core.php once then. Perhaps include_once() or require_once() might solve the issue.
      Reply With Quote

  5. #5
    New Registered User Shahlin's Avatar
    Join Date
    Aug 2011
    Posts
    35

    Default 13 Jan 2012 @ 12.57

    I Tried! But didn't work!
      Reply With Quote

  6. #6
    Administrator Frinkky's Avatar
    Join Date
    Dec 2008
    Posts
    1,817
    Blog Entries
    1

    Default 13 Jan 2012 @ 13.07

    Rather than us trying to guess what the problem is, why don't you post the contents of core.php and also the php from your index file.
      Reply With Quote

  7. #7
    New Registered User Shahlin's Avatar
    Join Date
    Aug 2011
    Posts
    35

    Default 13 Jan 2012 @ 13.13

    This is my core.php
    PHP Code:
    <?php
    ob_start
    ();
    session_start();
    $current_file $_SERVER['SCRIPT_NAME'];
    function 
    loggedin() {
        if (isset(
    $_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
            return 
    true;
        } else {
            return 
    false;
        }
    }
    function 
    getuserfield($field) {
        
    $query "SELECT `$field` FROM `users` WHERE `id`='".$_SESSION['user_id']."'";
        if (
    $query_run mysql_query($query)) {
            if (
    $query_result mysql_result($query_run0$field)) {
                return 
    $query_result;
            }
        }
    }
    ?>
      Reply With Quote

  8. #8
    New Registered User Shahlin's Avatar
    Join Date
    Aug 2011
    Posts
    35

    Default 13 Jan 2012 @ 13.21

    This is my index.php
    PHP Code:
    <?php
    require 'core.php';
    require 
    'config.php';
    if (
    loggedin()) {
        
    $firstname getuserfield('firstname');
        
    $surname getuserfield('surname');
        echo 
    'You are logged in, '.$firstname.' '.$surname.'!<br><br><a href="logout.php">Logout</a>';
    } else {
        include 
    'loginform.php';
    }
    ?>
      Reply With Quote

  9. #9
    Administrator Frinkky's Avatar
    Join Date
    Dec 2008
    Posts
    1,817
    Blog Entries
    1

    Default 13 Jan 2012 @ 13.54

    Is core.php being called in any of the other files or loggedin() being defined in any other file (config.php, logout.php, loginform.php)?

    Is this all custom code or are you extending a framework or cms that might also have a similar function?

    A quick google search suggested wrapping the following around your loggedin() function:
    PHP Code:
    if(!function_exists('loggedin')) {


    I can't think of anything else at this time - Janvt is the php guru around here, he might be able to suggest something else.
      Reply With Quote

  10. #10
    New Registered User Shahlin's Avatar
    Join Date
    Aug 2011
    Posts
    35

    Default 13 Jan 2012 @ 13.57

    Its used in 'loginform.php'.
    Its all custom codes.

    Thanks! I'll try that.
      Reply With Quote

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •