2

I'm fairly new to Codegniter framework. I wanna set a session to my website but as soon as I hit the back button of my browser it goes to the main(login) page of my website and when I click next the page shows not found. Also, on the welcome page I've a user button when I hit that too it takes me to my login page and yet the session data is still there.

Here is the code for my controller(Welcome.php):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {


    public function index()
    {
        $this->home();
    }

    public function home()
    {
        if($this->session->all_userdata('is_logged_in')==1){
            $this->load->view('welcome_message');
        }
        else{
            $data['title'] = 'ABC';
            $this->load->view('login', $data);
        }
    }

function loginapi()
{       
    if ($this->session->all_userdata('is_logged_in')==1) {
        $this->load->view('welcome_message', $this->session->all_userdata);
    }
    else{
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('password','Password','required');
        if ($this->form_validation->run()) {
            $username = $this->input->post('username');
            $password = $this->input->post('password');    
            $payload = json_encode( array( "username"=> $username, 
 "password"=> $password ) );
            $curl_handle = curl_init();
            curl_setopt($curl_handle, CURLOPT_URL, 
 '../api/dashboard/login');
            curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl_handle, CURLOPT_POST, 1);
            curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $payload);

            $buffer = curl_exec($curl_handle);
            $httpcode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);

            curl_close($curl_handle);
            var_dump($httpcode);
            $result = json_decode($buffer);
            var_dump($result->empid);

            if($httpcode == 200)
            {
                echo 'User has been updated.';
                $this->load->driver('session');
                //return $result;
                $data['empid'] = $result->empid;
                $data['username']=$this->input->post('username');
                $data['password']=$this->input->post('password');
                $data['title'] = 'ABC';
                $data['is_logged_in'] = '1';
                $this->session->set_userdata($data);

                $this->load->view('welcome_message', $data);
            }

            else
            {
                echo 'Something has gone wrong';
            }
        }
    else{
        $this->load->view('login'); 
        }
    }
}

function getdata()
{       
            if ($this->session->all_userdata('is_logged_in')==1) {

            $curl_handle = curl_init();
            curl_setopt($curl_handle, CURLOPT_URL, 
'../api/dashboard/getUser');
            curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
            $buffer = curl_exec($curl_handle);
            $httpcode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);

            curl_close($curl_handle);
            var_dump($httpcode);
            $result = json_decode($buffer);
            var_dump($result);

            if($httpcode == 200)
            {

                //return $result;
                $data['response'] = $result;
                $this->load->view('hr', $data);
             }

            else
            {
                echo 'Something has gone wrong';
            }
        }
        else{
            $this->load->view('login');
    }
}
 public function logout(){
    $this->session->sess_destroy();
    redirect('welcome/home');
}

}

My session data looks like this:

{
  '__ci_last_regenerate' => int 1493105150
  'empid' => string '13' (length=2)
  'username' => string 'user@example.xyz' (length=18)
  'password' => string 'password' (length=6)
  'title' => string 'ABC' (length=12)
  'is_logged_in' => string '1' (length=1)
}

My login.php View:

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
?>
  <!DOCTYPE html>
  <html lang="en">

  <head>
    <meta charset="utf-8">
    <title>
      <?php echo $title; ?>
    </title>


  </head>

  <body>

    <div id="container">
      <h1>
        <?php echo "login"; ?>
      </h1>

      <?php

    echo form_open('welcome/loginapi');
    echo validation_errors();
    echo "<p>Username: ";
    echo form_input('username');
    echo "</p>";

    echo "<p>Password: ";
    echo form_password('password');
    echo "</p>";

    echo "<p>";
    echo form_submit('login_submit','Login');
    echo "</p>";
    echo form_close();
    var_dump($this->session->all_userdata());
    ?>

        <p class="footer">Page rendered in <strong>{elapsed_time}</strong> 
seconds.
          <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter 
Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
    </div>

  </body>

  </html>

My welcome_message.php view

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8">
        <title><?php echo $title; ?></title>
</head>
<body>

<div id="container">
    <h1><?php echo "welcome page"; ?></h1>

    <div id="body">
        <?php           
            //var_dump($empid);
            var_dump($this->session->all_userdata('password'));
            echo form_open('welcome/getdata');
            echo form_hidden('_hidden_field',$this->session-
 >all_userdata('empid'));
            echo "<p>";
            echo form_submit('get_user','Users');
            echo "</p>";
            echo form_close();
        ?>
        <?php           
            echo form_open('welcome/logout');
            echo "<p>";
            echo form_submit('logout','Logout');
            echo "</p>";
            echo form_close();
        ?>
    </div>

    <p class="footer">Page rendered in <strong>{elapsed_time}</strong> 
seconds. <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter Version 
<strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>

</body>
</html>

Edit: when I click on the users button it takes me to hr.php via getdata function and when i go back it take me to welcome_message.php but from welcome_message.php when i press forward button it tell me to resubmit

This is my hr.php view:

 <?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 ?><!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="utf-8">
     <title><?php echo $title; ?></title>
 </head>
 <body onunload="">

 <div id="container">
     <h1><?php echo "Human Resource Management"; ?></h1>

     <div id="body">
         <div class="row">
                 <div class="col-lg-12">
                     <h1>Human Resource Management</h1>
                     <?php          
             echo form_open('welcome/registeruser');
             echo "<p>";
             echo form_submit('get_user','Register');
             echo "</p>";
             echo form_close();
         ?>
                 <br>
                 <br>
                 </div>
             </div>
         <div class="row">
                 <div class="col-lg-12">
                     <div class= "panel panel-default">
                         <div class="panel-heading">
                             <h4>Registered Employees</h4>
                         </div>
                         <div class="panel-body">
                         <table class="table table-hover">
                         <thead>
                             <tr>
                                 <th>Name</th>
                                 <th>Gender</th>
                                 <th>E-mail</th>
                                 <th>Contact No.</th>
                                 <th>Username</th>
                                 <th>Emp ID</th>
                                 <th>Department</th>
                                 <th>Designation</th>

                             </tr>
                         </thead>
                         <tbody>
                 <?php
                  foreach ($response as $object) {
                  ?>

                 <tr>

                     <td><?php echo $object->firstname; ?>&nbsp<?php echo 
  $object->lastname; ?></td>
                     <td><?php echo $object->gender; ?></td>
                     <td><?php echo $object->email; ?></td>
                     <td><?php echo $object->contactno; ?></td>
                     <td><?php echo $object->username; ?></td>
                     <td><?php echo $object->empid; ?></td>
                     <td><?php echo $object->department; ?></td>
                     <td><?php echo $object->designation; ?></td>
                     <td><?php          
             echo form_open('welcome/deleteuser');
             echo $object->empid;
             $empid=$object->empid;
             $department=$object->department;
             echo form_hidden('empid',$empid);
             echo form_hidden('department',$department);
             echo "<p>";
             echo form_submit('get_user','Delete');
             echo "</p>";
             echo form_close();
         ?></td>

                 </tr>

                   <?php
                 }?>
                         </tbody>    
                     </table>
                         </div>
                     </div>
                 </div>
             </div>
     </div>

     <p class="footer">Page rendered in <strong>{elapsed_time}</strong> 
 seconds. <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter 
 Version 
 <strong>' . CI_VERSION . '</strong>' : '' ?></p>
 </div>

 </body>
 </html>

I wanna maintain session throughout my website until the user opts to Logout(destroy it). Thank you in advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
Jayesh Rohira
  • 77
  • 2
  • 8
  • all_userdata() is DEPRECATED for more read [here](https://www.codeigniter.com/user_guide/libraries/sessions.html) – Gopal Bhuva Apr 25 '17 at 07:55

2 Answers2

3

you need use $this->session->userdata('is_logged_in') for get session value

Mahdi Majidzadeh
  • 798
  • 1
  • 14
  • 27
  • this solved on problem of mine. Thank you. I also want my website to go back and forth between pages by clicking back and forward buttons in the browser but it always says me to resubmit the form – Jayesh Rohira Apr 25 '17 at 07:53
  • you need to refresh your page while clicking on back button. Refer this [question](http://stackoverflow.com/questions/20899274/how-to-refresh-page-on-back-button-click) – Gopal Bhuva Apr 25 '17 at 07:56
  • @GopalBhuva I tried putting ' onunload="" ' in my body tag but it didn't work for the forward button. and I don't know how that new PHP file in the answer is supposed to be integrated. – Jayesh Rohira Apr 25 '17 at 08:23
  • try to refresh using `header("Refresh:0");` – Gopal Bhuva Apr 25 '17 at 08:30
  • @GopalBhuva Sorry man this doesn't work either it's giving me an error "Cannot modify header information - headers already sent by (output started at C:\wamp\www\CodeIGN\application\controllers\Welcome.php:115) " – Jayesh Rohira Apr 25 '17 at 08:44
  • @GopalBhuva Ignore the above comment. Sorry for that. It's working perfectly. Thanks a lot. – Jayesh Rohira Apr 25 '17 at 08:50
2

You should change this line, checking the session value

  if($this->session->all_userdata('is_logged_in')==1){
          $this->load->view('welcome_message');
     } 

As

if($this->session->userdata('is_logged_in')==1)
{
   $this->load->view('welcome_message');
}

You can use === operator in a better way

  //as your session value is string
 if($this->session->userdata('is_logged_in')=== '1')
  {
     $this->load->view('welcome_message');
 }
Bholu Bhaiya
  • 167
  • 1
  • 1
  • 12