Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:18:56+00:00 2026-06-11T13:18:56+00:00

I do know how to use sessions/cookies. I do keep session and a cookie

  • 0

I do know how to use sessions/cookies. I do keep session and a cookie + token for a user logged in.

I’m using MVC structure (my own) and i have a Login.php controller. I also have User.php class that is a singleton class having only 1 instance.

My base controller gets the instance of User and stores in a variable like this:

   abstract class Controller {
    private $model;
    private $user;

    function __construct($model = '') {
        ..... //some code

        $this->user = User::getInstance();
    }

    public function user() {
        return $this->user;
    }
   }

In my login.php i have the following once the user submits form with user name and pass:

function logUserIn() {
    if (! isset($_POST['UName']) || ! isset($_POST['UPass'])) {
        $this->_404();
    }

    $uname = strtolower($_POST['UName']);
    $pass = Hash::strongHashMD5($_POST['UPass']);
    $token = $_POST['token'];
    $isValid = $this->model->userCheck($uname, $pass);

    $res = $this->validateUser($isValid, $token, $uname);

    if ($res === false) {
        echo 'User Does Not Exist!';
    } else if ($res === 'Token_Error') {
        echo 'Invalid Form Submission';
    } else if ($res === true) {
        //update token
        $this->model->updateToken($isValid['ID'], $token, $_SERVER['REMOTE_ADDR']);
        header("Location: ../login");   
    }
    exit;
}

this is my method that validates user

private function validateUser($UInfo, $token, $UName) {
    if ($UInfo !== false && isset($UInfo['ID']) && $UInfo['ID'] > 0) {
        if ($UInfo['token'] == $token) {
            return 'Token_Error';
        } else {
            $this->user()->setValues($UInfo['ID'], $UName, $token);
            $this->user()->setSessions();
            return true;
        }
    }

    return false;
}

setsessions() method just sets the session/cookies of that user

Now everytime i want to access to see whether user is logged in or not i have to do it through controller and pass it to anywhere else.

Is there any better way of doing this? Is there any problem with my code in terms of security issues etc…

Any suggestions/advices will be appreciated, thanks guys

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:18:58+00:00Added an answer on June 11, 2026 at 1:18 pm

    Currently you have domain business logic leaking all over the place. It should stay in the model layer (yes, in proper MVC the Model is a layer not a class or object), instead of being in the presentation layer.

    Also, please stop hashing passwords with MD5. You might as well leave them as plain-text if you do so. Instead you should be using either crypt() with CRYPT_BLOWFISH or PBKDF2 algorithm.

    Oh .. and redirecting people to 404, if one of the form fields is empty, seems a bit like overreacting.

    anyway ..


    the main topic:

    The user authentication should happen entirely in the model layer (more precisely: in some Recognition service). Controller should only provide model layer with data and notify the current view that POST has request has been sent.

    The service, upon receiving data, should create domain object for the User and assign the values. If the data passes the validation (which is one of responsibilities of a domain object) the service should instantiate the appropriate data mapper and fetch the data from storage (which might or might not be an SQL database). If this goes thought with any problems (like missing record in storage), the domain object should conform the credentials.

    If any of this failed at some point, service should act upon the error state and put it in temporary storage (most likely – session).

    When application gets to the part where view is supposed to generate the response, the view instance would react on the indication about POST request by checking for error state in model layer and perform the redirect by sending an HTTP header as only response based on whether or not there has been an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone know how I can use httponly cookies for sessions and cookies on the
I have used sessions before but never cookies. I would like to use cookies
I have an application built with codeigniter using the sessions class and storing session
Now i know to use the method of float.Parse but have bumped into a
I am using Haml in a Ruby on Rails project. I know you use
Do i login using cookies or sessions in a login system? I've seen examples
I want to understand the logic of authorization,cookies,users logins,sessions..Do you know any source that
When we are using ViewState or cookies or cashing or sessions where are we
I'm designing my own session handler for my web app, the PHP sessions are
I have never wanted to allow a user to stay logged in for any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.