Close connections on every page?

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Close connections on every page?

Post by Citizen »

Should I mysql_close($dbh); on every page that I connect to my database on? Should I even close it on pages that use sessions? What is the effect?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This is a security question?

It's not absolutely needed for the most part. However, if you're running into connection limit issues, it's best to add them immediately after the last database dependant call is made to make sure connections are open as short as possible.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Your database connections are automatically closed by PHP after execution of the script. So the only effect not closing them would have is that your connections would be open microseconds longer. Not really a security issue.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

feyd wrote:This is a security question?

It's not absolutely needed for the most part. However, if you're running into connection limit issues, it's best to add them immediately after the last database dependant call is made to make sure connections are open as short as possible.
This is a good advice but here is a little tip if you do it like this.

If you edit your code sometime in the future you most likely forget that you did that. So make a mental note that if a database query does not work it is that the connection is closed. It took me some time to figure that out and that error drove me nuts before I did ;)
Post Reply