Link to home
Start Free TrialLog in
Avatar of egoselfaxis
egoselfaxis

asked on

Querying a multi-column CSV file using PHP

I have a PHP-based application that I've been managing that I'm currently in the process if re-factoring.  One part of the application contains a very lengthy if/else routine that's similar to the following:

$office_username = $_SERVER['REMOTE_USER'];
if($username == 'john') {
	$email = "john4556567@domain.com";	
} else if($username == 'mary') {
	$email = "mary3533367@emaidomainladdress.com";	
	....	
	....
} else {
	$email = "test@domain.com";
}

Open in new window


The application is currently using .htdigest based authentication, and we've decided that we'd prefer to keep it this way and to NOT store the user info in the database (to avoid having to build any kind of administrative console for managing users).

Although this approach is functional, it's resulting in problems when one of the other site administrators has to update the if/else routine with another username and email address.  In other words, he sometimes fails to download the single-page application from the server prior to applying his updates, which results in my recent updates being completely "blown out", so to speak.

What I'd like to do is replace the entire if/else statement with a routine that queries a separate CSV file (that lives outside of the site root) to see if it contains the username, and that then returns the email address that corresponds to it and that's on the matching row.  For example, the format of the CSV file would be as follows:

john,john4556567@domain.com
mary,mary3533367@emaidomainladdress.com

This way, the other site administrator would haver have to touch the application's source code.  He would only need to update the CSV file.  

How might I accomplish something like this? (I'm not opposed to using an external PHP library or to using a different file format)

Thanks in advance,
- Yvan
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of egoselfaxis
egoselfaxis

ASKER

Hi guys.  I'm so sorry for the delay in my replying to everyone's thoughtful responses here.  I've been in a holding pattern for the past week or so due to the client having gone MIA on me, and was hoping to get some feedback from him before replying.  Regardless, I guess I should just close this out for now.

Chris .. I haven't tested your PHP example yet, but it looks like it might very well fit the bill.  Thank you.

Ray  .. the approach you're suggesting sounds interesting, but knowing my client the way that I do, I'm fairly certain that he would find some way to screw things up if I gave him anything more complicated than a CSV file to work with.  Still, I learned something new .. so kudos to you.

Gr8gonzo .. your suggestion is excellent.  However, my client is often times administering his website from different locations and PCs, which means that configuring and using client software to manage the users database unnecessarily complex.  Still, .. thank you for making me aware of an excellent open source database administration tool.  I've just downloaded and installed a copy of it for myself.

Again, -- thanks all!

Regards,
- Yvan
Would have been much easier on you and the client to use XML for the configuration file and create your own management console but good luck.
A side note on the HeidiSQL - it is available as a portable app, which means you can simply save it onto a USB stick and run it straight from there, no matter what PC you're on.