PHP Functions, Calling function in another page.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eragon
    Contributor
    • Mar 2007
    • 431

    #1

    PHP Functions, Calling function in another page.

    -------- This question has not been fully answered. Please help me! --------

    Hello. I have a php login page that calls a function to determine if the username and password is correct. it is working great, but now i want to move the function, called authenticate, to an external file, so i can have one place to store my usernames and passwords. i will post the script i am using:

    [PHP]<?php

    session_start() ;

    echo('<html><he ad><title>Login </title></head><body>') ;

    if(!isset($_SES SION['userid']))
    {

    if(!isset($_POS T['username']) || !isset($_POST['password']))
    {
    echo('please login. <from method="post">U sername: <input type="text" name="username" /> Pasword: <input type="password" name="password" /><input type="submit" value="Login" /></form>');
    }
    else if(authenticate ($_POST['username'], $_POST['password']))
    {
    $_SESSION['userid'] = $_POST['username'];

    echo('<script>w indow.location= "thispage.php"; </script>');
    }
    }
    else
    {
    You have logged in.
    }
    //this is the finction with the usernames and passwords...
    function authenticate($u , $p)
    {
    if($u == "admin" && $p == "password") return true;
    if($u == "guest" && $p == "fish") return true;
    return false;
    }

    echo('</body></html>');
    ?>[/PHP]

    i want to get the function above seperated into another file, so if i use this login script on another page i dont need to go and update the usernames in EVERY page. Your help is greatly appreciated.
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    put the code u want in a separate php file, then in all other php files that use the same function, use:
    [PHP]include("file.p hp");[/PHP]

    good luck

    Comment

    • eragon
      Contributor
      • Mar 2007
      • 431

      #3
      Originally posted by epots9
      put the code u want in a separate php file, then in all other php files that use the same function, use:
      [PHP]include("file.p hp");[/PHP]

      good luck
      omg! i did that almost a hundred times! and all hundred times i used ' instead of ". Thanks much man!

      Comment

      • eragon
        Contributor
        • Mar 2007
        • 431

        #4
        and on line 23 i forgot to put echo... oops, error on my part.

        Comment

        • eragon
          Contributor
          • Mar 2007
          • 431

          #5
          oh, um, it didnt work, its echoing the contents of the file into the page...

          Comment

          • eragon
            Contributor
            • Mar 2007
            • 431

            #6
            ok, i got some of it working, at least the usernames and passwords arent printed right there on the page... but now when i submit it says call to undefined function, when the function description is the one i put on the external page. please show me (using the origional script i posted) how my external file should look like, and how the login page should look like after externalizing the function.

            Comment

            • eragon
              Contributor
              • Mar 2007
              • 431

              #7
              external file 1: psw.php
              [PHP]<?php
              //this is the finction with the usernames and passwords...
              function authenticate($u , $p)
              {
              if($u == "admin" && $p == "password") return true;
              if($u == "guest" && $p == "fish") return true;
              return false;
              }
              ?>[/PHP]

              main file 1: login.php
              [PHP]<?php

              session_start() ;

              echo('<html><he ad><title>Login </title></head><body>') ;

              if(!isset($_SES SION['userid']))
              {

              if(!isset($_POS T['username']) || !isset($_POST['password']))
              {
              include('inc/login.inc');
              }
              else if(authenticate ($_POST['username'], $_POST['password']))
              {
              $_SESSION['userid'] = $_POST['username'];

              echo('<script>w indow.location= "thispage.php"; </script>');
              }
              }
              else
              {
              echo("You have logged in.");
              }
              include("inc/psw.php");
              echo('</body></html>');
              ?>[/PHP]

              browser result after submitting form:
              Code:
              Fatal error: Call to undefined function authenticate() in login.php on line 14
              what am i doing wrong?

              Comment

              • improvcornartist
                Recognized Expert Contributor
                • May 2007
                • 303

                #8
                Does your include need to be placed before your call to the function in the include? Would it help to move include("inc/psw.php"); to the top of the file?

                Comment

                • eragon
                  Contributor
                  • Mar 2007
                  • 431

                  #9
                  that would help, wont it? ok, i got it working, thanks for the help.

                  Comment

                  Working...