Jump to content

Remove session key and value with query string


jds43
 Share

Recommended Posts

Hello, I'm wanting to allow the user to remove a key from their session, that had been created by previous form input, by clicking a link with a query string.

The condition runs, but I'm getting the following error with no changes to the session.

PHP Notice: Indirect modification of overloaded property ProcessWire\Session::$itinerary has no effect

I imagine the session is protected from accessing this way. Does anyone have any suggestions?

foreach($session->itinerary  as $key => $value) {
    echo "<li class='uk-text-capitalize uk-flex uk-flex-row uk-flex-middle'>
        <a href='$page->url?$key=true' data-unset='$key' data-uk-icon='icon: minus-circle'></a> The ".str_replace("-", " ", $key)."
    </li>";
    if(isset($_GET[$key])){
        if(is_array($session->itinerary)) {
            // destroy a single element of an array
            unset($session->itinerary[$key]);
            $session->redirect($page->url);	
        }
    }
}

 

Link to comment
Share on other sites

Thanks @Zeka No luck with the following change. The session persists. Any ideas?

if(isset($_GET[$key])){
	if(is_array($session->itinerary)) {
		// destroy a single element of an array
		$session->remove($key);
		$session->redirect($page->url);	
	}
}

 

Link to comment
Share on other sites

You have to first get the "itinerary" array as a variable distinct from the $session->itinerary overloaded property, modify it as needed, and then set it back to $session->itinerary.

if(isset($_GET[$key])){
	$itinerary = $session->itinerary;
	if(is_array($itinerary)) {
		// destroy a single element of an array
		unset($itinerary[$key]);
		$session->itinerary = $itinerary;
		$session->redirect($page->url);
	}
}

 

  • Like 2
  • Thanks 1
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...