473,803 Members | 4,391 Online
Bytes | Software Development & Data Engineering Community
+ 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><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.
Jun 25 '07 #1
8 10554
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.p hp");[/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.p hp");[/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><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:
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
5929
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
47
3887
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
19
4267
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 char* formatter, ...); Then, I want to call it as so:
44
2442
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 statement: void print_structs(void * struct_argument, enum struct_type stype) { switch(stype) { case STRUCT_TYPE_1:
5
3446
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
11
3366
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 manipulation routines in my Class: ******************************************************************************* imports System NameSpace MyFunctions
8
2325
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 types. The problem: I have a number of mixed-mode functions which I want reuse. These functions revolve around converting a CLR String to a C++ std::string or
13
1735
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 usage of PHP Classes rather than an actual technical guide? I've seen loads of things that show how to put together a class, but without actually necessarily saying why you'd want to use a class over say a separate file of functions or explaining:
38
2669
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: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html Unfortunately it does not work. Here is what I'm doing: -----------------------------------------
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10289
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5496
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.