0

Hello guys
Am still a newbie in php in codeigniter
Hmm, am creating a website..for that when a user log in...the session has to be kept..while she is browising between several page in the website.
how can i include this? Header or something?
I have already do the login part..
If code is neeeded, i can paste it here..

Please help me..
Thanks

7
Contributors
11
Replies
54
Views
4 Years
Discussion Span
Last Post by shakayu
Featured Replies
  • 2
    cereal 1,419   4 Years Ago

    Are you using CodeIgniter Session library? If yes then autoload it, go to `/application/config/autoload.php` and go to **Auto-load libraries** block: $autoload['libraries'] = array('database','session'); Otherwise load it inside the construct of the Controller. For example, use this controller: class Test extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('session'); } # … Read More

0
sesson_start()

At the top of each page. Set and get with $_SESSION[...]

Edited by diafol

0

can you elaborate?
please...
i should put it in all pages?

0

add session_start() method in your first line of php of each page.Session will be started.If you want to use data from particular session variable,call it but make sure session_start() mthod is called before it.

0

On each page where you want to keep track of the session, you need to do as diafol said, initialise the session, like so;

<?php
session_start();
0

how can i dispay the username at top of the page then..
Like SignIn-SignOut
Then
When user log ing
its Like TheUsername-signOut

A login header kind of :s

2

Are you using CodeIgniter Session library? If yes then autoload it, go to /application/config/autoload.php and go to Auto-load libraries block:

$autoload['libraries'] = array('database','session');

Otherwise load it inside the construct of the Controller. For example, use this controller:

class Test extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
    }

    # your functions here

    public function index()
    {
        $d['name'] = $this->session->userdata('name');
        $this->load->view('/test/index',$d);
    }

    public function name($name)
    {
        $data = array(
            'name' => $name
            );
        $this->session->set_userdata($data);
        redirect('/test');
    }

}

Inside /application/views/test/index.php write:

<?php

    echo $name;

?>

Load http://localhost/test/name/shakayu

The application loads Test::name() and registers the value to CI session, after that you are redirected to the index and you get the saved value. This is just an example, when you do the log in process, just retrieve the name of the user from the database and save the value to session by using $this->session->set_userdata($array);

More info: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Votes + Comments
Missed the CI bit! :)
0

This is what I used to passing session to any other page. You can have some idea of it.

For this example, this will only passing one session to the other page.
*Note that it should be working but I didnt test it yet as this is just a quick example pop into my head.

//on top of every pages that you need the session.
<?php
session_start();
?>

---testsession.php---
<?php
session_start();
?>

//setting the session.
<?php
$_SESSION['test'] = "1234";
?>

---viewsession.php---
<?php
session_start();
?>
//view is the session passing successfully.
<?php
$_SESSION['test'] = $test;
echo $test;
?>

---destroysession.php---
<?php
session_start();
?>
//destroy a selected session.
<?php
unset($_SESSION['test']);
?>

--------------------------------

as your question for displaying the logged user name then you can do it easily. (I am showing the simple way.)

If login successful than you should be redirecting the user to another page right?

if so than in the if statement just add another $_SESSION['user'] = $user; (I assume you passing the user name to the database is $user)

and at the other page where you want to display the name than you can use echo $_SESSION['user'];

this is just a simple way to it for beginner.

other method such as cookies can do the same thing.

etc...

Edited by yy886: added some details

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.