CodeIgniter Forums
Session Problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Session Problem (/showthread.php?tid=27487)



Session Problem - El Forum - 02-12-2010

[eluser]pisio[/eluser]
hi all. I have a problem.Can you help me ?
Code:
<?php
class user extends Controller {

    function user()
    {
        parent::Controller();
        header('Content-Type: text/html; charset=utf-8');
        $this->load->database(); //Активиране на базата данни.
        $msg['null']  =  'is null to debug'; // Ако няма стойност на $MSG  то тази прекратява проблемите.
     }//main function  user

    function register()
    {
        $msg['null']  =  'is null to debug'; // Ако няма стойност на $MSG  то тази прекратява проблемите.
        $reg['name']     =   $this->input->post('name');
        $reg['pass1']    =   $this->input->post('pass1');
        $reg['pass2']    =   $this->input->post('pass2');
        $reg['mail']     =   $this->input->post('mail');

        if($this->uri->segment(3) == 'go')
        {
            $this->form_validation->set_rules('name', 'Username', 'trim|required|min_length[5]|max_length[12]');
            $this->form_validation->set_rules('pass1','Password', 'trim|required|matches[pass2]|md5');
            $this->form_validation->set_rules('pass2','PassworD', 'trim|required');
            $this->form_validation->set_rules('mail', 'Mail', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                    $msg['error'] = 1;
            }
            else
            {
                //Проверка дали съществува  името.
                $query = $this->db->get_where('users', array('name' => $reg['name']), 1);
                if($query->num_rows == 0){

                    $data = array(
                        'name'      =>  $reg['name'],
                        'pass'      =>  md5($reg['pass1']),
                        'mail'      =>  $reg['mail'],
                        'avatar'    =>  '',
                        'signature' =>  '',
                        'notepad'   =>  '',
                        'ip_reg'    =>  $this->input->ip_address(),
                        'date_rag'  =>  time()
                    );
                   echo  $this->db->insert('users', $data);
                   $msg['error'] = 0;

                }
                else
                {
                    $msg['error'] = 2;
                }
            }//else
        }
        else
        {
            $msg['error'] = 1;
        }
        $this->load->view('user/register',$msg);
    }//function register


    function login()
    {
        $this->form_validation->set_rules('name', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('pass1', 'Password', 'trim|required|md5');

        if ($this->form_validation->run() == FALSE)
        {
                $msg['error'] = 1;
        }
        else
        {
                echo "minata proverka <br/>";
                $log['name'] = $this->input->post('name');
                $log['pass'] = $this->input->post('pass1');

                $q = $this->db->get_where('users',array('name'=> $log['name'],'pass'=>$log['pass']));
                
                if($q->num_rows == 1)
                {
                    $row = $q->row();
                    $newdata = array(
                                  'time' => time(),
                                  'ip'   => $this->input->ip_address(),
                                  'name' => $row->name
                    );
                    
                    $this->session->set_userdata($newdata);
                    $msg['error'] = 0;

                }
                else
                {  
                    $msg['error'] = 2;
                }
        }
        $this->load->view('user/login',$msg);
    }// function login
}
autoload.php ->
Code:
$autoload['libraries'] = array('database','session','form_validation');


/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|    $autoload['helper'] = array('url', 'file');
*/

$autoload['helper'] = array('form','html','url','email');



And Error is :
Quote:A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\konkurs_1\system\application\controllers\user.php:76)

Filename: libraries/Session.php

Line Number: 662



Session Problem - El Forum - 02-12-2010

[eluser]bretticus[/eluser]
line 76:
Code:
echo "minata proverka <br/>";
you later call
Code:
$this->session->set_userdata($newdata);
on line 91.

That'd be my guess. Wink


Session Problem - El Forum - 02-12-2010

[eluser]pisio[/eluser]
[quote author="bretticus" date="1266001381"]line 76:
Code:
echo "minata proverka <br/>";
you later call
Code:
$this->session->set_userdata($newdata);
on line 91.

That'd be my guess. Wink[/quote]
Can you explain more ? I can't understand you.
:S


Session Problem - El Forum - 02-12-2010

[eluser]bretticus[/eluser]
Google your error. This is a basic PHP and cookie problem.


Session Problem - El Forum - 02-12-2010

[eluser]pisio[/eluser]
[quote author="bretticus" date="1266017935"]Google your error. This is a basic PHP and cookie problem.[/quote]
And Can i fix it ?


Session Problem - El Forum - 02-14-2010

[eluser]Zorancho[/eluser]
It means that your HTTP headers are already being sent, so you cannot generate further output or modify header's information.
One fix to this is which is not very elegant is to open your root index.php file and on the very top add this:
Code:
ob_start();
Other fix is as bretticus said to remove the output on line 76 cause you are setting
Code:
$this->session->set_userdata($newdata);
later on.
Also, when the user logs in with the correct information it is good idea to redirect the user to the protected page instead to use different views.
Code:
if($this->form_validation->run() == false)
{
   //Load the form again, but this time with errors data.
   $this->load->view('form', $data)
}
else
{
   if(user_logged_in())
   {
      redirect('protected_page');
   }
   else
   {
     redirect('to_the_login_page');
   }
}