Jump to content

Recommended Posts

Hi all,

 

I'm developing my own little OOP-based PHP-MySQL Framework. Its derived from the MVC framework. I'm making a login script that uses sessions. I don't think I have ever used sessions in a class before, but I didn't think I would need to do anything fancy to use them as they are a global.

 

In short, on the index page, the first thing it does is call the checkLogIn() function from the login class to see whether there logged in or not. It simply checks the values of $_SESSION['auth'] and returns true or false depending on that value. But as soon as I load the index page, I get an error saying undefined index: auth.

 

The session is started.

 

Thanks,

Cobby

 

Link to comment
https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/
Share on other sites

I know it undefined, I am asking why.

 

login.class.php

 

<?php

class login {
        var $reg = array();

        function login($reg){
                $this->reg = $reg;
        }

        function checkLogIn($type=NULL){
                if($type == 'login'){
                        if($_SESSION['auth'] == 'yes'){
                                $this->reg['redirect']->redir(site_url.'admin/index', 0);
                        }
                }else{
                        if($_SESSION['auth'] != 'yes'){
                                $this->reg['redirect']->redir(site_url.'admin/login', 0);
                        }
                }
        }

        function log_in($username, $password){
                if(empty($username)){
                        $error[0] = "You haven't entered a username";
                }

                if(empty($password)){
                       $error[1] = "You haven't entered a password";
                }

                $encrypt = $this->reg['crypt']->encrypt($password);

                $sql = "SELECT * FROM 
                                `".tbl_prefix."users` 
                                WHERE `username` = '".mysql_escape_string($username)."' 
                                AND `password` = '".mysql_escape_string($encrypt)."'";


                if($this->reg['db']->dbNumRows($sql) == 1){
                        $_SESSION['auth'] = 'yes';
                        $user = $this->reg['db']->dbFetchArray($sql);

                        foreach($user as $key => $val){
                                $_SESSION['user'][$key] = $val;
                        }

                        $this->reg['smarty']->assign('userData', $user);
                        return 'true';

                }else{
                        $error[2] = "You have provided invalid login credentials";
                }

                if(isset($error)){
                        return $error;
                }
        }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363024
Share on other sites

When you have error reporting set to report everything, the interpreter will raise errors whenever you use a variable or array index that has not yet been defined.

 

<?php
  session_start();
  if($_SESSION['auth']){
    echo "logged in";
  }
?>

 

On the first hit to the page, session_start() will create an empty session, meaning that $_SESSION is empty and the index 'auth' in $_SESSION doesn't exist.  In that case, you do one of two things:

 

1)  Change the level of your error reporting.

 

2)  Change the comparison

<?php
  session_start();
  if(isset($_SESSION['auth']) && $_SESSION['auth']){
    echo "logged in";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363067
Share on other sites

Theres a lot of code here:

 

config.php

<?php

// set database information
$settings['server'] = 'localhost';
$settings['username'] = 'cobby';
$settings['password'] = '*******';  // intentionally left blank for the purpose of this post
$settings['db'] = 'framework';
$settings['tblpre'] = 'spf_';
$settings['urlformat'] = 'index.php?file=';

if(!defined('tbl_prefix')){
        define('tbl_prefix', $settings['tblpre']);
}

if(!defined('urlformat')){
        define('urlformat', $settings['urlformat']);
}
$settings['adminemail'] = 'admin@localhost';

?>

 

include.php

<?php

// Set error reporting
error_reporting(E_ALL);

// get url function
function url(){

        // get server protocol
        $sp = $_SERVER['SERVER_PROTOCOL'];

        $sp = explode ('/', $sp);

        $protocol = strtolower($sp[0]);

        // add protocol
        $url = $protocol."://".$_SERVER['SERVER_NAME'];

        // get the script path
        $path = $_SERVER['PHP_SELF'];

        // separate path by /
        $path = explode('/', $path);

        $final = '';
        // join parts, start at 1 to remove first blank and end 2 early to skip includes and filename name
        for($i=1; $i<count($path)-1; $i++){
                $final .= "/".$path[$i];
        }

        // add trailing /
        $final .= "/";

        // join parts
        $ret = $url.$final;
        return $ret;

}

// Set Constants
if(!defined('DIRSEP')){
        define('DIRSEP', DIRECTORY_SEPARATOR);
}

$site_path = realpath(dirname(__FILE__) . DIRSEP . '..' . DIRSEP) . DIRSEP;
if(!defined('site_path')){
        define('site_path', $site_path);
}

if(!defined('site_url')){
        define('site_url', url());
}

include site_path.DIRSEP.'classes'.DIRSEP.'reg.class.php';
$reg = new reg;

$scandir = scandir(site_path."classes\\");

foreach($scandir as $val){

        if(preg_match("+.class.php+", $val)){

                $val = substr($val, 0 ,-10);

                if(($val != 'Smarty_Compiler') && ($val != 'Config_File') && ($val != 'reg')){

                        include_once site_path."classes\\".$val.".class.php";

                        $reg[$val] = new $val($reg);

                }

        }

}
include site_path.DIRSEP.'includes'.DIRSEP.'config.php';
$reg['smarty']->assign('settings', $settings);
$reg['smarty']->assign('siteurl', site_url);
?>

 

index.php

<?php

// include includes.php
include 'includes/include.php';

// connect to database
$reg['db']->dbConnect($settings);

// run request handler
$reg['handler']->page();

// close database connection
$reg['db']->dbClose();

?>

Link to comment
https://forums.phpfreaks.com/topic/72033-i-cant-use-sessions/#findComment-363076
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.