Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Functions, Calling function in another page.

eragon's Avatar
Needs Regular Fix
 
Join Date: Mar 2007
Location: US
Posts: 428
#1: Jun 25 '07
-------- 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><head><title>Login</title></head><body>') ;

if(!isset($_SESSION['userid']))
{

if(!isset($_POST['username']) || !isset($_POST['password']))
{
echo('please login. <from method="post">Username: <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>window.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's Avatar
Moderator
 
Join Date: May 2007
Location: Canada
Posts: 1,313
#2: Jun 25 '07

re: PHP Functions, Calling function in another page.


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.php");[/PHP]

good luck
eragon's Avatar
Needs Regular Fix
 
Join Date: Mar 2007
Location: US
Posts: 428
#3: Jun 25 '07

re: PHP Functions, Calling function in another page.


Quote:

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.php");[/PHP]

good luck

omg! i did that almost a hundred times! and all hundred times i used ' instead of ". Thanks much man!
eragon's Avatar
Needs Regular Fix
 
Join Date: Mar 2007
Location: US
Posts: 428
#4: Jun 25 '07

re: PHP Functions, Calling function in another page.


and on line 23 i forgot to put echo... oops, error on my part.
eragon's Avatar
Needs Regular Fix
 
Join Date: Mar 2007
Location: US
Posts: 428
#5: Jun 25 '07

re: PHP Functions, Calling function in another page.


oh, um, it didnt work, its echoing the contents of the file into the page...
eragon's Avatar
Needs Regular Fix
 
Join Date: Mar 2007
Location: US
Posts: 428
#6: Jun 25 '07

re: PHP Functions, Calling function in another page.


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.
eragon's Avatar
Needs Regular Fix
 
Join Date: Mar 2007
Location: US
Posts: 428
#7: Jun 25 '07

re: PHP Functions, Calling function in another page.


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><head><title>Login</title></head><body>') ;

if(!isset($_SESSION['userid']))
{

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

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

browser result after submitting form:
Expand|Select|Wrap|Line Numbers
  1. Fatal error: Call to undefined function authenticate() in login.php on line 14
  2.  
what am i doing wrong?
Expert
 
Join Date: May 2007
Posts: 213
#8: Jun 25 '07

re: PHP Functions, Calling function in another page.


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?
eragon's Avatar
Needs Regular Fix
 
Join Date: Mar 2007
Location: US
Posts: 428
#9: Jun 25 '07

re: PHP Functions, Calling function in another page.


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