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 7974429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:22:05+00:00 2026-06-04T08:22:05+00:00

When a user logs in, here is my user function: public function login($user) {

  • 0

When a user logs in, here is my user function:

public function login($user) {
    global $database;
    if ($user) {

        $_SESSION['user_id'] = $user->id;
        $this->user_id = $_SESSION['user_id'];

        $_SESSION['username'] = $user->username;
        $this->username = $_SESSION['username'];

        setcookie('user_id', $this->user_id, time() + (60 * 60 * 24 * 14));
        setcookie('username', $this->username, time() + (60 * 60 * 24 * 14));

        $this->logged_in = true;

    }
}

When I look at the Cookiees in Chrome, I find two cookies relating to this:
1 for user_id, 1 for username.

But when the browser is closed and I try to come back, it will not detect the cookiee: Here is the process:

class Session {

     // Most of the class has been edited out; the code above is also a method in this clas. Removed so it's not duplicated.


     private $logged_in = false;
     public $user_id; // yes I realize this is insecure
     public $username; // yes I realize this is insecure

     function __construct() {
          session_start();
           $this->check_login();
     }

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

   private function check_login() {

    if (isset($_COOKIE['user_id']) && (isset($_COOKIE['username']))) {
        $_SESSION['user_id']= $_COOKIE['user_id'];
        $_SESSION['username'] = $_COOKIE['username'];

    } else { // When I test, below shows up showing it doesn't think Cookie is set.
        echo "Cookie not set in check_login().<br />";
    }

    if (isset($_SESSION['user_id'])) {
        $this->user_id = $_SESSION['user_id'];
        $this->username = $_SESSION['username'];
        $this->logged_in = true;
    } else {
        unset($this->user_id);
        $this->logged_in = false;

    }
  }

 $session = new Session();

}
  • 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-04T08:22:07+00:00Added an answer on June 4, 2026 at 8:22 am

    Try to set the path and domain.

    setcookie('user_id', $this->user_id, time() + 3600, '/', '.yourdomain.com');
    

    If you leave the path to empty, the cookie will only “active” within the current directory. For example, if your login script is http://example.com/user/login.php, then when you open http://example.com, the browser won’t set the cookies.

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

Sidebar

Related Questions

Here is the situation: User logs in via username/password stored in an MSSQL database
Here is a use case of my login using a CustomMembershipProvider User Logs in
I have a database full of php time(); stored when a user logs in.
I need to start a program every time the user logs in, but I
Here is how my app is laid out: onResume() user is prompted to login
in this app, the user logs in and their credentials are checked against a
I am working off this test page: http://www.comehike.com/test_fb_connect.php When the user logs in using
I'm trying to make a basic user login. In the PHP page that logs
With PHP and MySQL I am working on a user login type site. Here
The issue here is that when my user logs into my app, they always

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.