Jump to content

Simple security question - setting globals within functions/classes


Anti-Moronic

Recommended Posts

So I have a function. Let's say:

 

<?php

function cleanfield($field){

  global $badfeed;
  
  if(empty($field)){
    
    $badfeed[]= "This field is required.";

  }else{
    return $field;
  }

}

?>

 

Then in my code, I use:

 

 

<?php

$field = cleanfield($_POST['somefield']);

if(!$badfeed){

  //perform database queries

}

?>

 

NOTE: this is NOT my whole function, I have just rewrote a very simple example for sake of simplicity.

 

My obvious intent here is to register a badfeed in case of bad input and recognize that a badfeed exists. If no badfeed exists, then no bad input exists (determined by my own filtering).

 

But, I have registered badfeed as a global so can now be accessed as such. Could somebody potentially return a NULL on that global and thus bypass my check?

 

Finally, is there a way to make a variable only available to the script, and not through globals?

 

Any help is greatly appreciated.

Link to comment
Share on other sites

I used to do that but it requires too much management across pages. Setting and destroying sessions or unsetting certain elements. This is the source of my error reporting so there's too much to handle sometimes.

 

I may even be missing something fundamental about php. How could somebody unset the global $badfeed between the creation of it, and the checking, when they are within the same script? Would this even be possible?

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.