• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Problem with User section

#3
Thanks for help but he say that error :

[Image: 1501601803-erreur.png]

my code :

User controller : 
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* User class.
*
* @extends CI_Controller
*/
class User extends CI_Controller {

    /**
     * __construct function.
     *
     * @access public
     * @return void
     */
    public function __construct() {
        
        parent::__construct();
        $this->load->library(array('session'));
        $this->load->helper(array('url'));
        $this->load->model('user_model');
        
    }
    
    
    public function index() {
        
         //If no session, redirect to login page
        redirect('home/');
        
        
    }
    
    /**
     * register function.
     *
     * @access public
     * @return void
     */
    public function register() {
        
        // create the data object
        $data = new stdClass();
        
        // load form helper and validation library
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        // set validation rules
        $this->form_validation->set_rules('pseudo', 'pseudo', 'trim|required|alpha_numeric|min_length[4]|is_unique[accounts.pseudo]', array('is_unique' => 'Ce pseudo est déjà choisis. Veuillez en choisir un nouveau.'));
        $this->form_validation->set_rules('mail', 'mail', 'trim|required|valid_email|is_unique[accounts.email]',
        array('is_unique' => 'L\'adresse mail est déjà utilisé.  Veuillez en choisir une nouvelle.'));
        $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[6]',
        array('min_length' => 'Le mot de passe doit comporter au moins 6 caractères.'));
        $this->form_validation->set_rules('conf_password', 'conf_password', 'trim|required|matches[password]',
        array('matches' => 'Les mots de passes ne sont pas identiques.'));
        
        if ($this->form_validation->run() === false) {
            
            // validation not ok, send validation errors to the view
            $this->load->view('layout/header');
            $this->load->view('user/register', $data);
            $this->load->view('layout/footer');
            
        } else {
            
            // set variables from the form
            $username = $this->input->post('pseudo');
            $email    = $this->input->post('mail');
            $password = $this->input->post('password');
            
            if ($this->user_model->create_user($username, $email, $password)) {
                
                // user creation ok
                $this->load->view('layout/header');
                $this->load->view('user/register_success', $data);
                $this->load->view('layout/footer');
                
            } else {
                
                // user creation failed, this should never happen
                $data->error = 'Un problème est surveneu lors de la création de votre compte. Veuillez réessayer.';
                
                // send error to the view
                $this->load->view('layout/header');
                $this->load->view('user/register', $data);
                $this->load->view('layout/footer');
                
            }
            
        }
        
    }
        
    /**
     * login function.
     *
     * @access public
     * @return void
     */
    public function login() {
        
        // create the data object
        $data = new stdClass();
        
        // load form helper and validation library
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        // set validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        
        if ($this->form_validation->run() == false) {
            
            // validation not ok, send validation errors to the view
            $this->load->view('layout/header');
            $this->load->view('user/login');
            $this->load->view('layout/footer');
            
        } else {
            
            // set variables from the form
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            
            if ($this->user_model->resolve_user_login($username, $password)) {
                
                $user_id = $this->user_model->get_user_id_from_username($username);
                $user    = $this->user_model->get_user($user_id);
                
                // set session user datas
                $_SESSION['user_id']        = (int)$user->guid;
                $_SESSION['username']       = (string)$user->pseudo;
                $_SESSION['email']          = (string)$user->email;
                $_SESSION['pseudo_dofus']   = (string)$user->pseudo_dofus;
                $_SESSION['pseudo_youtube'] = (string)$user->pseudo_youtube;
                $_SESSION['sexe']           = (string)$user->sexe;
                $_SESSION['travail']        = (string)$user->travail;
                $_SESSION['url_dofus']      = (string)$user->url_dofus;
                $_SESSION['logged_in']      = (bool)true;
                
                // user login ok
                $this->load->view('layout/header');
                $this->load->view('user/login_success', $data);
                $this->load->view('layout/footer');
                
            } else {
                
                // login failed
                $data->error = 'Mauvais nom d\'utlisateur ou mot de passe !';
                
                // send error to the view
                $this->load->view('layout/header');
                $this->load->view('user/login', $data);
                $this->load->view('layout/footer');
                
            }
            
        }
        
    }
    
    /**
     * logout function.
     *
     * @access public
     * @return void
     */
    public function logout() {
        
        // create the data object
        $data = new stdClass();
        
        if(!$this->session->has_userdata('guid')){
            
            // remove session datas
            foreach ($_SESSION as $key => $value) {
                unset($_SESSION[$key]);
            }
            
            // user logout ok
            $this->load->view('layout/header');
            $this->load->view('user/logout_success', $data);
            $this->load->view('layout/footer');
            
        } else {
            
            // there user was not logged in, we cannot logged him out,
            // redirect him to site root
            redirect('/');
            
        }
        
    }
    
         /**
    * account function.
    *
    * @access public
    * @return void
    */
    public function show_user($id)
    {
       $this->load->model('User_model');
       $data['accounts'] = $this->user_model->get_account($id);
       $this->load->view('user/account',$data);
    }

    
     /**
    * account function.
    *
    * @access public
    * @return void
    */
   public function account() {

        
        
        // load form helper and validation library
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        // set validation rules
        $this->form_validation->set_rules('pseudo', 'pseudo', 'required|alpha_numeric');
        $this->form_validation->set_rules('pseudo_dofus', 'pseudo_dofus', 'required');
        $this->form_validation->set_rules('email', 'email', 'required');
        $this->form_validation->set_rules('pseudo_youtube', 'pseudo_youtube', 'required');
        $this->form_validation->set_rules('sexe', 'sexe', 'required|alpha_numeric');
        $this->form_validation->set_rules('travail', 'travail', 'required');
        $this->form_validation->set_rules('url_dofus', 'url_dofus', 'required');
        
        
        if ($this->form_validation->run() == false) {
            
            // validation not ok, send validation errors to the view
            $this->load->view('layout/header');
            $this->load->view('user/account');
            $this->load->view('layout/footer');
            
        } else {
            
            // set variables from the form
            $pseudo = $this->input->post('pseudo');
            $pseudo_dofus = $this->input->post('pseudo_dofus');
            $email = $this->input->post('email');
            $pseudo_youtube = $this->input->post('pseudo_youtube');
            $sexe = $this->input->post('sexe');
            $travail = $this->input->post('travail');
            $url_dofus = $this->input->post('url_dofus');
                

            if ($this->user_model->update_user($pseudo, $pseudo_dofus, $email, $pseudo_youtube, $sexe, $travail, $url_dofus)) {
                
                // user account update
                $this->load->view('layout/header');
                $this->load->view('user/account_update');
                $this->load->view('layout/footer');
                
            } else {

           // there user was not logged in, we cannot logged him out,
           // redirect him to site root
           redirect('/');

       }

   }
    }
    
}


User_model :

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* User_model class.
*
* @extends CI_Model
*/
class User_model extends CI_Model {

    /**
     * __construct function.
     *
     * @access public
     * @return void
     */
    public function __construct() {
        
        parent::__construct();
        $this->load->database();
        
    }
    
    /**
     * create_user function.
     *
     * @access public
     * @param mixed $username
     * @param mixed $email
     * @param mixed $password
     * @return bool true on success, false on failure
     */
    public function create_user($username, $email, $password) {
        
        $data = array(
            'pseudo'   => $username,
            'email'      => $email,
            'pass'   => $this->hash_password($password),
        );
        
        return $this->db->insert('accounts', $data);
        
    }
    
    /**
     * resolve_user_login function.
     *
     * @access public
     * @param mixed $username
     * @param mixed $password
     * @return bool true on success, false on failure
     */
    public function resolve_user_login($username, $password) {
        
        $this->db->select('pass');
        $this->db->from('accounts');
        $this->db->where('pseudo', $username);
        $hash = $this->db->get()->row('pass');
        
        return $this->verify_password_hash($password, $hash);
        
    }
    
    /**
     * get_user_id_from_username function.
     *
     * @access public
     * @param mixed $username
     * @return int the user id
     */
    public function get_user_id_from_username($username) {
        
        $this->db->select('guid');
        $this->db->from('accounts');
        $this->db->where('pseudo', $username);

        return $this->db->get()->row('guid');
        
    }
    
    /**
     * get_user function.
     *
     * @access public
     * @param mixed $user_id
     * @return object the user object
     */
    public function get_user($user_id) {
        
        $this->db->from('accounts');
        $this->db->where('guid', $user_id);
        return $this->db->get()->row();
        
    }
    
    /**
     * hash_password function.
     *
     * @access private
     * @param mixed $password
     * @return string|bool could be a string on success, or bool false on failure
     */
    private function hash_password($password) {
        
        return password_hash($password, PASSWORD_BCRYPT);
        
    }
    
    /**
     * verify_password_hash function.
     *
     * @access private
     * @param mixed $password
     * @param mixed $hash
     * @return bool
     */
    private function verify_password_hash($password, $hash) {
        
        return password_verify($password, $hash);
        
    }
        /**
     * account function.
     *
     * @access public
     * @param mixed $username
     * @param mixed $email
     * @param mixed $password
     * @return bool true on success, false on failure
     */
    public function get_account($id)
    {
      $query = $this->db->where('guid',$id)->get('accounts');
      return $query->row();
    }
    
    /**
     * account_update function.
     *
     * @access public
     * @param mixed $username
     * @param mixed $email
     * @param mixed $password
     * @return bool true on success, false on failure
     */
    public function update_user($pseudo, $pseudo_dofus, $email, $pseudo_youtube, $sexe, $travail, $url_dofus) {
        
        $guid = $this->session->userdata('user_id');
        
        $data = array(
            
            'pseudo'         => $pseudo,
            'pseudo_dofus'   => $pseudo_dofus,
            'email'          => $email,
            'pseudo_youtube' => $pseudo_youtube,
            'sexe'           => $sexe,
            'travail'        => $travail,
            'url_dofus'      => $url_dofus,
        );
        
         $this->db->where('guid', $guid);

         return $this->db->update('accounts', $data);
        
    }
    
}


and the user views : 

Code:
<?php if (isset($_SESSION['username']) && $_SESSION['logged_in'] === true) : ?>

<!--=== Content Part ===-->    
<div class="container content">    
        <div class="row">
            <?php if (validation_errors()) : ?>
                <div class="col-md-12">
                    <div class="alert alert-danger" role="alert">
                        <?= validation_errors() ?>
                    </div>
                </div>
            <?php endif; ?>
            <?php if (isset($error)) : ?>
                <div class="col-md-12">
                    <div class="alert alert-danger" role="alert">
                        <?= $error ?>
                    </div>
                </div>
            <?php endif; ?>    
     </div>
</div>

<!-- -->
<section>
    <div class="container">

        <!-- RIGHT -->
        <div class="col-lg-9 col-md-9 col-sm-8 col-lg-push-3 col-md-push-3 col-sm-push-4 margin-bottom-80">

            <ul class="nav nav-tabs nav-top-border">
                <li class="active"><a href="#info" data-toggle="tab">Informations personelles</a></li>
                <li><a href="#avatar" data-toggle="tab">Avatar</a></li>
                <li><a href="#password" data-toggle="tab">Changer mon mot de passe</a></li>
                
            </ul>

            <div class="tab-content margin-top-20">

                <!-- PERSONAL INFO TAB -->
                <div class="tab-pane fade in active" id="info">
                <?php foreach($accounts as $account) { ?>
                <?= form_open() ?>
                    <form role="form" action="#" method="post">
                        <div class="form-group">
                            <label class="control-label">Pseudo*</label>
                            <input type="text" value="<?= $account->pseudo;?>" class="form-control" name="pseudo">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Pseudo Dofus Touch*</label>
                            <input type="text" value="<?= $this->session->userdata('pseudo_dofus'); ?>" class="form-control" name="pseudo_dofus">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Email*</label>
                            <input type="text" value="<?= $this->session->userdata('email'); ?>" class="form-control" name="email">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Pseudo Youtube*</label>
                            <input type="text" value="<?= $this->session->userdata('pseudo_youtube'); ?>" class="form-control" name="pseudo_youtube">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Sexe</label>
                            <input type="text" value="<?= $this->session->userdata('sexe'); ?>" class="form-control" name="sexe">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Travail/Études irl</label>
                            <input type="text" value="<?= $this->session->userdata('travail'); ?>" class="form-control" name="travail">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Url vers votre page perso dofus</label>
                            <input type="text" value="<?= $this->session->userdata('url_dofus'); ?>" class="form-control" name="url_dofus">
                        </div>
                        <div class="margiv-top10">
                            <button class="btn btn-primary"><i class="fa fa-check"></i>Confirmer les changements</button>
                        </div>
                    </form>
                    <?php } ?>
                    <p>* : champs obligatoires</p>
                </div>
                <!-- /PERSONAL INFO TAB -->

                <!-- AVATAR TAB -->
                <div class="tab-pane fade" id="avatar">

                    <form class="clearfix" action="#" method="post" enctype="multipart/form-data">
                        <div class="form-group">

                            <div class="row">

                                <div class="col-md-3 col-sm-4">

                                    <div class="thumbnail">
                                        <img class="img-responsive" src="<?= img_url('avatar/default.jpg') ?>" alt="" />
                                    </div>

                                </div>

                                <div class="col-md-9 col-sm-8">

                                    <div class="sky-form nomargin">
                                        <label class="label">Selectionner un fichier dans l'ordinateur</label>
                                        <label for="file" class="input input-file">
                                            <div class="button">
                                                <input type="file" id="file" onchange="this.parentNode.nextSibling.value = this.value">Parcourir
                                            </div><input type="text" readonly>
                                        </label>
                                    </div>

                                    <a href="#" class="btn btn-danger btn-xs noradius"><i class="fa fa-times"></i> Supprimer l'avatar</a>

                                </div>

                            </div>

                        </div>

                        <div class="margiv-top10">
                            <a href="#" class="btn btn-primary">Confirmer les changements </a>
                            <a href="#" class="btn btn-default">Retour </a>
                        </div>
                        </form>
                        <p>* : champs obligatoires</p>
                        </div>
                <!-- /AVATAR TAB -->

                <!-- PASSWORD TAB -->
                <div class="tab-pane fade" id="password">

                    <form action="#" method="post">

                        <div class="form-group">
                            <label class="control-label">Mot de passe actuel*</label>
                            <input type="password" class="form-control">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Nouveau mot de passe*</label>
                            <input type="password" class="form-control">
                        </div>
                        <div class="form-group">
                            <label class="control-label">Re taper le nouveau mot de passe*</label>
                            <input type="password" class="form-control">
                        </div>

                        <div class="margiv-top10">
                            <a href="#" class="btn btn-primary"><i class="fa fa-check"></i> Changer mon mot de passe</a>
                            <a href="#" class="btn btn-default">Retour </a>
                        </div>

                    </form>
                    
                    <p>* : champs obligatoires</p>

                </div>
                <!-- /PASSWORD TAB -->

                <!-- PRIVACY TAB -->
                <div class="tab-pane fade" id="privacy">

                    <form action="#" method="post">
                        <div class="sky-form">

                            <table class="table table-bordered table-striped">
                                <tbody>
                                    <tr>
                                        <td>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam.</td>
                                        <td>
                                            <div class="inline-group">
                                                <label class="radio nomargin-top nomargin-bottom">
                                                    <input type="radio" name="radioOption" checked=""><i></i> Yes
                                                </label>

                                                <label class="radio nomargin-top nomargin-bottom">
                                                    <input type="radio" name="radioOption" checked=""><i></i> No
                                                </label>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam.</td>
                                        <td>
                                            <label class="checkbox nomargin">
                                                <input type="checkbox" name="checkbox" checked=""><i></i> Yes
                                            </label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam.</td>
                                        <td>
                                            <label class="checkbox nomargin">
                                                <input type="checkbox" name="checkbox" checked=""><i></i> Yes
                                            </label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam.</td>
                                        <td>
                                            <label class="checkbox nomargin">
                                                <input type="checkbox" name="checkbox" checked=""><i></i> Yes
                                            </label>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

                        </div>

                        <div class="margin-top-10">
                            <a href="#" class="btn btn-primary"><i class="fa fa-check"></i> Save Changes </a>
                            <a href="#" class="btn btn-default">Cancel </a>
                        </div>

                    </form>

                </div>
                <!-- /PRIVACY TAB -->

            </div>

        </div>

        
        <!-- LEFT -->
        <div class="col-lg-3 col-md-3 col-sm-4 col-lg-pull-9 col-md-pull-9 col-sm-pull-8">
        
            <div class="thumbnail text-center">
                <img src="<?= img_url('avatar/default.jpg') ?>" alt="" />
                <h2 class="size-18 margin-top-10 margin-bottom-0"><?= $this->session->userdata('username'); ?></h2>
                <h3 class="size-11 margin-top-0 margin-bottom-10 text-muted">DEVELOPER</h3>
            </div>

            


            <!-- info -->
            

        </div>
        
    </div>
</section>
<!-- / -->

   <?php else :  redirect('/'); ?>
   
   <?php endif; ?>
Reply


Messages In This Thread
[Solved] Problem with User section - by wendaf - 07-31-2017, 02:05 PM
RE: Problem with User section - by Wouter60 - 08-01-2017, 06:45 AM
RE: Problem with User section - by wendaf - 08-01-2017, 08:37 AM
RE: Problem with User section - by Wouter60 - 08-01-2017, 11:11 AM
RE: Problem with User section - by wendaf - 08-01-2017, 02:00 PM
RE: Problem with User section - by jarmen_kell - 08-01-2017, 06:52 PM
RE: Problem with User section - by wendaf - 08-02-2017, 02:18 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.