do I have to check login session when initiating PHP class?

Josh

I used to program php in procedural way. Since I would like to learn more about OOP I have decided to program php in OOP way for my new project.

Anyways, let's say I have a project that requires user to login. Which means in login.php once user enters the correct username and password, it will be redirected to index.php and will start loading all the products from the product table and display them nicely in index.php.

Before, how I did this was, in login.php I'll have the following code:

login.php

session_start();
...
if (loggedCorrect($user, $password)) {
     $_SESSION['loggedinuser'] = $user;
     //redirect to index.php
}

index.php

session_start();
if (isset($_SESSION['loggedinuser']) {
      //select fields from products table and display them
      ...
}

so in OOP it will be something like:

login.php

session_start();
$user = new User($user, $password);
if ($user->hasCorrectLogin()) {
     $_SESSION['loggedinuser'] = $user->getUsername();
     //redirect to index.php
}

index.php

session_start();
if (isset($_SESSION['loggedinuser']) {
     $products = new Products();
     //display all products
}

Product class

class Products {
    private $productArray;
    ...
    __construct() {
         //select all products from mySQL table then put every product in productArray
    }
...
}

My question are:

  1. when initiating object (like Products in my case). do I have to check login session? if so, should I do it inside __contruct? or should I do it before the "class Products" line?

  2. I also have a cronjob.php, which will be executed every x minutes. When it's executing, it will create some objects like Products and analysis them. So if login session checking is required, then I'm not sure how to make this works, as cronjob doesn't support session.

Please advise

Ja͢ck

Quick answers

  1. No. Your domain objects themselves should not be dependent on a logged in session; however, they may need a User instance to perform certain duties, such as only showing the products that a particular user is able to see.

  2. Because of #1, this is now trivial.

Code review

First let's consider your login page code:

$user = new User($user, $password);
if ($user->hasCorrectLogin()) {

In this code, it would seem that User interacts with the database and has knowledge how to validate credentials. That seems a bit too much responsibility for a single class.

It could perform password validation by keeping the hashed password inside the object, but you would only need to validate a password once, so there's really no need to keep that field around. Another reason not to have it done here is when you need to consider strengthening your passwords on-the-fly, which could be a site policy to scale with growing hardware (e.g. when you're using bcrypt).

It should definitely not be doing database interaction; to separate both database interaction and password verification from the User class you could consider adding an authentication service.

try {
    $user = $authService->login($userName, $password);
    $_SESSION['loggedinuser'] = $user;
    // redirect to index.php
} catch (InvalidLoginException $e) {
    // oops, username or password invalid
}

Inside the authentication service, you could add another layer of abstraction to load the user record (using a data mapper for instance).

Instead of only storing the username in the session, you can store the whole User object as well. In some cases this may lead to inconsistencies, but it saves round trips to the database.

Now, let's observe the product overview page:

$products = new Products();

In terms of naming I would say that Products is not a good candidate to describe a collection of objects. A name such as ProductList or ProductCollection is better.

As with the authentication above, it's unclear how the Products class gets populated; it should come from some storage, so let's introduce the repository that will provide the list of products:

$productRepository = new ProductRepository($db);
$products = $productRepository->getAll();

In the simplest scenario, the repository gets initialized with a database instance; more levels of abstraction can be applied when necessary.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I hold a session through a PHP login redirect?

From Dev

PHP - How to check users level into a login session

From Dev

Check when R session have been started?

From Dev

I have an ldap connection on my RoR app but now how do I check users on login?

From Dev

When initiating a PHP class is there any benefit to passing reserved variables through the constructor?

From Dev

How to check that you have been login use session & curl?

From Dev

update table when finish session login in php

From Dev

When do I have to 'require' a Ruby class in my Rake task?

From Dev

Rails how do I check for existance of a class when not instantiated yet?

From Dev

How do I check if a session variable is set?

From Dev

Apache Spark: ERROR local class incompatible when initiating a SparkContext class

From Dev

do i have to create login for database

From Dev

Do I have to check the response status in ApplyResponseChallengeAsync?

From Dev

Do I have to check the response status in ApplyResponseChallengeAsync?

From Dev

How do I check if my dates have a certain year, after retrieving all dates via php?

From Dev

Login and Session PHP

From Dev

PHP login session and cookie

From Dev

Login and Session PHP

From Dev

PHP login and session establishment

From Dev

PHP login with session

From Dev

Security for login by session in php

From Dev

PHP session authentication for login

From Dev

PHP Login (Session Issues)

From Dev

PHP Login script with ajax works but session variables do not exist

From Dev

initiating class as a member of struct

From Dev

how do i give session email to php?

From Dev

Do I need to use session_start() in PHP to use $_SESSION?

From Dev

In AJAX, do I have to init the class?

From Dev

Why do I have to define the class of the pointer?

Related Related

  1. 1

    How do I hold a session through a PHP login redirect?

  2. 2

    PHP - How to check users level into a login session

  3. 3

    Check when R session have been started?

  4. 4

    I have an ldap connection on my RoR app but now how do I check users on login?

  5. 5

    When initiating a PHP class is there any benefit to passing reserved variables through the constructor?

  6. 6

    How to check that you have been login use session & curl?

  7. 7

    update table when finish session login in php

  8. 8

    When do I have to 'require' a Ruby class in my Rake task?

  9. 9

    Rails how do I check for existance of a class when not instantiated yet?

  10. 10

    How do I check if a session variable is set?

  11. 11

    Apache Spark: ERROR local class incompatible when initiating a SparkContext class

  12. 12

    do i have to create login for database

  13. 13

    Do I have to check the response status in ApplyResponseChallengeAsync?

  14. 14

    Do I have to check the response status in ApplyResponseChallengeAsync?

  15. 15

    How do I check if my dates have a certain year, after retrieving all dates via php?

  16. 16

    Login and Session PHP

  17. 17

    PHP login session and cookie

  18. 18

    Login and Session PHP

  19. 19

    PHP login and session establishment

  20. 20

    PHP login with session

  21. 21

    Security for login by session in php

  22. 22

    PHP session authentication for login

  23. 23

    PHP Login (Session Issues)

  24. 24

    PHP Login script with ajax works but session variables do not exist

  25. 25

    initiating class as a member of struct

  26. 26

    how do i give session email to php?

  27. 27

    Do I need to use session_start() in PHP to use $_SESSION?

  28. 28

    In AJAX, do I have to init the class?

  29. 29

    Why do I have to define the class of the pointer?

HotTag

Archive