PDA

View Full Version : PHP Question


Pages : [1] 2

ViN86
05-14-07, 08:53 AM
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?

evilghost
05-14-07, 09:36 AM
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.

superklye
05-14-07, 10:17 AM
I <3 includes :D

ViN you're putting your security/login session checks as an include file as well, right?

ViN86
05-14-07, 02:48 PM
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 :D that's awesome.

I <3 includes :D

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 :)

evilghost
05-14-07, 02:49 PM
Post you're code, we'd be happy to review it with you especially for security concerns/etc.

fivefeet8
05-16-07, 03:05 AM
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'); ?

supra
05-16-07, 04:57 AM
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

evilghost
05-16-07, 11:44 AM
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.

atriq
05-16-07, 01:55 PM
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.

fivefeet8
05-16-07, 02:37 PM
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.

evilghost
05-16-07, 03:06 PM
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.

tieros
05-17-07, 12:21 PM
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.

ViN86
05-17-07, 03:20 PM
ill probably end up referencing multiple functions from the same file, so ill use require() so i dont end up calling the require_once() multiple times (i agree multiple instances is a poor technique ghost)

also, what do you all use to debug your php scripts? right now im just runnin a local apache server with PHP5. but if i have errors, the code just doesnt display at all.

what can i use to debug the code and find errors? thx :)

evilghost
05-17-07, 03:24 PM
In /etc/php.ini (or your php.ini)

error_reporting = E_ALL & ~E_NOTICE
display_errors = on

ViN86
05-17-07, 03:32 PM
In /etc/php.ini (or your php.ini)

error_reporting = E_ALL & ~E_NOTICE
display_errors = on
lol... DE DE DE

should have figured i should turn error reporting on in php. thanks ghost :thumbsup:

evilghost
05-17-07, 03:33 PM
:)

fivefeet8
05-18-07, 02:22 PM
ill probably end up referencing multiple functions from the same file, so ill use require() so i dont end up calling the require_once() multiple times (i agree multiple instances is a poor technique ghost)


I think in your case, either require_once() or require() will work because you'd still only be calling one instance. Since all the functions you need will be located on a single php script. Maybe you think require_once() means you need to require the file again if you need the function again on the same page. Which isn't the case.

Require_once() simply checks to see if you've already included an instance. If you have, it will not include it again. If you haven't, it will include it. Require() does not check and will include an instance again no matter if you've already included it or not.

I only suggested require_once() because if the plan to extend into OOP programming later on, require() may cause errors if your classes reference other classes.

ViN86
05-18-07, 02:50 PM
I think in your case, either require_once() or require() will work because you'd still only be calling one instance. Since all the functions you need will be located on a single php script. Maybe you think require_once() means you need to require the file again if you need the function again on the same page. Which isn't the case.

Require_once() simply checks to see if you've already included an instance. If you have, it will not include it again. If you haven't, it will include it. Require() does not check and will include an instance again no matter if you've already included it or not.

I only suggested require_once() because if the plan to extend into OOP programming later on, require() may cause errors if your classes reference other classes.
oh ok. yes i misunderstood the purpose of require_once()

ViN86
05-21-07, 04:03 PM
what do you guys use to manage your MySQL database on a remote server?

also, what is the default username and password? do i have to email the host to get this info? or is it probably just the account info?

supra
05-21-07, 09:08 PM
what do you guys use to manage your MySQL database on a remote server?


i use phpmyadmin

evilghost
05-22-07, 12:57 AM
what do you guys use to manage your MySQL database on a remote server?

also, what is the default username and password? do i have to email the host to get this info? or is it probably just the account info?

SSH + /usr/bin/mysql

SLippe
05-22-07, 04:33 AM
Holy crap you all scare me! LOL I've been wanting to add a forum to my site for years now, but the whole PHP MySQL thing scares me and I haven't the slightest idea about it. If someone would like to help me...evilghost...I would really appreciate it! :D Sorry about the hijack.

ViN86
05-22-07, 08:49 AM
Holy crap you all scare me! LOL I've been wanting to add a forum to my site for years now, but the whole PHP MySQL thing scares me and I haven't the slightest idea about it. If someone would like to help me...evilghost...I would really appreciate it! :D Sorry about the hijack.
http://upload.wikimedia.org/wikipedia/commons/thumb/c/c0/Pirate_Flag_of_Rack_Rackham.svg/744px-Pirate_Flag_of_Rack_Rackham.svg.png

:rofl

but seriously, ill keep posting questions in here as they come up. Slippe, go out and buy a Apache/PHP/MySQL book and just start reading. if you have any program experience (C, C++, even VB) it will be extremely easy :thumbsup:

ViN86
05-22-07, 09:49 AM
i use phpmyadmin
how does phpmyadmin prevent others from accessing it?

cant they just access the phpmyadmin and mess with the database? or is there a login or something? :confused:

ive been lookin into it. it's pretty powerful and functional. i like it.

t3hl33td4rg0n
05-22-07, 10:30 AM
how does phpmyadmin prevent others from accessing it?

cant they just access the phpmyadmin and mess with the database? or is there a login or something? :confused:

ive been lookin into it. it's pretty powerful and functional. i like it.

phpMyAdmin is a godsend to MySQL admins!

To Slippe: I can give you a hand with setting up a forum if your server supports PHP/MySQL. Its actually pretty easy.