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

Member Avatar for diafol
sesson_start()

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

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

its with codeigniter by the way.

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.

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();

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

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

commented: Missed the CI bit! :) +14

Thanks Cereal..Am going to try this :)

the session destroy was also in the same config folder as well

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...

Oki..thanks ..am going to try this :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.