php - Storing Session data Fails, due to a following redirect -


i want store session data right before 302 redirect occurs, same page, without of original request parameters.

for example:

  1. visitor goes domain.com/?ab_saveme=hey
  2. hey value stored
  3. visitor redirected domain.com/
  4. page output hey

this code came with, redirect, doesn't manage store value (hey not being outputed).

without redirect block, store correct.

<?php session_start(); session_name("hello"); $_session['cache']=$_get['ab_saveme']; // store `saveme` value  // begin self redirect code  function unparse_url($parsed_url) {   $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';   $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';   $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';   $user     = isset($parsed_url['user']) ? $parsed_url['user'] : '';   $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : '';   $pass     = ($user || $pass) ? "$pass@" : '';   $path     = isset($parsed_url['path']) ? $parsed_url['path'] : '';   $query    = !empty($parsed_url['query']) ? '?' . trim($parsed_url['query'], '&') : '';   $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';   return "$scheme$user$pass$host$port$path$query$fragment"; }  function strip_query($url, $query_to_strip) {   $parsed = parse_url($url);   $parsed['query'] = preg_replace('/(^|&)'.$query_to_strip.'[^&]*/', '', $parsed['query']);   return unparse_url($parsed); }  $url = "http://$_server[http_host]$_server[request_uri]";   $new_url = (strip_query($url, 'ab')); # or whatever query want strip/keep   $filtered = array_filter(array_keys($_get), function($k) {     return strpos($k, 'ab') === 0; });  if ( !empty($filtered) ) {     header ("location: $new_url"); }  // end self redirect code  echo $_session['cache']; // needs echo original `saveme` value echo session_id(); if (is_writable(session_save_path())) {     echo "writable"; } ?> 

edit:

thanks zimmi, noticed re-storing null value.

is best practice handle change:

$_session['cache']=$_get['ab_saveme']; // store `saveme` value 

into:

if (!empty($_get['ab_saveme'])) { $_session['cache']=$_get['ab_saveme']; // store `saveme` value } 

or there better way? might have various parameters (such ab_1,ab_2)


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -