Jump to content

[SOLVED] unset


CraigSherwood

Recommended Posts

Does anyone know why this doesn't actually unset?

<?php
session_start();
if (!isset($_SESSION['sesscss'])) {
   $_SESSION['sesscss'] = 80;
   session_register('sesscss');  
   } else {
   $nmbr=$_REQUEST['nmbr'];
   $_SESSION['sesscss'] = $_SESSION['sesscss'] + $nmbr;
   unset($nmbr);
   }
?>

Is there perhaps another way to explicity delete a $_REQUEST. Or if not, perhaps to assign another value to it - such as 0.

 

Thanks,

Craig

Link to comment
https://forums.phpfreaks.com/topic/80757-solved-unset/
Share on other sites

Try unsetting the actual $_REQUEST array element, rather than the variable $nmbr that you have copied it into.

 

You use session_register incorrectly here (and session_register is deprecated - you shouldn't be using it in any case). See http://www.php.net/manual/en/function.session-register.php.

 

Corrected code:

<?php
session_start();
if (!isset($_SESSION['sesscss'])) {
   $_SESSION['sesscss'] = 80;
   } else {
   $nmbr=$_REQUEST['nmbr'];
   $_SESSION['sesscss'] = $_SESSION['sesscss'] + $nmbr;
   unset($_REQUEST['nmbr']);
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/80757-solved-unset/#findComment-409637
Share on other sites

Many thanks on the pointer about session_register.

Also about the unset - but doesn't seem to make any difference.

The value has definately been unset, as demonstrated by having this in the page

<?php echo $_REQUEST['nmbr']; ?>

. Sure enough it shows no value.

However, I'm guessing that as the url in the address bar still shows the variable

http://localhost/cdca_local/page1.php?nmbr=-4

, the font size is still being incremented/decremented when I simply refresh the page.

In other words, this isn't truly deleting the variable on a page refresh

<?php
session_start();
if (!isset($_SESSION['sesscss'])) {
   $_SESSION['sesscss'] = 80;
   } else {
   $nmbr=$_REQUEST['nmbr'];
   $_SESSION['sesscss'] = $_SESSION['sesscss'] + $nmbr;
   unset($_REQUEST['nmbr']);
   }
?>

Any ideas how to actually delete it? Doesn't make any difference even if I assign a new value to it, i.e. 0.

Link to comment
https://forums.phpfreaks.com/topic/80757-solved-unset/#findComment-409695
Share on other sites

That would be because changing the values in $_REQUEST does not change the URL in the browser address bar. You could perhaps use:

 

if (!isset($_SESSION['sesscss'])) {
   $_SESSION['sesscss'] = 80;
} else {
   if (isset($_REQUEST['nmbr'])) {
      $nmbr=$_REQUEST['nmbr'];
      $_SESSION['sesscss'] = $_SESSION['sesscss'] + $nmbr;
      header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']);
   }
}

 

Just make sure you never output any HTML before header() statements.

Link to comment
https://forums.phpfreaks.com/topic/80757-solved-unset/#findComment-409712
Share on other sites

Truly fabulous - thank you so much.

I ended up using this:-

<?php
session_start();
if (!isset($_SESSION['sesscss'])) {
   $_SESSION['sesscss'] = 80;
} else {
   if (isset($_REQUEST['nmbr'])) {
      $nmbr=$_REQUEST['nmbr'];
      $_SESSION['sesscss'] = $_SESSION['sesscss'] + $nmbr;
  unset($_REQUEST['nmbr']);
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
   }
}
?>

I presumed that

a) 'nbmr' should be unset for a second inadvertent refresh

b) changed $_SERVER['SCRIPT_NAME'] to $_SERVER['PHP_SELF'] because

i) I read somewhere that 'SCRIPT_NAME' searches from the root down

ii) whereas PHP_SELF is more explicit and thus quicker?

Anyways - not too sure that I've got it all right - but it works which is the main thing.

Again, thank you,

Craig

Link to comment
https://forums.phpfreaks.com/topic/80757-solved-unset/#findComment-409733
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.