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 '[email protected]' (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.

1
  • all_userdata() is DEPRECATED for more read here Commented Apr 25, 2017 at 7:55

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

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
you need to refresh your page while clicking on back button. Refer this question
@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.
try to refresh using header("Refresh:0");
@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) "
|
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');
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.