ZionBludd Posted May 10, 2018 Posted May 10, 2018 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. } ?> 1
bernhard Posted May 10, 2018 Posted May 10, 2018 42 minutes ago, mattcohen said: Can someone explain to me the PW equivalent of PHP's $_SESSION Maybe the docs can explain that? ? https://www.google.com/search?q=site:processwire.com/api/ref+session 3
thetuningspoon Posted May 10, 2018 Posted May 10, 2018 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. 2
dragan Posted May 10, 2018 Posted May 10, 2018 What exactly is not working, and where? Install Tracy Debugger - it might help to narrow down what's going wrong. 1
gRegor Posted May 10, 2018 Posted May 10, 2018 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/ 2
ZionBludd Posted May 11, 2018 Author Posted May 11, 2018 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.
gRegor Posted May 11, 2018 Posted May 11, 2018 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); 1
Zeka Posted May 11, 2018 Posted May 11, 2018 Also consider to use selector as an regular array https://processwire.com/blog/posts/processwire-3.0.13-selector-upgrades-and-new-form-builder-version/#new-selectors-as-regular-arrays 3
bernhard Posted May 11, 2018 Posted May 11, 2018 @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'); 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).
gRegor Posted May 11, 2018 Posted May 11, 2018 8 hours ago, Zeka said: Also consider to use selector as an regular array https://processwire.com/blog/posts/processwire-3.0.13-selector-upgrades-and-new-form-builder-version/#new-selectors-as-regular-arrays Nice! I'd not seen this feature. 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now