www.webdeveloper.com
Page 11 of 14 FirstFirst ... 910111213 ... LastLast
Results 151 to 165 of 198

Thread: My questions thread.

  1. #151
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    I'm confused. Child classes inherit their parents public and protected properties, right? When then am I getting NULL when I try to var_dump() $data in the home controller but get array(1) { ["user_info"]=> array(2) { [0]=> bool(false) [1]=> bool(false) } } when I var_dump() it in the MY_Controller controller.

    MY_Controller.php
    PHP Code:
    class MY_Controller extends CI_controller {

        public 
    $user_info = array();
        
         public function 
    __construct() 
         {
            
    parent::__construct();
          
    $this->user_info[] = $this->session->userdata('username');
            
    $this->user_info[] = $this->session->userdata('id');
            
    $data=array();
            
    $data['user_info'] = $this->user_info;
        }
    }

    /* end of class */ 
    Home.php
    PHP Code:
    class Home extends MY_Controller {
        
        public function 
    __construct() 
         {
            
    parent::__construct();
             
    //tried var_dump($this->data); here        <<--- look
         
    }
         
         
        public function 
    index()
        {
            
    //and tried var_dump($this->data); here        <<--- look
            
            
    $data['title'] = 'Dog Realm - A Virtual Dog Game';
            
            
    $data['main_content'] = 'home/static';
                 
            
    $this->load->view('templates/full_layout'$data);
            
            }

    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  2. #152
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    $data is not defined as a class variable, so it is only visible within the parent class's constructor. If it needs to be accessible elsewhere, then you need to define it as a class variable and access it via $this->data.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #153
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    How come when I do this:

    PHP Code:
    class MY_Controller extends CI_controller {

        public 
    $user_info = array();
         public 
    $data = array();
        
         public function 
    __construct() 
         {
            
    parent::__construct();
          
    //$this->user_info[] = $this->session->userdata('display_name');
            //$this->user_info[] = $this->session->userdata('user_id');
            
    $data['user_info'] = 5//$this->user_info;
            
    var_dump($data);
        }

    I get array(1) { ["user_info"]=> int(5) }

    but when I move the var_dump to the Home controller:
    PHP Code:
    class Home extends MY_Controller {
        
        public function 
    __construct() 
         {
            
    parent::__construct();
            
            
    $data $this->data;
            
    var_dump($data);
         }
         
         
        public function 
    index()
        {
            
            
    $data['title'] = 'Dog Realm - A Virtual Dog Game';
            
            
    $data['main_content'] = 'home/static';                   
                  
            
    $this->load->view('templates/full_layout'$data);
            
            }

    I get this: array(0) { }
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  4. #154
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    I think you're confusing method-local variables and class/object variables. Without the "$this->" in front of it, you are referring to a separate variable that is local only to that method, and has no connection of any sort with the object property of the same name.
    PHP Code:
    class {
      public 
    $foo;
      public function 
    test()
      {
        
    $foo 1;
        
    $this->foo 99;
        echo 
    $foo " - " $this->foo;
      }
    }

    $obj = new A();
    $obj->test(); // outputs "1 - 99" 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #155
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Is there a way to use PHP to tell if somebody is using a proxy? I want to keep proxy users off my site because they are so hard to IP ban.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  6. #156
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Only if you have a way to identify the IP address as being a proxy (maybe a DB of known proxy hosts?). However, the downside is that you then risk excluding people who are using proxy hosts for non-malicious reasons.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #157
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    **I didn't use PHP bracket tags around the code because it was omitting some of the $this variables in my code They need to fix that 0_0 Hope you can read this

    Anyway, I come with two problems. First off, I'm wondering if I'm putting my methods in the best controllers. I made an Auth controller and put a bunch of methods in it.

    It has:
    signup() - public
    login() - public
    logout() - public
    activate() - public
    send_code() - private, used methods that send a code through email
    resend_code() - public, re-sends the activation code (utilizes send_code())
    change_email() - public, uses send_code() because it deactivates account and sends another activation code
    create_code() - private, creates a 10 character string of random letters and numbers, used for activation codes and other kinds of codes
    forgot_password() - public, accepts a login name, sends a password change code to email on file with account
    reset_password() - public, accepts a login name and password reset code, allows user to change password for account
    change_password() - public, lets somebody change their password the normal way, by entering their old password and the new password they want
    change_login_name() - public, haven't made yet but will let somebody change their login name

    So, Question #1 do all these belong in Auth?

    Now, I'm also wondering if I'm putting too much functionality in each of my methods and if I would be better off splitting the functionality into several methods. Auth::change_password() is a good example of the approach I've been taking. Here it is:

    public function change_password()
    {
    $this->data['title'] = 'Change Password';
    $this->data['captcha'] = $this->load_captcha();

    if (!$this->user_info['logged_in']) // can't change an account's password unless you're logged into it
    {
    $this->data['alert_type'] = 'problem'; //my view is set up so if alert_type and alert_message are set, a message box will display
    $this->data['alert_message'] = 'You have to be logged in to an account, if you want to change its password.';
    }
    else
    {
    if ($this->input->post('process'))
    {
    $this->load->model('auth_model');
    $login_name = $this->auth_model->get_user_info($this->user_info['user_id'], 'login_name'); //querys the database with SELECT `login_name` FROM `user_accounts` WHERE `user_id` = 'whatever id was entered'

    $this->load->library('form_validation');
    $this->form_validation->set_rules('current_password', 'Current Password', 'trim|required|max_length[25]');
    $this->form_validation->set_rules('new_password_1', 'New Password', 'trim|required|matches[new_password_2]|does_not_match['.$login_name.','.$this->user_info['display_name'].']|min_length[6]|max_length[25]'); //password can't be the same as login_name or display_name
    $this->form_validation->set_rules('new_password_2', 'Enter Again', 'trim|required');
    $this->form_validation->set_rules('recaptcha_response_field', 'Image Verification', 'required|captcha'); //captcha looks at what was entered and sees if it matches the text in the image verification

    $this->form_validation->set_message('does_not_match', 'Your new password cannot be the same as your display name or login name.');

    if (!$this->form_validation->run())
    {
    $this->data['alert_type'] = 'problem';
    $this->data['alert_message'] = validation_errors();
    $this->data['extra_brs'] = 'no';
    }
    elseif (!$this->auth_model->password_check($this->user_info['user_id'], $this->input->post('current_password')))
    {
    $this->data['alert_type'] = 'problem';
    $this->data['alert_message'] = 'What you put for your current password was wrong. Did you forget it? If so, <a href="http://'.$_SERVER['HTTP_HOST'].'/auth/forgot_password">click here</a>.';
    }
    else
    {
    $changes = array(
    'password' => md5($this->input->post('new_password_1'))
    );

    if ($this->auth_model->alter_user($this->user_info['user_id'], $changes)) //querys the database with UPDATE `user_info` SET `password` = md5(their new password) WHERE `user_id` = 'whatever their user id is'
    {
    $this->data['alert_type'] = 'success';
    $this->data['alert_message'] = 'Password successfully changed. Write it down and put it in a safe place.';
    }
    else
    {
    $this->data['alert_type'] = 'problem';
    $this->data['alert_message'] = 'Everything you entered was fine but there was a problem updating your account. This is unusual. Please send an email to **staff email address omitted** and explain what happened. so it can be looked into.';
    }

    }
    }

    $this->data['main_content'] = 'auth/change_password_form';
    }

    $this->load->view('templates/full_layout', $this->data); //templates/full_layout.php is below
    }


    templates/full_layout.php
    <?php if (!isset($user_info)) $user_info = ''; $this->load->view('templates/header', $user_info); ?>

    <?php if (isset($main_content)) $this->load->view($main_content); ?>

    <?php $this->load->view('templates/footer'); ?>

    Here is auth/change_password_form.php
    <form action='http://<?php print $_SERVER['HTTP_HOST']; ?>/auth/change_password' method='post'>
    <input type='hidden' name='process' value='true'>
    <table class='table rounded' width='500'>
    <tr>
    <td class='header1 rounded'><strong>Current Password:</strong></td>
    <td class='content1 rounded' width=''><input type='password' name='current_password' size='10' length='25' required /></td>
    </tr>
    <tr>
    <td class='header1 rounded'><strong>New Password:</strong></td>
    <td class='content1 rounded'><input type='password' name='new_password_1' size='10' length='25' required /></td>
    </tr>
    <tr>
    <td class='header1 rounded'><strong>Type Again:</strong></td>
    <td class='content1 rounded'><input type='password' name='new_password_2' size='10' length='25' required /></td>
    </tr>
    <tr>
    <td class='content1 rounded' colspan='2' align='center'><center><?php print $captcha; ?></center></td>
    </tr>
    </table>

    <input type='submit' class='button' value='Change Password'>
    </form>

    So, Question #2, did I take the right approach to the design of my change_password() method?
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  8. #158
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    To my mind, you probably need a User model class that would include methods for all your associated "crud" activities: create, read, update, and delete -- keeping all your direct interaction with the database there. Whether or not you then just have one User controller or separate controllers, (e.g. maybe User_Registration, User_Profile, and User_Auth) is probably not as cut-and-dried. Whichever way you go, if you find individual methods getting too long, look for both complex logic and/or duplicate logic that can be moved into separate methods (most likely private methods available only to that controller (or children)).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #159
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Well, I have an auth_model class. That's where the get_user_info() and alter_user() methods were coming from. Here's what's in them:

    auth_model::get_user_Info()
    public function get_user_info($user_id, $fields)
    {
    $this->db->where('user_id', $user_id);

    $this->db->select($fields);

    $query = $this->db->get('user_accounts');

    if ($query->num_rows() == 1)
    {
    if (is_array($fields)) implode(',', $fields);

    if (str_replace(',', '', $fields) == $fields) // return a string if just one item was pulled
    {
    $user_info = $query->row_array();
    return $user_info[$fields];
    }
    else //return an array if they pulled more than one item
    {
    return $query->row_array();
    }
    }
    else
    {
    return FALSE;
    }
    }

    auth_model::alter_user()
    public function alter_user($user_id, $changes)
    {
    $this->db->where('user_id', $user_id);
    $this->db->update('user_accounts', $changes);

    return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    What do you think? Should it be called user_auth_model?
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  10. #160
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    I want to use PHP to layer transparent PNG images on top of each other, and preserve their anti-aliasing. I need help writing the code that does this. The reason I want to do this with PHP and not CSS is complicated and I'm not going to bother to explain unless you ask. Anyway, I threw together a crude example:

    lines.png marking2.png marking1.png base.png

    The code I envison should be able to put them together into a single png that looks like this:

    alltogether.png

    I found some code on-line that sort of does this, albeit with some problems:
    PHP Code:
    /**
    * Compose a PNG file over a src file.
    * If new width/ height are defined, then resize the PNG (and keep all the transparency info)
    * Author: Alex Le - http://www.alexle.net
    */
    function imageComposeAlpha( &$src, &$ovr$ovr_x$ovr_y$ovr_w false$ovr_h false)
    {
    if( 
    $ovr_w && $ovr_h )
    $ovr imageResizeAlpha$ovr$ovr_w$ovr_h );

    /* Noew compose the 2 images */
    imagecopy($src$ovr$ovr_x$ovr_y00imagesx($ovr), imagesy($ovr) );
    }

    /**
    * Resize a PNG file with transparency to given dimensions
    * and still retain the alpha channel information
    * Author: Alex Le - http://www.alexle.net
    */
    function imageResizeAlpha(&$src$w$h)
    {
    /* create a new image with the new width and height */
    $temp imagecreatetruecolor($w$h);

    /* making the new image transparent */
    $background imagecolorallocate($temp000);
    ImageColorTransparent($temp$background); // make the new temp image all transparent
    imagealphablending($tempfalse); // turn off the alpha blending to keep the alpha channel

    /* Resize the PNG file */
    /* use imagecopyresized to gain some performance but loose some quality */
    imagecopyresized($temp$src0000$w$himagesx($src), imagesy($src));
    /* use imagecopyresampled if you concern more about the quality */
    //imagecopyresampled($temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
    return $temp;
    }


    header('Content-type: image/png');

    /* Open the photo and the overlay image */
    $photoImage ImageCreateFromJPEG('images/MiuMiu.jpg');
    $overlay ImageCreateFromPNG('images/hair-trans.png');

    $percent 0.8;
    $newW ceil(imagesx($overlay) * $percent);
    $newH ceil(imagesy($overlay) * $percent);

    /* Compose the overlay photo over the target image */
    imageComposeAlpha$photoImage$overlay8615$newW$newH );

    /* Open another PNG file, then resize and compose it */
    $watermark imagecreatefrompng('images/watermark.png');
    imageComposeAlpha$photoImage$watermark1020imagesx($watermark)/2imagesy($watermark)/);

    /**
    * Open the same PNG file then compose without resizing
    * As the original $watermark is passed by reference, it was resized already.
    * So we have to reopen it.
    */
    $watermark imagecreatefrompng('images/watermark.png');
    imageComposeAlpha$photoImage$watermark80350);
    Imagepng($photoImage); // output to browser

    ImageDestroy($photoImage);
    ImageDestroy($overlay);
    ImageDestroy($watermark); 
    The problems with this code are:
    -The anti-aliasing is not completely preserved. The edges aren't as soft.
    -It requires the base image to be a .jpg. If I change it to a ImageCreateFromPNG() function and put a .png in there, the anti-aliasing goes away entirely and I'm left with jagged edges.
    -It only layers two images. I'm going to need to be able to layer as many as 6 or so.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  11. #161
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    My first thought is to take a look at the ImageMagick extension to see if it performs any better for you. (Downside is that it's not always installed by default on many web servers.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #162
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    I cannot for the life of me remember how to extend inherited methods. Is this the right way?

    PHP Code:
    class Some_Class {

       public function 
    some_method($word)
       {
          print 
    $word;
       }
    }

    class 
    Whatever extends Some_Class {

       public function 
    some_method($word)
       {
          
    parent::some_method();
         
    //add more implementation after this
       
    }


    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  13. #163
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Looks okay to me with the exception that you would need to pass $word to the parent method when you call it (which, by the way, such a call is not required -- it's correct if you want to duplicate the entire functionality of the parent method at that point, but if not, then you would leave it out and just write entirely new functionality to override it).
    Last edited by NogDog; 10-20-2012 at 11:46 AM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  14. #164
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    #1 I want to make a page on my website that shows a list of all the users that have been active in the last 5 minutes. My dates are stored in time()'s format and they are in an int column that is called "last_active". PHP sends a query that updates this column every time the user visits a page.

    The main thing stumping me is I'm not sure what to put in the "WHERE" part of my MySQL query, nor do I know how to get PHP to generate anything that it might need to generate.

    #2 I also want to make a user's profile page say "Online" if they have been active in the last 5 minutes. Is there anything different I need to do?
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  15. #165
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    select whatever from users where last_active > time() - 300

    on profile page.. if $data["last_active"] > current_timestamp - 300 ... show_online();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles