473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PHP Functions, Calling function in another page.

eragon
431 Contributor
-------- 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.
Jun 25 '07 #1
8 10534
epots9
1,351 Recognized Expert Top Contributor
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
Jun 25 '07 #2
eragon
431 Contributor
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!
Jun 25 '07 #3
eragon
431 Contributor
and on line 23 i forgot to put echo... oops, error on my part.
Jun 25 '07 #4
eragon
431 Contributor
oh, um, it didnt work, its echoing the contents of the file into the page...
Jun 25 '07 #5
eragon
431 Contributor
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.
Jun 25 '07 #6
eragon
431 Contributor
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?
Jun 25 '07 #7
improvcornartist
303 Recognized Expert Contributor
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?
Jun 25 '07 #8
eragon
431 Contributor
that would help, wont it? ok, i got it working, thanks for the help.
Jun 25 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

99
5812
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
47
3795
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
19
4218
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
44
2373
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
5
3392
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
11
3333
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
8
2292
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
13
1705
by: Simon Dean | last post by:
Hi, I have a couple of questions. If you don't mind. Sorry, I do get a bit wordy at times. First one just throws some thoughts around hoping for answers :-) 1) Anyone got a theory on the...
38
2625
by: abasili | last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from a C program. Since Google is my friend I've ended up to this link which seems very clear: ...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.