Testing if a variable isset?

I have

echo '<pre>';print_r($_POST);echo '</pre>';
...
...
if(isset($_POST['rack_id'])) { $rack_id = $_POST['rack_id']; } 
...
...
 if(isset($_POST['rack_id'])) { echo 'You must be <a href="../racks/show_rack.php?id='.$rack_id.'">logged in</a>.'; } else { echo 'You must be <a href="../../index.php">logged in</a>.'; }

the result.


<pre>Array
(
    [new_ps_Name] => dvbdghj
    [ps_Manufacturer] => Interex
    [ps_Model] => J58890CH1L2
    [tabtwo] => on
    [Voltage1] => 208
    [r_type1] => IEC-320 C13
    [Count] => 7
    [Voltage2] => 
    [r_type2] => 
    [Count2] => 
    [Voltage3] => 
    [r_type3] => 
    [Count3] => 
    [Location] => Bottom-Left
    [ps_Notes] => 
    [type] => Unknown
    [asset_id] => 1
    [rack_id] => 1
)
</pre><br />
<b>Warning</b>:  Undefined variable $rack_id in <b>C:\xampp\htdocs\APP\SST\assets\power_strips\add_power_strip_engine.php</b> on line <b>43</b><br />
You must be <a href="../racks/show_rack.php?id=">logged in</a>.

does isset() not work or something?

The code is correct. There must be something in the parts you don’t show to us

1 Like

I mean, i’d be checking to see if $rack_id was set in the second if, but yeah, the code as listed should work unless something in the intervening code removes it (or if it’s on a different code path).

If $rack_id is first declared inside the if, it only exists inside the if.

(if I remember my php correctly)

1 Like

I’ve not seen the form before this, but if it contains an input named rack_id it should always be set, unless it’s a checkbox. So that would make the test redundant anyway (certainly doing it twice is).
Maybe check for empty instead.

1 Like

If they didn’t change this in one of the latest PHP versions, that is wrong. There is nothing like a block scope in PHP. All variables declared in a block can be accessed from outside also.

(But my PHP knowledge is also long time not updated)

3 Likes

Not quite true. There is no general block scope, but user-defined functions are a separate scope. They can access the global scope with the global declaration at the start of their block, but otherwise, all variables in a function are local-scope.

2 Likes

I’m not sure an IF statement would be considered a block in any case?

Javascript would consider the meat of an if statement to be a block.

if (true) { 
  let a = 3;
  console.log(a); << 3
}
a; << Uncaught ReferenceError: a is not defined

(EDIT: Tweaked it to make it a bit more obvious. Also fixed syntax error.)

2 Likes

I’ll be a bit more basic. Why are you doing this at all?

Based on the filename - add_power_strip_engine.php, this is part of some Create operation. If this operation requires a logged in user, you should never display the form or run any of the form processing code without first having a logged in user, not attempt to provide a way for the user to login after the fact.

Also, since post data doesn’t persist outside the request it was submitted with, using a post value as part of a link won’t do any good, since the visitor will need to fill in and resubmit the form after logging in.

1 Like