Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 05-14-07, 07:53 AM   #1
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default PHP Question

im getting into php and plan to use it for managing a MySQL database and some other things. (adding permissions, login control, email mailing list, activity log, etc.)

im looking to make a standard function for adding entries to the database. i shouldnt have a problem creating the function.

however, is it possible for me create the function in a location and then access it from other .php files, or do i have to write the function into each php file? the latter sounds like a waste of space as well as extra time spent processing the page.

anyone know how to reference functions in another .php file?
ViN86 is offline   Reply With Quote
Old 05-14-07, 08:36 AM   #2
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: PHP Question

Sure, simply create a separate page called "functions.php" and on each PHP page where you require the use of the function issue the following:

require("functions.php");

It's really that easy.

Remember, any time you pass a variable to MySQL make sure you mysql_escape_string() first to avoid the possibility of SQL injection attacks.
evilghost is offline   Reply With Quote
Old 05-14-07, 09:17 AM   #3
superklye
 
Join Date: Jul 2004
Location: MKE
Posts: 13,629
Default Re: PHP Question

I <3 includes

ViN you're putting your security/login session checks as an include file as well, right?
superklye is offline   Reply With Quote
Old 05-14-07, 01:48 PM   #4
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Re: PHP Question

Quote:
Originally Posted by evilghost
Sure, simply create a separate page called "functions.php" and on each PHP page where you require the use of the function issue the following:

require("functions.php");

It's really that easy.

Remember, any time you pass a variable to MySQL make sure you mysql_escape_string() first to avoid the possibility of SQL injection attacks.
thx that's awesome.

Quote:
Originally Posted by superklye
I <3 includes

ViN you're putting your security/login session checks as an include file as well, right?
well, i havent really learned all that yet and havent written the database nor the SELECT/INSERT/UPDATE/DELETE statements/functions i need yet.

ill be back here when i need help with preventing SQL injection attacks. thx guys
ViN86 is offline   Reply With Quote
Old 05-14-07, 01:49 PM   #5
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: PHP Question

Post you're code, we'd be happy to review it with you especially for security concerns/etc.
evilghost is offline   Reply With Quote
Old 05-16-07, 02:05 AM   #6
fivefeet8
Ngemu Mod
 
Join Date: Jul 2003
Location: Fresno, CA
Posts: 1,886
Default Re: PHP Question

Quote:
Originally Posted by evilghost
Sure, simply create a separate page called "functions.php" and on each PHP page where you require the use of the function issue the following:

require("functions.php");

It's really that easy.
Isn't it better to use:

require_once('functions.php'); ?
__________________
[i7 2600k @4.4ghertz][2x4 GB DDR3 1600][EVGA GTX570 1.280GB SC][EVGA GTX460 physx][Asrock Extreme7 Gen3 Z68][2xSeagate 160 Gb SATA HD raid0][Seagate 250 GB SATA2 HD][Sony Bravia 40' 1080p LCD HDTV][NEC 3520a DVD+-DLw][Windows 7 Ultimate x64][Rosewill 1000w]
fivefeet8 is offline   Reply With Quote
Old 05-16-07, 03:57 AM   #7
supra
2JZ powered
 
supra's Avatar
 
Join Date: Dec 2004
Location: Aus
Posts: 1,321
Send a message via MSN to supra
Default Re: PHP Question

Quote:
Originally Posted by fivefeet8
Isn't it better to use:

require_once('functions.php'); ?
dont think it matters too much unless that file is referenced more then once in the script
__________________
supra is offline   Reply With Quote
Old 05-16-07, 10:44 AM   #8
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: PHP Question

Quote:
Originally Posted by supra
dont think it matters too much unless that file is referenced more then once in the script
Exactly, IMHO it would be poor coding to include the function more than once. I could see a use for require_once() in a conditional but in this case I would assume require() would be more fitting with the file inclusion occurring at the "top" of the code outside of any conditional.
evilghost is offline   Reply With Quote

Old 05-16-07, 12:55 PM   #9
atriq
Registered User
 
atriq's Avatar
 
Join Date: May 2007
Posts: 34
Default Re: PHP Question

In practice, I usually use require/include_once() just in the event the chain of them end up referencing them multiple times. Solves a lot of "function redefined" statements.
atriq is offline   Reply With Quote
Old 05-16-07, 01:37 PM   #10
fivefeet8
Ngemu Mod
 
Join Date: Jul 2003
Location: Fresno, CA
Posts: 1,886
Default Re: PHP Question

Quote:
Originally Posted by evilghost
Exactly, IMHO it would be poor coding to include the function more than once. I could see a use for require_once() in a conditional but in this case I would assume require() would be more fitting with the file inclusion occurring at the "top" of the code outside of any conditional.
I've found that once you start programming in OOP with PHP. It can get messy when multiple classes requires the same functions/classes from other php files, but you needed to use both classes on one page. For example:

All objects(classes) are defined on different php scripts:
Object 1 requires object2;
Object 3 requires object2;

Your page requires object1, object2, and object3. Using only require('xx.php') would cause errors. I guess it would depend on what you're trying to do though.
__________________
[i7 2600k @4.4ghertz][2x4 GB DDR3 1600][EVGA GTX570 1.280GB SC][EVGA GTX460 physx][Asrock Extreme7 Gen3 Z68][2xSeagate 160 Gb SATA HD raid0][Seagate 250 GB SATA2 HD][Sony Bravia 40' 1080p LCD HDTV][NEC 3520a DVD+-DLw][Windows 7 Ultimate x64][Rosewill 1000w]
fivefeet8 is offline   Reply With Quote
Old 05-16-07, 02:06 PM   #11
evilghost
Registered User
 
Join Date: Jul 2005
Posts: 3,606
Default Re: PHP Question

Quote:
Originally Posted by fivefeet8
I've found that once you start programming in OOP with PHP. It can get messy when multiple classes requires the same functions/classes from other php files, but you needed to use both classes on one page. For example:

All objects(classes) are defined on different php scripts:
Object 1 requires object2;
Object 3 requires object2;

Your page requires object1, object2, and object3. Using only require('xx.php') would cause errors. I guess it would depend on what you're trying to do though.
Good point.
evilghost is offline   Reply With Quote
Old 05-17-07, 11:21 AM   #12
tieros
Registered User
 
tieros's Avatar
 
Join Date: Jul 2002
Posts: 374
Default Re: PHP Question

If you're using PHP5, I'd suggest putting that core code into a class, and then putting the class file somewhere in your autoinclude path. That way, you don't need to require() or include() it, or worry about instantiation issues from other files.

A call like:
$sess = new SessionHandler();

will look in your autoinclude path for a file named SessionHandler.php, read it in if it's not already instantiated, and call it.

Putting the code in classes also keeps variable scoping a little cleaner plus a few other nice freebies. Using classes had a bit too much overhead in PHP4, but they are great in 5.
__________________
Athlon64 x2 3800@2400MHz eVGA 8800GTS 640MB eVGA NF44 SLI mATX mobo
OCZ 2GB DDR400 Gold XTC RAM Dual Samsung Spinpoint 250GB Sata HDDs in RAID 0
Aspire X-QPACK case w/Enermax EG425P-VE PSU Liteon Dual Layer DVD Burner Dual Dell FP 2001 20" LCD
tieros is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
One more question... soundview00 NVIDIA Linux 1 10-02-02 05:31 PM
1st post - XP Restore question Needa916 Microsoft Windows XP And Vista 2 09-12-02 05:24 PM
Newby question about nforce.... superconti NVIDIA Linux 2 09-07-02 11:46 PM
Dual boot question.. Tactics General Hardware 15 09-07-02 10:39 AM
Nvidia Driver Rpm Question Big Booger NVIDIA Linux 17 08-11-02 12:55 PM

All times are GMT -5. The time now is 05:53 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.