Jump to content

Processwire equivalent of PHP $_SESSION?


ZionBludd
 Share

Recommended Posts

Hi, I am currently in the process of converting over some friends code from pure PHP code to the PW API. However, I've come into a road block with $_SESSION.

Can someone explain to me the PW equivalent of PHP's $_SESSION. What I am doing below is preventing the page doing another post on a page refresh, using the Post/Redirect/Get method. 

<?php
  //var_dump($_POST);
  if($_SERVER["REQUEST_METHOD"] == "GET"){
    //check for filter action
    if(isset($_SESSION["filter"])){
      $template = $_SESSION["filter"]; //this is your templates
      //things should just  work.
      $_SESSION["filter"] = null;
    }
    else{
      if ($page->id == "1019") {
        $template = "product_maker=NULL";
        $template = "template=products, product_type=Starter Kits,sort=name";
        // Defaults for fields
        $_SESSION["product_type"] = $_SESSION["product_maker"]  = $_SESSION["limit"] = null;
       }
    }

    if(isset($_SESSION["product_type"])){
      $product_type =  $_SESSION["product_type"] ;
    }
    if(isset($_SESSION["product_maker"])){
      $product_maker =  $_SESSION["product_maker"] ;
    }
    if(isset($_SESSION["has_images"])){
      $has_images =  $_SESSION["has_images"] ;
    }
    if(isset($_SESSION["limit"])){
      $limit =  $_SESSION["limit"] ;
    }
    var_dump($_SESSION["product_type"]);
  }
  else if($_SERVER["REQUEST_METHOD"] == "POST"){

    if ($input->post->$sanitzer->product_type || $input->post->has_images || ($input->post->$sanitzer->product_maker || ($input->post->limit)))  {
        $_SESSION["product_type"] = $product_type = $input->post->product_type;
        $_SESSION["product_maker"] = $product_maker = $input->post->product_maker;
        $_SESSION["has_images"] = $has_images = $input->post->has_images;
        $_SESSION["limit"] =$limit = $input->post->limit;
        $template = "template=products, has_images=$has_images, product_maker=$product_maker, limit=$limit, product_type=$product_type";

$template;
$_SESSION["filter"] = $template;

      }
    elseif ($input->post->has_images) {
     $template = "template=products";
    }
    //else {
    //    $product_type = $input->post->product_type;
    //}
    $_SESSION["filter"] = $template ;


    header("Location: ".$_SERVER['REQUEST_URI']);
    exit();
//redurect to self.

  }
?>

 

  • Like 1
Link to comment
Share on other sites

You should be fine using the native $_SESSION, although PW also provides the $session api variable if you want to use it (store vars using $session->myVar = $myValue and retrieve with $session->myVar). I am suspicious that something else is causing your problem.

  • Like 2
Link to comment
Share on other sites

PW's `$session` stores data in `$_SESSION` but it's "namespaced" in the array. E.g. `$session->value` != `$_SESSION['value']`

I'd recommend using the PW `$session` if possible instead of mixing and matching with `$_SESSION`.

In PW3 you can also use `$input->requestMethod()` for request method checks.

https://processwire.com/api/ref/session/
https://processwire.com/api/ref/input/

  • Like 2
Link to comment
Share on other sites

Thanks so much for all of your responses. Such an amazing community. 

I've done a-lot of reading, and debugging with TracyDebugger and this what what I've come up with


<?php
$limit = null;
$sort = null;
$template = null;
$kit_type = $session->get('search_type');

if ($page->path == "/kits/starter-kits/") {
  if ($input->post->product_brand_name || $input->post->limit || $input->post->product_type || $input->post->product_port_location) {
    $session->set ('template', 'products');
    $session->set ('limit', $input->post->limit);
    $session->set ('product_type', $input->post->product_type);
    $session->set ('product_brand_name', $input->post->product_brand_name);
    $session->set ('port_location', $input->post->product_port_location);
    // Build selector
    $selector = "template=$session->template, product_brand_name=$session->product_brand_name, product_port_location=$session->port_location,limit=$session->limit, product_type=$session->product_type";
    var_dump ($selector);

  } else {
    $session->set ($product_type , 'Starter Kits');
    $selector = "template=products, sort=name, product_type=$session->product_type";
    echo "<pre style='padding:10pt'>";
    echo "Default Starter Kits landing settings";
    echo "<br>";
    var_dump ($selector);
    echo "</pre>";


  }
} elseif ($page->path == "/kits/advanced-kits/") {
  if ($input->post->product_maker || $input->post->limit || $input->post->product_type || $input->post->product_port_location) {
    $session->set ('template', 'products');
    $session->set ('product_maker', $input->post->product_maker);
    $session->set ('limit', $input->post->limit);
    $session->set ('product_type', $input->post->product_type);
    $session->set ('port_location', $input->post->product_port_location);
    // Build selector
    $selector = "template=$session->template, product_port_location=$session->port_location, product_maker=$session->product_maker, limit=$session->limit, product_type=$session->product_type";
    var_dump ($selector);

  } else {
    $session->set ('product_type' , 'Advanced Kits');
    $session->set ('product_maker' , '');

    $selector = "template=products,sort=price, product_type=Advanced Kits";
    echo "<pre style='padding:10pt'>";
    echo "Default Advanced Kits landing settings";
    echo "<br>";
    var_dump ($selector);
    echo "</pre>";


}

} elseif ($page->path == "/kits/advanced-kits/") {
    if ($input->post->product_maker || $input->post->limit || $input->post->product_type || $input->post->product_port_location) {
      $session->set ('template', 'products');
      $session->set ('product_maker', $input->post->product_maker);
      $session->set ('limit', $input->post->limit);
      $session->set ('product_type', $input->post->product_type);
      $session->set ('port_location', $input->post->product_port_location);
      // Build selector
      $selector = "template=$session->template, product_port_location=$session->port_location, product_maker=$session->product_maker, limit=$session->limit, product_type=$session->product_type";
      var_dump ($selector);

    } else {
      $session->set ('product_type' , 'Advanced Kits');
      $session->set ('product_maker' , '');
      $session->set ('limit', '10');
      $selector = "template=products,sort=price, limit=$session->limit, product_type=Advanced Kits";
        if ($user->isSuperuser()){
          echo "<pre style='padding:10pt'>";
          echo "Default Advanced Kit landing settings";
          echo "<br>";
          var_dump ($selector);
          echo "</pre>";
        }

  }

}
elseif ($page->path == "/kits/intermediate-kits/") {
    if ($input->post->product_maker || $input->post->limit || $input->post->product_type || $input->post->product_port_location) {
      $session->set ('template', 'products');
      $session->set ('product_maker', $input->post->product_maker);
      $session->set ('limit', $input->post->limit);
      $session->set ('product_type', $input->post->product_type);
      $session->set ('port_location', $input->post->product_port_location);


      // Build selector
      $selector = "template=$session->template, product_port_location=$session->port_location, product_maker=$session->product_maker, limit=$session->limit, product_type=$session->product_type";
      var_dump ($selector);

    } else {
      $session->set ('product_type' , 'Intermediate Kits');
      $session->set ('product_maker' , '');
      $session->set ('limit', '10');
      $selector = "template=products,sort=price, limit=$session->limit, product_type=Intermediate Kits";
        if ($user->isSuperuser()){
          echo "<pre style='padding:10pt'>";
          echo "Default Intermediate Kit landing settings";
          echo "<br>";
          var_dump ($selector);
          echo "</pre>";
        }

  }

}

?>

However, haven't been able to implement post resubmission errors (Post Get Redirect method)

And I'm having trouble with removing the product_brand_name selector when it's not selected in my form.

Link to comment
Share on other sites

For redirection (PRG), you can use http://processwire.com/api/ref/session/redirect/

Since you're using POST values in your selectors, you should sanitize them first.

To customize the selector based on which fields are submitted in the POST, I would do something like this:

$temp = [];

if ( $input->post->product_brand_name ) {
	$temp[] = 'product_brand_name=' . $sanitizer->selectorValue($input->post->product_brand_name);
}

if ( $input->post->product_type ) {
	$temp[] = 'product_type=' . $sanitizer->selectorValue($input->post->product_type);
}

// ... and so on

$selector = implode(',', $temp);

 

  • Like 1
Link to comment
Share on other sites

@mattcohen not sure what you are trying to do exactly but you have lots of if/else and duplicates that makes the code quite hard to read.

Maybe somethink like this could clean things up a little?

$path = '/foo';

$selector['foo'] = 1;
$selector['bar'] = 2;
$selector['type'] = $sanitizer->text($input->post->type);
$selector['limit'] = 100;
switch($path) {
    case '/foo':
        unset($selector['bar']);
        $selector['limit'] = 10;
        break;
    case '/bar':
        unset($selector['foo']);
        $selector['limit'] = 20;
        break;
}
d($selector, 'selector array');

$session->tmp = json_encode($selector);
d($session->tmp, 'session as string');
d(json_decode($session->tmp), 'session as object');

252856949_2018-05-1110_35_20.png.5f2b7e84ca6e4d15ed18deb79fb6192b.png

As you can see I would highly recommend to use d() = dump() or bd() = bardump() in tracy. It's a lot more readable than var_dump() and a lot less effort. See the tracy docs for all the options (maxLength etc).

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...