Page 1 of 1

is this wrong?

Posted: Fri Aug 07, 2009 7:58 am
by sandersonlj1529
Hi,

I am quite new to PHP but I am making a simple CMS as a fun project to help me test me knowledge. I have already made the log-in but the way I have developed it just seems too easy compared to other applications I have seen.

I have index.php and log-in.php then a data folder the data folder contains config.php and connect.php these handle the database connection and a few other things.

I also have a file called functions.php this is where I have been storing almost everything! (I also have images, css and jquery folders in this data folder)

So this functions.php document looks a little like this:

class user_functions {
function validate_user
{
some code
}
function log_out
{
some code
}
} ect...

so ill be adding more classes when I need them e.g. a class that contains functions that deal with site stats or functions for adding content etc..

I can then just call my functions from the login.php (like user_functions->validate_user) and it works beautifully and makes loads of sense to me.

but When i look at open source programs they user about 2 million different files to do the same job... where am i going wrong? :?

Thanks

Lawrence

Re: is this wrong?

Posted: Fri Aug 07, 2009 8:01 am
by onion2k
Code should be as simple as it needs to be. In your case I imagine you're just testing a username and password matches what's in the database. In the case of the more complex implementations they'll be setting up permission flags, doing password reminders, checking for brute force attacks, etc. If you need to do stuff like that then your code will be much bigger. But if you don't, keep it simple.

Also, don't assume that an open source application is necessarily well written. There's some really bad code out there.

Re: is this wrong?

Posted: Fri Aug 07, 2009 8:03 am
by jayshields
Nothing's "wrong" if it works for you!

It would seem you're using a procedural way of programming and just moving common code into a class/function. The other open-source systems you've been looking at probably use a framework along with an Object Oriented Programming approach, which probably incorporates an ORM of some type. This is generally considered better practice, for reasons which you can find all over these forums and Google.

Re: is this wrong?

Posted: Fri Aug 07, 2009 8:05 am
by sandersonlj1529
Thank you for such quick and friendly replies!
onion2k wrote:Code should be as simple as it needs to be. In your case I imagine you're just testing a username and password matches what's in the database. In the case of the more complex implementations they'll be setting up permission flags, doing password reminders, checking for brute force attacks, etc. If you need to do stuff like that then your code will be much bigger. But if you don't, keep it simple.

Also, don't assume that an open source application is necessarily well written. There's some really bad code out there.
I am adding this to the functions document under various classes e.g. permissions will be under user_functions class and I am planning to add things like a security_functions class.. can my way still work?
jayshields wrote:Nothing's "wrong" if it works for you!

It would seem you're using a procedural way of programming and just moving common code into a class/function. The other open-source systems you've been looking at probably use a framework along with an Object Oriented Programming approach, which probably incorporates an ORM of some type. This is generally considered better practice, for reasons which you can find all over these forums and Google.
Since I don't know much about the differances and why one is considered better practice. I personally think my way is better... I know where all my code is so its easy to fix/add more and I don't need to include more than one or two files at a time. :? but I am not very good at this whole php deal yet.

For example: why would I create a logout.php when it would contain basically nothing (session_destroy();) when I can just have something like function log_out { session_destroy(); } then on my log out button have it linked to index.php?status=logout then use the a simple $GET in the header of index.php to call the function? (this is what I do and it works)