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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.