Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Read a lot of posts about storing sessions inside database but no one seems to provide proper information.Then i came across this post and i found it to be very helpful.
http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/
Now what i don't understand with these posts is that they are taking a class named Database which contain certain functions in order to make session class work i.e. to store sessions inside database.
Question : My question to you all is if it is possible please provide me with that database class even it means creating one own database class file.Just to make sure all the functions that the session class is looking inside database class are found and working.
Codepad:http://codepad.org/mtvT3XXB
That article you cite has horrible issues with wording (example: "...we instantiate a copy of the database class...", which is just plain nonsense). But the basic thing behind such "database" class simply is to keep things generic for the reader, which makes sense.
To interact with a database you need some routines for things like connection handling, query execution and preparation and the like. These routines are typically implemented as methods of a class. Such a class is what the author refers to. He does not name a specific one since they are more or less exchangeable.
You don't actually have to implement your own class, you can use one of those already provided. A short overview is given in the php documentation (which you should read!):
http://php.net/manual/en/set.mysqlinfo.php
I suggest you pick the mysqli connector and go through a few tutorials to learn what it does and how to use it.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I know very little about design patterns out there. In fact i never worked with one yet, as i always went for raw coding. But i think its time to enrich my knowledge on design patterns out there. Specially i want to know more about Factory, Singleton & Strategy design patterns. I googled about them of course but I still not clear about their differences, how to implement them etc.
If anyone can suggest me some good document where i can read much more, that would be very much helpful.
Thanks in advance for helping.
https://sourcemaking.com/design_patterns is a very helpful website, with a lot of explanations and code samples, including PHP ones. I added very short summaries in my own words below. Disclaimer: because they summaries are very short, the may not be very accurate, but give you an idea on how the patterns compare.
Factory Method: https://sourcemaking.com/design_patterns/factory_method
In short: you have a separate class that is responsible for creating instances of a certain class. This is to make sure that a class is always constructed 'in the right way'.
Singleton Pattern: https://sourcemaking.com/design_patterns/singleton
In short: only one instance of a singleton class is possible, the class itself has a static class variable that stores the instance, and a static method that returns the stored instance, or create one if it is not yet created.
Strategy Pattern: https://sourcemaking.com/design_patterns/strategy
In short: if there are multiple ways to solve some problem, provide a set of classes that each contain one implementation to the problem and let the client decide which implementation to use.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm in the process of learning ASP.NET and I'm having a few difficulties with certain things. The main difficulty I have right now is accessing data from a database. Usually in PHP in the model I'd just write an SQL query for a specific model function and then that'd query the DB and return the results I want to the calling controller. Is doing this fine in ASP MVC? And if so how do you do it? The tutorial I was following started using scaffolding and I want to avoid that as I want to fully understand how it all fits together.
I myself started in PHP and gradually moved to .NET. The way I manage my database is via Entity Framework. I know it may sound like you've got to learn yet another useless thing, but it is actually very simple. If you just want to learn the concept: use code first approach.
This is how it works: you create a bunch of models (that you will need anyway its called Model-View-Controller for a reason) that are nothing but objects. Those models are used to build your database. So you no longer need to set up the database manually (e.g. phpMyAdmin). It sounds quite amazing but that's how it works and then you use LINQ (or LINQ-to-SQL) to get queries.
This an example of a LINQ-to-SQL query:
var query = from u in dbContext.User where u.UserID == 1 select u;
This will come useful later on as well when you want to validate information etc. Here is a great starting tutorial (just watch the first 4 of those videos):
https://www.youtube.com/watch?v=Z7713GBhi4k&list=PL6n9fhu94yhUPBSX-E2aJCnCR3-_6zBZx
Moving from PHP to .NET is a big move, but it is really worth it.
Good Luck!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a project that I need to let users to create pages on my server. however, I do not want to let users clutter my mysql database by storing the stuff in there so I cannot use mysql database for creating the pages.
I did research this topic and there seem to be a some sort of a plugin for WP that will allow virtual page creation.
is this possible using pure php WITHOUT the use of any database ?
It's possible, but wrong.
You can use php to write a html file to your web directory, sure. But that
solution is in no way cleaner or less cluttered than putting stuff in your
database, for a few reasons:
It's easier to have structured information in the database
It's a good thing conceptually to separate user data from your program
It's easier to control access to your database in a safe way, compared
to writing user data to the file system
"I really do not want to use mysql database" is not a good reason to give this
up. You might have a good reason, but it's not easy to guess what that is, which
makes suggesting alternatives very difficult.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm attempting to create a login and registration system for my website, but I'm having trouble deciding on how functionality should be split up. For example should I have a User class that has login() and registration() functions, or should I have Login and Registration classes that contain a user object and the User simply contains data (id, username, etc...) without much functionality by itself? Do either of these seem like suitable OOP approaches, or am I way off base here?
EDIT:
Sorry that my question was so vague, I'll try to make it more specific. The User class that I currently have takes care of both logging in and registering. I've been told that each class should have one responsibility, so I'm not sure mixing those login and register into one class is good design. My question is when creating a class, specifically for a member system, is it better to separate functionality such as login, register and update into completely separate classes that have that have one purpose? Is one User class having a function for each of those purposes bad because it mixes too much functionality into one class?
Depending on your authorization system; both might be true or both might be false.
Short Answer: Select one of them and continue
If you want to write Good Code
Try to make it
Adaptable
Brief
Consistent
Correct
Extensible
Fast
Flexible
Generic
Modular
Reusable
Stable under changing requirements
If you are trying to make your code even better, first of all; I suggest you to write your contracts (a.k.a. interfaces). After writing your interfaces (one for your data provider maybe, and one for service provider); try to write one for your login & registration system. And then; you will find the answer I hope.
You can use any realization..
Just see realization of it in the top frameworks like Symfony, Zend, Yii, etc..
Or just use one of them. It is much better ;)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm building an Authentification library that's going to have around 45+ methods for dealing with user related stuff. However I've been wondering if it's actually recommendable to keep everything on a single file.
Is there a benefit on splitting my class into several subclasses and load them when needed?
I can always for example split the class into "mandatory" elements and the elements that only registered users need...
For example:
Mandatory Methods:
$user->is_logged()
$user->login()
$user->register()
Methods for Register...
Methods for logged-in user.
It just depends on how you want to be including the class file(s) in your pages. If you want one simply include() statement for every page, then keep it all in one file. Unless your library is HUGE, the overhead from the other classes shouldn't be too much.
If you do it the other way, you'll simply be including different files based on the status of the session of the client.
Personally, I'd split them up as it's easier to edit them that way, but it's totally up to you.
I'd go with the class/sub-class option.
You could then use a factory to return the correct type of user object based on the current URL or by simply specifying the desired type of user object if your particular setup doesn't lend itself to this.