简体   繁体   中英

Opencart custom admin area -> invalid token session

The answers I received for my last question ( Opencart custom admin area -> queries only showing first record of tables ) were very helpful, so I decided to come back for a little more advice regarding the same project.

Whenever I redirect to a custom page/controller (ie /admin/index.php?route=custom/verkopen, coming from /admin/index.php?route=custom/hoofdpagina), I am asked to log in again because of an invalid token session. I've tried to google it and figure it out for myself, but the solutions I've found are either incomplete or don't work for me.

Here's my code again:

Controller main:

<?php

class Controllercustomhoofdpagina extends Controller{ 
        public function index() {

    $template="custom/hoofdpagina.tpl"; // .tpl location and file

    $this->load->model('custom/hoofdpagina');

    $this->template = ''.$template.'';
    $this->response->setOutput($this->render());
}

}

?>

Controller subpage:

   <?php 

class Controllercustomverkopen extends Controller{


    public function Index(){

    $template="custom/verkopen.tpl"; // .tpl location and file

    $this->load->model('custom/hoofdpagina');

    $this->data['verkopen'] =                                             $this->model_custom_hoofdpagina->verkopenLijst();

     $this->template = ''.$template.'';
     $this->response->setOutput($this->render());

    }

    public function verkopenTonen(){
        $this->load->model('custom/hoofdpagina');
        $verkopen = $this->model_custom_hoofdpagina->verkopenLijst();
        return $verkopen;
    }

}




?>

Model:

<?php
class Modelcustomhoofdpagina extends Model {

public function verkopenLijst() {

  $query = $this->db->query("SELECT * FROM `shop_order_product`");
    if($query->num_rows > 0){
    $verkopen = array();
       foreach($query->rows as $result){
            $verkopen[] = array(
                'name' => $result['name'],
                'model'      => $result['model'],
                'quantity'      => $result['quantity'],
                'price'     => $result['price'],
                'total'     => $result['total'],
                'tax'     => $result['tax']);

        }
        return $verkopen;
    }

}

public function klantenLijst() {


    $query = $this->db->query("SELECT * FROM `shop_customer`");

    if($query->num_rows > 0){
    $klanten = array();
       foreach($query->rows as $result){
            $klanten[] = array(
                'first name' => $result['firstname'],
                'last name'      => $result['lastname'],
                'email'      => $result['email'],
                'telephone'     => $result['telephone'],
                'date added'     => $result['date_added']
                );

        }
        return $klanten;
    }
}

public function productenLijst() {


    $query = $this->db->query("SELECT * FROM `shop_product_description`");

    if($query->num_rows > 0){
    $producten = array();
       foreach($query->rows as $result){
            $producten[] = array(
                'name' => $result['name'],
                'description'      => $result['description']
                );

        }
        return $producten;
    }
}

public function productenAanpassen() {

   $this->request->post['name'];
   $this->request->post['text'];
   $this->request->post['description'];
   $this->request->post['price'];



}

}

?>

View main:

  <div class="container-fluid">

<div class = "row">
  <div class = "col-xs-12">
    <div class = "head font-effect-neon"> Admin Area </div>
  </div>
</div>

<div class="row">
  <div class="col-xs-3">
    <div class="button">
      <a href="http://maureen.codehub.be/ew/admin/index.php?route=custom/verkopen"><button class = "buttonverkopen font-effect-neon" name = "verkopen"> Verkopen </button></a>
    </div>
  </div>

  <div class="col-xs-3">
    <div class="button">
      <a href="http://maureen.codehub.be/ew/admin/index.php?route=custom/klanten"><button class = "buttonklanten font-effect-neon" name = "klanten"> Klanten </button></a>
    </div>
  </div>

  <div class="col-xs-3">
    <div class="button">
      <a href="http://maureen.codehub.be/ew/admin/index.php?route=custom/producten"><button class = "buttonproducten font-effect-neon" name = "producten"> Producten </button></a>
    </div>
  </div>

  <div class="col-xs-3">
    <div class="button">
      <a href="http://maureen.codehub.be/ew/admin/index.php?route=custom/aanpassen"><button class = "buttonaanpassen font-effect-neon" name = "aanpassen"> Aanpassen </button></a>
    </div>
  </div>
</div>


<div class = "row">
  <div class = "col-xs-4">
    <div class = "credits font-effect-neon"> Eindwerk </div>
  </div>
  <div class = "col-xs-4">
    <div class = "credits font-effect-neon"> VDO Webontwikkeling 2014-2015 </div>
  </div>
  <div class = "col-xs-4">
    <div class = "credits font-effect-neon"><span class="glyphicon glyphicon-copyright-mark"> Maureen van Eede </span></div>
  </div>

View subpage:

<div class="container-fluid">

<div class = "row">
  <div class = "col-xs-12">
    <div class = "head font-effect-neon"> Verkopen </div>
  </div>
</div>

<div class="row">
  <div class="col-xs-12">
    <div class = "php1"> <pre>
      <?php
      foreach($verkopen as $verkoop){
          echo '<p>';
          echo 'Product:'. $verkoop['name'].'<br />';
          echo 'Prijs:'. $verkoop['price'];
          echo '</p>';
        }
        ?>
      </pre>
    </div>
  </div>
</div>

Any help would greatly be appreciated.

When you hit the module via browser,you have add session token in url as a query string, like this 'token=' . $this->session->data['token'] 'token=' . $this->session->data['token'] . If session token is not present in the url it will never go to your custom module.

I had face this issue while creating my own module.
One easiest shortcut way to resolve this issue is to add $_GET['token'] in template file. So that whenever the administrator clicks the custom module link, a token will be appended to the URL given in Dashboard of the Admin Panel. In order to prevent the invalid token session .

<li><a href="<?php echo DOMAIN_API_SYNC; ?>&token=<?php echo $_GET['token']; ?>" <i class="fa fa-key"></i> <span><?php echo "API ACCESS"; ?></span></a></li>

In config.php

// URL
define('DOMAIN_API_SYNC', 'Domain_Name.org/admin/index.php?route=yourdirectory/your_file');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM