Results 1 to 3 of 3

Thread: "Globalizing" functions in PHP...HOW TO ?

  1. #1
    Join Date
    Jun 2003
    Posts
    284
    Rep Power
    0

    Default "Globalizing" functions in PHP...HOW TO ?

    I have this code :
    Code:
    /*FILE 1*/
    include("authenthicate_login.inc.php");
    if( !authenthicate_login($_SERVER['PHP_AUTH_USER'],md5($_SERVER['PHP_AUTH_PW']))){
              header("Location: ../login.php");
              exit;
         }
    else
    {
              <table> 
                <td style="font-size: 10px">
                 
                  <?php include("submit_form.inc.php") ?>
    
                </td>
    
                 <td>
                  <?php include("query.inc.php")?>
                 </td>
    
                 <td>
                  <?php include("select_query.inc.php") ?>
                 </td>
    
                </tbody>
               </table>

    Now to prevent people from going directly to the "submit_form.inc.php" file and the other files (without logging in) I start the ("submit_form.inc.php") code with the following:

    Code:
    /*FILE 2 submit_form.inc.php*/
    include("authenthicate_login.inc.php");
    if( !authenthicate_login($_SERVER['PHP_AUTH_USER'],md5($_SERVER['PHP_AUTH_PW']))){
              header("Location: login.php");
              exit;
    }
    else
    {
        /** LOAD PAGE, BECAUSE THE USER AS LOGGED IN **/
    }


    NOTE : that the

    But I keep getting this error :



    Fatal error: Cannot redeclare authenthicate_login() (previously declared in c:\apache\htdocs\folder\subfolder\authenthicate_lo gin.inc.php:25) in c:\apache\htdocs\folder\subfolder\authenthicate_lo gin.inc.php on line 25

    On line 25 I have ;
    function authenthicate_login($user_name_entered,$hash_of_pa ssword_entered){
    .
    .
    .
    }

    I think the error is pretty self explanatory....but how do I get around this error....? Is there a way to "globalize" functions and use them thoughout my various scripts without having to include them ?

    If I remove the include from FILE 2 (i.e submit_form.inc.php) I will be able to go ahead (i.e assuming that I went through the normal login process)...however the user will B able to simply enter the address of this page (i.e submit_form.inc.php) and get there with no authenthication....they will however get this error !


    Fatal error: Call to undefined function: authenthicate_login() in c:\apache\htdocs\folder\subfolder\submit_form.inc. php on line 72

    On line 72 (as expected) I have :

    if( !authenthicate_login($_SERVER['PHP_AUTH_USER'],md5($_SERVER['PHP_AUTH_PW']))){
    header("Location: login.php");
    exit;
    }
    ﻈļŭώŭļƒ

  2. #2
    Join Date
    Apr 2003
    Posts
    1,389
    Rep Power
    0

    Default Re: "Globalizing" functions in PHP...HOW TO ?

    hmm... your error seems like it would only occur if both files have the function defined in them... 2 ways to fix this:

    1 - you CAN declare globals.. but it drops security in your scripts.

    2 - The safer way, and the standard way, is to create all your funcs and special classes in one file.. then include them in your header.

    That way you'd include say, config.php OR functions.php which would have all your functions you want to globalize.. then set a
    Code:
    require ("functions.php");
    now if you have a header file you only need to require it in the header and so when all your other files call the header you also call the require.
    The sword of the Spirit is a mighty 2-edged blade || Re-arranging my life for Christ!

  3. #3
    Join Date
    Jun 2003
    Posts
    284
    Rep Power
    0

    Default Re: "Globalizing" functions in PHP...HOW TO ?

    I have kinda given up on the approach I wuz using....but now I will have to go take a look into the way U suggested...thanx
    ﻈļŭώŭļƒ

Posting Permissions

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