ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

User Login

November 02, 2009 7:37pm

Subscribe [4]
  • #1 / Nov 02, 2009 7:37pm

    GamingFusion

    97 posts

    I have made a simple Login Script that asks the user for their username and password.

    Heres my problem, when i go to the login page it doesn’t display. I think it has somehting to do with my if statement at the top but it should work.

    here is my code
    ——View——

    <?php 
    $this->database->checkLogin();
    
    if ($loggedIn == FALSE)
    {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html >
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?=$title?></title>
    </head>
    
    <body>
    <h1><?=$heading?></h1>
    <hr >
      
    <?php echo validation_errors(); ?>
    
    <?=form_open('theater/checkLogin');?>
    
    <table>
        <tr>
            <td>Username: </td><td><input name="username" class="input" type="text" value="<?php echo set_value('username'); ?>" /></td>
        </tr>
        <tr>
            <td>Password: </td><td><input name="password" class="input" type="password" />
            </td>
            <td><?=form_submit('submit', 'Login');?></td>
        </tr>
    </table>
    
    </form>
    
    </body>
    </html>
    <?php
    }else{
        echo 'Logged In';
        //redirect('theater', 'refresh');
    }
    ?>

    ——model——

    function checkLogin()
        {
            $u1 = get_cookie('username');
            $p1 = get_cookie('password');
    
            $logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));
    
            //Rename some of the $logged['variables'] with easier names.
            $uId = $logged['id'];
            $uName = $logged['username'];
            
            if ($uName) {
                return $data['loggedIn'] = TRUE;
            }else{
                return $data['loggedIn'] = FALSE;
            }
        }
  • #2 / Nov 02, 2009 7:47pm

    clip

    72 posts

    <?php
    $loggedIn = $this->database->checkLogin();
    
    if ($loggedIn == FALSE)
    {
    ?>
    ......
    
    //or
    if ($this->database->checkLogin() == FALSE)
    {
    ?>
    ......

    Edit: Also your return should just be like this:

    if ($uName) {
                return TRUE;
            }else{
                return FALSE;
            }
  • #3 / Nov 02, 2009 8:26pm

    GamingFusion

    97 posts

    $loggedIn is returned from the database model why do i have to make the function in the database model equal to it?

    anyways that didnt help at all. it still doesnt work

  • #4 / Nov 02, 2009 9:19pm

    BrianDHall

    760 posts

    First, your $loggedIn will not be effected by your function because it returns a value that is simply discarded. Passing data though an array into a view happens when you call load->view() - you can’t do it even by reference inside the view itself.

    You’ll need to have your checkLogin() function return true or false, and then do as Clip states above.

    The next thing is you need to remove the if statements entirely and just see if your form displays sans login check. If it does, then you need to check the value of $loggedIn. If it is true then your form should display - if it doesn’t then that’s a clue what’s the next thing that is wrong.

  • #5 / Nov 02, 2009 11:10pm

    GamingFusion

    97 posts

    I took out the if’s in both the view and model and it didnt make a difference.

  • #6 / Nov 02, 2009 11:12pm

    GamingFusion

    97 posts

    There is something wrong with calling the checkLogin() function

  • #7 / Nov 02, 2009 11:50pm

    BrianDHall

    760 posts

    There is something wrong with calling the checkLogin() function

    I presume this means that when you take out that call in your view it loads fine?

    Try using an otherwise empty function in your controller that just echos something directly, and make sure that loads ok. Then put in the call to check login, and see if that works.

    Keep narrowing it down to see if you can find one specific line that causes the problem, then perhaps you’ll have an answer or we can be of more help.

  • #8 / Nov 02, 2009 11:58pm

    GamingFusion

    97 posts

    it doesn’t load this either

    function echoSometing()
        {
            echo 'Something';    
        }
  • #9 / Nov 03, 2009 12:05am

    BrianDHall

    760 posts

    Blank screen? Well then something has gone wrong way before all this then. Search the forums for “blank screen” or “white screen” - there is a whole set of things you’ll need to check one by one to narrow down what happened.

    You basically need to pair down your application, commenting out big pieces as you go, until you can get something that works properly. Or if you are just starting this application, just use a fresh install of CI and copy/paste, making sure it doesn’t break as you go.

    We can’t troubleshoot code we don’t have, so you’ll need to work back to something that loads properly and then work your way back forward towards what you are wanting to do. I have found most whitescreens I experience are from models, but if that function is from a constructor and you don’t even get a 404 then something else entirely is wrong.

  • #10 / Nov 03, 2009 12:07am

    GamingFusion

    97 posts

    well in my other pages work just fine when loading a model but i will look into the search you told me to look at.

  • #11 / Nov 03, 2009 12:24am

    GamingFusion

    97 posts

    Ok i added this code to my .htaccess file

    php_value display_errors 1
    php_value error_reporting 2039

    and that gave me an error about get_cookie()

    so i forgot to load the cookie helper.

    now i get this error

    Fatal error: Cannot use object of type CI_DB_mysql_result as array in /Users/chadgregory/Sites/ci/system/application/models/database.php on line 129

    heres my code for the database.php file(model) with a star on line 129

    function checkLogin()
        {
        
            $this->load->helper('cookie');
                
            $u1 = get_cookie('username');
            $p1 = get_cookie('password');
    
            $logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));
    
            //Rename some of the $logged['variables'] with easier names.
    *        $uId = $logged['id'];
            $uName = $logged['username'];
            
            if ($uName) {
                return TRUE;
            }else{
                return FALSE;
            }
        }

    basically i want it to assign the id and username of the user who is logged in into the two variables

  • #12 / Nov 03, 2009 2:19am

    clip

    72 posts

    Please read the user guide on how to work with query results

    $logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));
    
    if($logged->num_rows() > 0)
    {
         $row = $query->row();
    
         $uId = $row->id;
         $uName = $row->username;
    }
  • #13 / Nov 03, 2009 6:29pm

    GamingFusion

    97 posts

    ok that worked thanks man, the code i ahd there before was from my user system i made in php a few months ago. But now i was wandering how to return the variable $uId and $uName to the page that i execute the function on so i can do something like this:

    <?="Your Id is: ", $uId?>
    <?="Your Id is: ", $uName?>
  • #14 / Nov 03, 2009 8:51pm

    clip

    72 posts

    You can either add them to a session or change your return from your model

    //change 
    return TRUE;
    //to
    $data['username'] = $uName;
    $data['user_id'] = $uId;
    return $data;
  • #15 / Nov 03, 2009 9:04pm

    GamingFusion

    97 posts

    did work man here my code

    function checkLogin()
        {
        
            $this->load->helper('cookie');
                
            $u1 = get_cookie('username');
            $p1 = get_cookie('password');
    
            $logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));
    
            if($logged->num_rows() > 0)
            {
                 $row = $query->row();
    
                 $uId = $row->id;
                 $uName = $row->username;
                
                $data['userId'] = $uId;
                $data['userName'] = $uName;
                
                return $data;
            } 
        }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases