Welcome Guest, Not a member yet? Register   Sign In
please i cant understand session
#1

[eluser]mrphp[/eluser]
any one please explain ci session to me because i red the user guide and i cant understand session from it
#2

[eluser]Tri Dang[/eluser]
Greetings!

Can you let others know what particular points that you don't understand?
#3

[eluser]mrphp[/eluser]
OK in this simple code i want in every page check for session

class home extends controller
{
function index()
{
/*if session echo hello */

echo anchor(index.php/home/login);
}

function login ()
{
$name=$_POST['name'];
$pass=$_POST['password'];
$users = array('username' => $_POST['name'],'logged_in' => TRUE);
$sql=$this->db->query("select * from users where user_name='$name' and password='$pass'");
if($sql->num_rows()==1)
{
echo "hello".$name ;
echo anchor('index.php','back to home');
}

else
{
echo "wrong user name or password";
}

}
#4

[eluser]whobutsb[/eluser]
[quote author="mrphp" date="1232497805"]OK in this simple code i want in every page check for session

class home extends controller
{
function index()
{
/*if session echo hello */

echo anchor(index.php/home/login);
}

[/quote]

Well the first thing you want to do is load up the session.
Code:
$this->load->library('session');

Once you've done that or you've autoloaded it in your config. You can start working with CI sessions. Next you want to check a session.

Code:
$mySession = $this->session->userdata('mySession');
//If the session is empty set it
if(!empty($mySession)){
   $this->session->set_userdata('mySession', 'myInformation');
}else{
//Display hello because its not empty and there is information associated with it.
   echo "hello";
//Or echo whats in the session.
  echo $mySession;
}

If you are trying to write a login script to make sure the person is logged in. I usually add this to the top of my controller in the constructor.

Code:
if(!this->session->userdata('logged_in')){
   redirect('home');
}
#5

[eluser]mrphp[/eluser]
iam sorry i think iam stupid in this point but if you can to put a full example in using session it will be greate

as i mention i have two function index and login
#6

[eluser]whobutsb[/eluser]
They are easy. Two basic functions reading and writing.

To write a session you use:
Code:
$this->session->set_userdata('variable_name_of_session', 'value_of_session');

To read a session:
Code:
$this->session->userdata('variable_name_of_session');

Thats pretty much all there is to it.
#7

[eluser]Michael Wales[/eluser]
Code:
class Home extends Controller {

  function Home() {
    $this->load->library('session');
  }

  function index() {
    $this->session->set_userdata(array('text' => 'This is some text'));
  }

  function login() {
    if ($this->session->userdata('text') !== FALSE) {
      echo $this->session->userdata('text');
    } else {
      echo 'No session was found';
    }
  }
}
#8

[eluser]whobutsb[/eluser]
[quote author="Michael Wales" date="1232510115"]
Code:
class Home extends Controller {

  function Home() {
    $this->load->library('session');
  }

  function index() {
    $this->session->set_userdata(array('text' => 'This is some text'));
  }

  function login() {
    if ($this->session->userdata('text') !== FALSE) {
      echo $this->session->userdata('text');
    } else {
      echo 'No session was found';
    }
  }
}
[/quote]

Nicely explained.
#9

[eluser]mrphp[/eluser]
i add a link in the index function to go to login page
in login page appear 'No session was found' but i think it would be 'This is some text'
Quote:class Home extends Controller {

function Home() {
$this->load->library('session');
}

function index() {
$this->session->set_userdata(array('text' => 'This is some text'));
echo anchor('index.php/home/login','login');
}

function login() {
if ($this->session->userdata('text') !== FALSE)
{
echo $this->session->userdata('text');
}
else
{
echo 'No session was found';
}
}
}
#10

[eluser]mrphp[/eluser]
ok I modify the code and it work with me
Quote:class Home extends Controller {

function Home()
{
$this->load->library('session');
$this->session->set_userdata(array('text' => 'This is some text'));
}

function index()
{
echo anchor('index.php/home/login','login');
}

function login() {
if ($this->session->userdata('text') )
{
echo $this->session->userdata('text');
}
else
{
echo 'No session was found';
}
}
}

now the code work very good
but i want to change $this->session->set_userdata(array('text' => 'This is some text'));
to $this->session->set_userdata(array('text' =>$this->input->post('name'));
post('name') will hold data in one page only but i want to have the data in every page




Theme © iAndrew 2016 - Forum software by © MyBB