/    Sign up×
Community /Pin to ProfileBookmark

One file, or many?

Back when I first started PHP, I used to make a new file for everythng like login.php, logout.php, forgotpass.php, etc. For the last year or so I have been doing all the work inside of a process.php, using a hidden form value to tell the file what I want it to do.

I was wondering how many other coders do it this way, or how many coders do it the old way, and why.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@Sleep_ModeAug 02.2006 — I am at the first stage of php i think ? I'm the most beginner here. I use a new file for everything ? How do you do it by using one file?
Copy linkTweet thisAlerts:
@FireCracker37authorAug 02.2006 — Well, I pass the info around using forms 99% of the time, so in that form I create a hidden field named action, and assign it a value of what I want the form to do, for instance if I want to login a user I will put <input type="hidden" name="action" value="login"> Then in my process.php file I put something like this:

[code=php]
if($action == "login") {
//Code to log in user
}
[/code]


I just create a new if statement for everything that I would otherwise create a new file for.
Copy linkTweet thisAlerts:
@aaronbdavisAug 03.2006 — It depends on what you mean I suppose. Putting different actions in different files (or different functions, if you use the MVC approach) makes the most sense. However, all parts of a single action should be dealt with in a single file.

For example: login and logout could be separate, but you should use one form to submit the login info, process it, and do any redirection.

This lets your users avoid having to retype all their info if there was a problem with the form: you can just print out their old values, directly in the input box and show some error message.
Copy linkTweet thisAlerts:
@emoritzAug 03.2006 — One file is most efficient if you are executing a group of similar commands, such as form handling, but you should make seperate files for different purposes, like one to login and one to load blogs, etc.
Copy linkTweet thisAlerts:
@The_Little_GuyAug 03.2006 — Why don't you just get the type out of the URL?

[code=php]if($_GET['login'] == "login"){
#display login form
}else{
#display homepage
}[/code]


Or use a switch statement

[code=php]switch($page){
case "login":
#display login
break;
case "register":
#display registration
break;
default:
#display main/homepage for this file
break;
}[/code]


I use the switch statement the most, and try to make a limited number of files as possible.
Copy linkTweet thisAlerts:
@PineSolPirateAug 03.2006 — I agree with aaronbdavis. It's best to seperate distinct actions, otherwise you can und up with ungainly, ugly, huge files that do ten things and none of them well.

Another often overlooked problem here is file naming. Pick a style and stick to it.

It sucks to have a db table named "counselor" with "counselors_add.php" "counselorsDelete.php" and the like.

I'm a camel case fan (java fan ?) but just make sure you keep to one method and watch your pluralities. Is that even a word...?
Copy linkTweet thisAlerts:
@FireCracker37authorAug 04.2006 — I do use different files for different "types" of actions. Such as one file for all the session related info (like login/logout), Two files for the blog commands (one for user, one for poster), and many files for forums, but with grouped functions in the same file.

I do use the case statement most of the time, or custom functions/classes. The whole if...elseif was just what I typed for my example.
Copy linkTweet thisAlerts:
@bokehAug 04.2006 — It's best to seperate distinct actions, otherwise you can und up with ungainly, ugly, huge files that do ten things and none of them well.[/QUOTE]Or if written well with reusable code it is possible to have one file which does 10 related tasks and that is no bigger than any one of the ten seperate files.
×

Success!

Help @FireCracker37 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...