SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member spheroid2007's Avatar
    Join Date
    Oct 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Auth.php undefined variable when trying to add session variables

    Regarding the PHP Anthology class Auth.php...I can't seem to add items to the session in this class once the user logs in. I have information I want to grab variables from...My page is blank after I've added a few extra lines:

    PHP Code:
    // If there isn't is exactly one entry, redirect
       
    if ($row['num_users'] != 1)
       {
          
    $this->redirect();
          
    // Else is a valid user; set the session variables
          
    } else {
          
    $this->storeAuth($login$password);
          
          
    // Grab data from db and store in session
          
    $sql "
          SELECT u.user_id, ui.first_name, ui.last_name, ui.email,
          ui.billto_address1, ui.billto_address2, ui.billto_city,
          ui.billto_state, ui.billto_zip, ui.billto_country
          FROM " 
    USER_TABLE " u
          LEFT JOIN " 
    USERINFO_TABLE " ui
          ON ui.user_id = u.user_id
          WHERE " 
    USER_TABLE_LOGIN " = '$login'
          AND " 
    USER_TABLE_PASSW " = '$password'
          AND activated='1'
          "
    ;

          
    $result $this->db->query($sql);
          
    $row $result->fetch();
          
          
    // Add additional session variables
          
    while ($row $result->fetch())
          {
             
    $user_id $row['user_id'];
             
    $first_name $row['first_name'];
             
    $last_name $row['last_name'];
             
    $email $row['email'];
             
    $billto_address1 $row['billto_address1'];
             
    $billto_address2 $row['billto_address2'];
             
    $billto_city $row['billto_city'];
             
    $billto_state $row['billto_state'];
             
    $billto_zip $row['billto_zip'];
             
    $billto_country $row['billto_country'];
          }
          
          
    $this->session->set('user_id'$user_id);
          
    $this->session->set('first_name'$first_name);
          
    $this->session->set('last_name'$last_name);
          
    $this->session->set('email'$email);
          
    $this->session->set('billto_address1'$billto_address1);
          
    $this->session->set('billto_address2'$billto_address2);
          
    $this->session->set('billto_city'$billto_city);
          
    $this->session->set('billto_state'$billto_state);
          
    $this->session->set('billto_zip'$billto_zip);
          
    $this->session->set('billto_country'$billto_country);
          
          
    // SET COMMON SESSION VARIABLES
          
    $refresh_time 120;
          
    $c_hour date("H");    //Current Hour
          
    $c_min date("i");     //Current Minute
          
    $c_sec date("s");     //Current Second
          
    $c_mon date("m");     //Current Month
          
    $c_day date("d");     //Current Day
          
    $c_year date("Y");    //Current Year
          
    $t_timestamp mktime($c_hour,$c_min+$refresh_time,$c_sec,$c_mon,$c_day,$c_year);
          
    $this->session->set('stamp'$t_timestamp);

          
    $ipaddress = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
          
    $browser $_SERVER['HTTP_USER_AGENT'];

          
    $sessionid $session->get('login_hash');

          
    $timezone = -8;
          
    $logintime gmdate("Y-m-d h:i:s"time() + 3600*($timezone+date("I")));    
          
          
    $sql="
          INSERT INTO login_times
          (user_id, logintime, ipaddress, browser, sessionid)
          VALUES
          ('
    $user_id', '$logintime', '$ipaddress', '$browser', '$sessionid')
          "
    ;

          
    $this->db->query($sql); 
    Errors in my log:

    PHP Notice: Undefined variable: query in ...MySQL.php on line 172
    PHP Fatal error: Cannot access empty property in ...MySQL.php on line 172

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    sorry, not enough information provided. you need to figure out exactly which line of this file is causing the error.

  3. #3
    SitePoint Member spheroid2007's Avatar
    Join Date
    Oct 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have shown the line 172 below, and also posted the .zip of my files that I'm testing.

    MySQL.php:

    PHP Code:
     function fetch()
       {
          if (
    $row mysql_fetch_array($this->queryMYSQL_ASSOC))
          {
             return 
    $row;
          } elseif ( 
    $this->size() > ) {
             
    mysql_data_seek($this->$query0);  // This is line 172
             
    return false;
          } else {
             return 
    false;
          }
       } 
    Attached Files Attached Files

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See the changes here:
    PHP Code:
    ..................
    $result $this->db->query($sql);
    #$row = $result->fetch();
    // Add additional session variables
    while ($row $result->fetch()){
    ............... 
    The resource is already used, that's why it is showing such error. Remove first fetching the resource line.

    Did you get??
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Member spheroid2007's Avatar
    Join Date
    Oct 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip. This makes sense. I'm closer, but still get the same errors. If I do this within Auth.php:
    PHP Code:
          $result $this->db->query($sql);
          
          
    // Add additional session variables
          
    while ($row $result->fetch())
          {
             echo 
    $row['first_name']." ".$row['last_name'];
          } 
    I see the user's first and last name such as "Bob Smith" after logging in. So the variable is being passed ok. But, I still get the error when trying:

    PHP Code:
          // Add additional session variables
          
    while ($row $result->fetch())
          {
             
    $this->session->set('first_name'$row['first_name']);
          } 
    Here's what's interesting. If I modify the code to output the row to a local variable first, then pass to the session this way:

    PHP Code:
          // Add additional session variables
          
    while ($row $result->fetch())
          {
             
    $first_name $row['first_name'];
             
    $this->session->set('first_name'$first_name);
          } 
    ...At first when I log in the web page is blank and I see the same 2 errors in the error log. When I refresh the page, I see the session variable for first_name is passed correctly (my 5.php page uses a command to: print_r($_SESSION);. Seems like I'm so close! What is causing this?

  6. #6
    SitePoint Member spheroid2007's Avatar
    Join Date
    Oct 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SOLVED! I didn't realize that the SQL statement where I select the username and other info from the db...only had 1 result. So I couldn't do a while to fetch the variables....this works perfectly:

    PHP Code:
          $result $this->db->query($sql);

          
    $row $result->fetch();

          
    $this->session->set('user_id'$row['user_id']);
          
    $this->session->set('first_name'$row['first_name']); 

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's why it is better to see how many records are being retrieved from the table and also see the prepared query to see before executing. I always do so.

    Just my opinion. Lol!
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Member spheroid2007's Avatar
    Join Date
    Oct 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are there additional lines of code you add? If so what would you do?


Bookmarks

Posting Permissions

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