I have an admin page (../index2.php) I would like to log in as my home URL when I first open the browser. You have to be logged in to view this page with username and password. Obviously if I log out and close the browser, then reopen, it should redirect to the login page. This is the top code of the index2.php page when it first opens:
<?php
require_once ("../includes_kl/initialize.php"); // simply sets up database and other classes including session class
?>
<?php
if(!$session->is_logged_in()) {
redirect_to("/cms/login.php");
}
?>
It will redirect to the login.php page, but that page does this:
require_once ("../../includes_kl/initialize.php");
if ($session->is_logged_in()) {// Skip login page if already logged in.
redirect_to("index.php");
}
And because I am now logged in apparently (not sure when that happened), it goes to index.php.
I verify my cookiees are set when I open the browser. And the cookies do this:
private function check_login() {
if (isset($_COOKIE['user_id']) && (isset($_COOKIE['username']))) {
$_SESSION['user_id']= $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
}
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;
}
}
My session Constructor:
function __construct() {
session_start();
$this->check_login();
}
UPDATE: I added this code to echo varibles:
function __construct() {
session_start();
$this->check_login();
if ($this->logged_in) {
echo "Session User Success: " . $this->username;
} else {
echo "Session User Fail";
}
}
It returns the “Session User Fail” When I first open the browser (I took off the redirect for testing purposes in the index2.php so I can see the results).
UPDATED 2:
I have tracked down issues as Cookiees NOT being set.
Here is where I set them:
public function login($user) {
global $database;
if ($user) {
$this->user_id = $_SESSION['user_id'] = $user->id;
$this->username = $_SESSION['username'] = $user->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;
}
}
Is this being done correct? This is part of when the user is logging in. I have verified in Chrome that the cookies are set. Yet, in the check_login() method, it odes not find them in that first “if” statement. The “isset” test fails.
I figured it out with help from another question. It did have to do with Cookies. I simply need to set the path and the domain in the setcookie() method.
setcookie(‘username’, $this->username, time() + (60 * 60 * 24 * 14), ‘/’, ‘domain.com’);
My login.php (where the cookie was first set) was in a different folder level than my index2.php that the browser was opening up to. So the cookie was only set for the folders in the ‘domain.com/folder/HERE’ level (this is where my login.php was: domain.com/CMS/login.php).
On the other hand, I was opening up browser to domain/index2.php where the cookie was not set. So it then sent you to the login.php page where the cookie WAS seen so it would pass the same test that it just field and redirect you to index.php where I was not logged in. So that is why my browser was opening me up in index.php AND logged in.