Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: SESSIONS in MVC

  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    41
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Smile SESSIONS in MVC

    I need help. I'm working on a big school project focusing on MVC and MySQL. My issue is the login. I can successfully login but when i refresh the page it sets my session[loged] to false. I swear I can't figure out why. I thought maybe it had to do with MVC. Should I set the SESSION[loged] in the model, controller, view or the index in the root folder? For now I have the "if(!isset($_SESSION[loged]))$_session[loged] = false" in the model class. But like I said, when ever I refresh or change the page $_SESSION[loged] is set to false. Please Help! Thanks!
    1. index.php
    2. controller
      • /controller.class.php
    3. model
      • /model.class.php
    4. view
      • /index.php
      • /inc/...php

    If you don't know MVC you're lost. Here's the code:
    index.php
    PHP Code:
    <?php 
        
    require_once('controller/controller.class.php');
        
    $controller = new controller();
        
    $controller->enable();
        require_once(
    'view/index.php');
    ?>
    controller/controller.class.php
    PHP Code:
    <?php
    require_once('model/model.class.php');
    class 
    controller{
        public 
    $model;
        public function 
    __construct(){
            
    $this->model = new model();    
        }
        public function 
    enable(){
            
    // ALL LOGIC GOES HERE
            /* example:
            * if(isset($_POST['submit']))
            * $this->model->add_user();*/
            
            // SESSION LOGED SESSION ADMIN
            
    if(!isset($_SESSION['loged'])) $_SESSION['loged'] = false;
            if(!isset(
    $_SESSION['admin'])) $_SESSION['admin'] = false;
            if(isset(
    $_GET['logout'])){
                
    $_SESSION['loged'] = false;
                
    $_SESSION['admin'] = false;    
            }
            
            
    // REGISTER SUBMIT
            
    if(isset($_POST['registerSubmit'])){
                
            }
            
            
    // LOGIN SUBMIT
            
    if(isset($_POST['loginSubmit'])){
                
    $this->model->login($_POST['loginUname'], $_POST['loginPword']);
            }
                
        }
    }
    ?>
    model/model.class.php
    PHP Code:
    <?php
    class model{
        private 
    $_db;
        public function 
    __construct(){
            
    //connect to db
            /*** mysql hostname ***/
            
    $hostname 'localhost';
            
    /*** mysql username ***/
            
    $username 'root';
            
    /*** mysql password ***/
            
    $password '';
            try{
    $this->_db = new PDO("mysql:host=$hostname;dbname=mysql"$username$password);}
            catch(
    PDOException $e){echo $e->getMessage();}
        }
        
        
    // Login
        
    public function login($username$password){
            
    $sql "SELECT * FROM scalpler_users";
            foreach(
    $this->_db->query($sql) as $user){
                if(
    $user['username'] == $username){
                    if(
    $user['password'] == $password){
                        
    $_SESSION['loged'] = true;
                        if(
    $user['admin'] == 'y'){
                            
    $_SESSION['admin'] = true;
                            echo 
    '<script>this.alert("Echo you are an admin !!!!");</script>';    
                        }else 
    $_SESSION['admin'] = false;
                    }else{
                        echo 
    '<script>this.alert("Wrong Password!");</script>';
                    }
                }else{
                    echo 
    '<script>this.alert("Username Does not Exist!");</script>';
                }
            }
        }
        
        
        public function 
    __destruct(){
            
    //close db
            
    unset($this->_db);
        }
    }
    ?>
    Last edited by matz0rz; 06-21-2013 at 03:02 AM.

  • #2
    New Coder
    Join Date
    Oct 2011
    Posts
    41
    Thanks
    27
    Thanked 0 Times in 0 Posts
    LOL forgot my session_start(); RESOLVED

    Sorry guys <3


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •