Member Avatar for Member #153709

Hello people,

If I want to access to my admin back-end, you will get a form to input your name and password.

I have created a class that sets my session in a file called

StartSession.class.php

here it is:

<?php
class StartSession{
	static $admin_s;
	static $password_s;
	function __consrtuct($admin_session,$password_session){
		session_start();
		$_SESSION['admin']=$admin_session;
		$_SESSION['password']=$password_session;
		self::$admin_s=$admin_session;
		self::$password_s=$password_session;
	}
	
}
?>

In my admin file that I named

admin.php

I called to my StartSession() class like this:

$startsession=new StartSession($adminad,$passwordad);

Then I cread an awelcome.php file in my admin folder, which calls to a class that checks if my session is set, if not it redirects you to input your form, like this:

<?php
require_once('StartSession.class.php');
class SessionCheck{
	function __construc(){
		if(!isset(StartSession::$admin_s)||!isset(StartSession::$password_s)){
			header('Location:admin/index.php');
			exit();
		}
	}
	
}
?>

In my

awelcome.php

file, I called this class like this:

<?php
require_once('../Classes/SessionCheck.class.php');
$sessioncheck=new SessionCheck();
require_once('Includes/aheader.inc.php');
require_once('Includes/alMenu.inc.php');
?>
<div id="centered_content">
present
</div>
<?php
require_once('Includes/afooter.inc.php');
?>

Everything seems to work well, but then, when I taped the url to my

awelcome.php

file it did not redirect to my form input :( which thing means my session are not set as it is supposed to be : any one can help, please ?

Dani AI

Generated

Two quick points that will fix this and save time debugging.

First, is right: your constructors are misspelled so PHP never runs them. Second (and more important) is a design mistake: storing the login in a static class property will not survive a new HTTP request. Use the $_SESSION superglobal for persistence and call session_start() on every request that reads or writes the session.

A simple, safe pattern:

<?php
class StartSession {
    public function __construct($userId) {
        if (session_status() !== PHP_SESSION_ACTIVE) session_start();
        // store a user id or role — do NOT store plaintext password
        $_SESSION['user_id'] = $userId;
        $_SESSION['is_admin'] = true;
        session_regenerate_id(true); // avoid fixation
    }
}

And check the session on protected pages like this:

<?php
class SessionCheck {
    public function __construct() {
        if (session_status() !== PHP_SESSION_ACTIVE) session_start();
        if (empty($_SESSION['is_admin'])) {
            header('Location: /admin/index.php');
            exit;
        }
    }
}

Debug checklist if it still fails:

  • Turn on errors: error_reporting(E_ALL); ini_set('display_errors',1); — constructor typos will show once enabled.
  • Inspect cookies in the browser devtools: is PHPSESSID being set and sent on the next request?
  • Check for output/BOM before session_start() (headers already sent).
  • Verify session.save_path exists and is writable (see phpinfo()).
  • Don’t keep plaintext passwords in session; store an ID/flag and verify credentials on login using password hashing.

Fix the constructor names and switch your checks to $_SESSION; that will make the redirect behave correctly when someone types awelcome.php directly.

Not sure if this is your original code or not but if it is the fact that construct is misspelled in both classes would cause a problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.