Page 1 of 1

Connection to MySQL Database & to another Database to Login

Posted: Mon Jan 12, 2015 12:46 pm
by cecilchampenois
So, as a new PHP Developer, I am trying to sort things out in my head and I need to try to make sense. If someone can guide me, I would surely appreciate it.

In my company's case, we have a web page called Login_Page.php that POSTs the "username" and "password" to the next web page which is the Check_Login.php page. We have an include() statement at the beginning of the check_login.php page that takes care of the connection to the MySQL database.

(1) Login_Page.php - Calls Check_Login.php
(2) Check_Login.php - includes dbConnect.php

Check_Login runs a SQL statement and looks for the username and password from a different database called "claimsweb".
dbConnect.php maintains the connection to the MySQL database and sets up the webpage's appearance.

Question # 1:
I do not understand the constant need for the statement, include("dbConnect.php"); to be included on every web page. Is this a necessary thing since there is no persistent database connection?

Question # 2:
The previous developer puts the username and password in for access to the MySQL database into the code. Isn't this wrong to do this? I mean, anyone reading the code can see these things.

Re: Connection to MySQL Database & to another Database to Lo

Posted: Mon Jan 12, 2015 1:03 pm
by Celauran
cecilchampenois wrote:Question # 1:
I do not understand the constant need for the statement, include("dbConnect.php"); to be included on every web page. Is this a necessary thing since there is no persistent database connection?
Not because there's no persistent DB connection, but because of how PHP works; it completes its request and dies. Rather than having include statements all over the place, you can use a front controller pattern that will handle bootstrapping your application so the DB connection only needs to be established in a single place.
cecilchampenois wrote:Question # 2:
The previous developer puts the username and password in for access to the MySQL database into the code. Isn't this wrong to do this? I mean, anyone reading the code can see these things.
You have to put it somewhere. So long as it's not in version control, I wouldn't worry too much about it. Keeping it out of the document root is also a good idea.

Re: Connection to MySQL Database & to another Database to Lo

Posted: Mon Jan 12, 2015 3:25 pm
by cecilchampenois
How do you do that?

"front controller pattern that will handle bootstrapping your application"

Is that any different than using the include statement?

Re: Connection to MySQL Database & to another Database to Lo

Posted: Mon Jan 12, 2015 3:40 pm
by Celauran

Re: Connection to MySQL Database & to another Database to Lo

Posted: Mon Jan 12, 2015 3:42 pm
by cecilchampenois
Interesting concept.

Merci,

Re: Connection to MySQL Database & to another Database to Lo

Posted: Mon Jan 12, 2015 10:30 pm
by Christopher
Given your comments, I would recommend using a framework. They have done all the computer science, leaving you to be productively focused on building your application.

Re: Connection to MySQL Database & to another Database to Lo

Posted: Tue Jan 13, 2015 6:21 am
by Celauran
Agreed. If/when you're ready to start refactoring/rewriting the codebase, definitely look at using a framework.