Welcome Guest, Not a member yet? Register   Sign In
using sessions from one function to another
#1

[eluser]Prasad.P[/eluser]
Hi everybody,

we are trying to create Captcha and store the captcha word using session. The Captcha is generated in one function and the validation is done in another function, problem here is the session what we create in first function is not accessible in the other function. Here are the two functions

class Captchacontroller1 extends Controller
{
function Captchacontroller1()
{
parent::controller();
$this->load->helper('url');
$this->load->helper('form');
$this->load->plugin('captcha');
$this->load->library('session');
}
function index()
{
$vals = array(
'img_path' => './captcha/',
'img_url' => base_url().'captcha/',
'font_path' => './system/fonts/gara.ttf',
'img_width' => '90',
'img_height' => 24
);

$captcha = create_captcha( $vals );
$this->session->set_userdata('foo',$captcha['word']);
$data['title']="Captcha";
$data['heading']="Creating Captcha";
$data['image'] = $captcha['image'];
$this->load->view('captchaview',$data);
}
function printtxt()
{
echo $this->session->userdata('foo');
}
}


Thanks in Advance.
#2

[eluser]gtech[/eluser]
have you tried

Code:
$this->session->set_userdata(array('foo'=>$captcha['word']));

[strike]as I believe set_userdata takes an associative array. [/strike]
[edit]actually your method should work after reading the user guide[/edit]

what I would do is create a simple test controller

Code:
<?php
class Temp extends Controller {

  function Temp()
  {
    parent::Controller();
    $this->load->library('session');
  }
  function index()
  {
    $this->session->set_userdata('testkey','testvalue');
    echo 'set session';
  }
  function getsess() {
    echo $this->session->userdata('testkey');
  }
}
?>

the above code worked for me, you could browse to

http://<ciinstall>/index.php/temp
and then
http://<ciinstall>/index.php/temp/getsess

you should see 'testvalue' echoed to the browser.. if not then I suggest turning the log threashold on in your config and see if you are getting any errors.
#3

[eluser]Kromack[/eluser]
Does sessions are working fine in your server with non CI Script ?
#4

[eluser]Prasad.P[/eluser]
Hi GTECH,

Thank you very much for your reply.

I checked with your temp controller. I found that i have some problem with sessions in my system.

When i executed this link "http://<ciinstall>/index.php/temp"

I got the following error log:

DEBUG - 2008-05-21 20:11:13 --&gt; Config Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Hooks Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; URI Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Router Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Output Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Input Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2008-05-21 20:11:13 --&gt; Language Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Loader Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Database Driver Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Controller Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; Session Class Initialized
DEBUG - 2008-05-21 20:11:13 --&gt; A session cookie was not found.
ERROR - 2008-05-21 20:11:13 --&gt; Severity: Warning --&gt; Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\protest\system\application\controllers\temp.php:18) C:\xampp\htdocs\protest\system\libraries\Session.php 295
ERROR - 2008-05-21 20:11:13 --&gt; Severity: Warning --&gt; Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\protest\system\application\controllers\temp.php:18) C:\xampp\htdocs\protest\system\libraries\Session.php 295
DEBUG - 2008-05-21 20:11:13 --&gt; Final output sent to browser
DEBUG - 2008-05-21 20:11:13 --&gt; Total execution time: 0.1001


When i executed this link "http://<ciinstall>/index.php/temp/getsess"

I got the following error log:

DEBUG - 2008-05-21 20:11:27 --&gt; Config Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Hooks Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; URI Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Router Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Output Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Input Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2008-05-21 20:11:27 --&gt; Language Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Loader Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Database Driver Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Controller Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; Session Class Initialized
DEBUG - 2008-05-21 20:11:27 --&gt; A session cookie was not found.
ERROR - 2008-05-21 20:11:27 --&gt; Severity: Warning --&gt; Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\protest\system\application\controllers\temp.php:18) C:\xampp\htdocs\protest\system\libraries\Session.php 295
DEBUG - 2008-05-21 20:11:27 --&gt; Final output sent to browser
DEBUG - 2008-05-21 20:11:27 --&gt; Total execution time: 0.0978


Can u plz help me in tracing this problem.

Thanks in advance.
#5

[eluser]gtech[/eluser]
1st check you have no spaces outside the &lt;?php ?&gt; tags as that will cause the headers already being sent message. I am guessing that from the error message you have a space after the closing tag on line 18.

infact that is your problem as when I put a space after my ?&gt; tag I get exactly the same error message.
#6

[eluser]Prasad.P[/eluser]
Hi GTECH,

Thank you very much for your fast reply.

I have checked the code, there is no such space. Whatever the code you had given above as TEMP Controller, i just copied and pasted it.

&lt;?php
class Temp extends Controller {

function Temp()
{
parent::Controller();
$this->load->library('session');
}
function index()
{
$this->session->set_userdata('testkey','testvalue');
echo 'set session';
}
function getsess() {
echo $this->session->userdata('testkey');
}
}
?&gt;
#7

[eluser]gtech[/eluser]
Code:
&lt;?php
class Temp extends Controller {

function Temp()
{
parent::Controller();
$this->load->library('session');
}
function index()
{
$this->session->set_userdata('testkey','testvalue');
echo 'set session';
}
function getsess() {
echo $this->session->userdata('testkey');
}
}
?&gt; <-CHECK THERE IS NO SPACE HERE as cutting and pasting will produce a space!
I will try to explain

when you use the session it is set by sending header information to the browser.. when you display content this also sends header information to the browser.

So if you echo content or load a view before you set the session information you will get the headers already sent error message you are seeing..

what will also cause headers to be sent is any characters outside the php tags.. as your webserver will not send information to the browser that is inside the php tags.. but characters outside the tags will be sent to the browser, causing headers to be sent also.

hope this helps to explain your error. your error message is at line 18 which is suspiciously the end of the file!
#8

[eluser]Prasad.P[/eluser]
Hi gTech,

Yup it is working fine now.

Tons of "Thanks to U". It was a silly mistake, thanks for bringing it to notice.

Thanks U very much again.




Theme © iAndrew 2016 - Forum software by © MyBB