SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 33 of 33
  1. #26
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Kremenchug / Ukraine
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IMHO E_ALL wolud be enabled always.
    But when You developing script you should to display errors:
    ini_set('display_errors', 'On');

    and when script is working
    ini_set('display_errors', 'Off);
    ini_set('log_errors', 'On');
    So if something happen, all errors will in the log-file

  2. #27
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There appear to be just two methods in the above class, written out many times...

  3. #28
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    There appear to be just two methods in the above class, written out many times...
    Which may be in part becuase you are not allowed to write a $_(GET|POST|etc.) superglobal as a variable variable (IIRC, from when I tackled a similar issue).

  4. #29
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Which may be in part becuase you are not allowed to write a $_(GET|POST|etc.) superglobal as a variable variable (IIRC, from when I tackled a similar issue).
    Thanks for the tip - didn't know that.

  5. #30
    SitePoint Addict
    Join Date
    Aug 2002
    Location
    Ottawa, Ontario, Canada
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Which may be in part becuase you are not allowed to write a $_(GET|POST|etc.) superglobal as a variable variable (IIRC, from when I tackled a similar issue).
    Just thought I would post this class... I had a few spare minutes so I put this together. It gets around the above limitation by using eval() (let the shrieking begin!)

    PHP Code:
    <?php
    /**
    *
    * @version $Id$
    * @copyright 2003
    */

    /**
    * Class loosely based on a scope checking class source code that
    * appeared in {@link [url]http://www.phparch.com[/url] php|Architect}  <look up issue>.  I have modified 
    * what the existence check functions return.  They used to return
    * the variable contents if it existed.  I have changed them to be
    * true boolean methods (i.e. true/false result).  I also extracted
    * the check code into its own private method and used eval() to
    * all dynamic checking of superglobals.  The user can only check
    * scopes listed in the array in the _valid_scope() method (In case
    * the private methods are directly invoked).

    * Its intent is to  provide a "simpler" interface to checking 
    * for variable existence and equality in the Super-Global scopes.

    * For example, a more "traditional" check:
    * if(isset($_POST['my_var'])){}

    * Using this class:
    * if(SgChk::post('my_var')){}

    * Not a huge difference, but for equality checks it is more noticable:

    * A "Traditional" existence and equality check:
    * if(isset($_POST['my_var'])&&strcmp($_POST['my_var'],$val)==0){}

    * Using this class:
    * if(SgChk::post_eq('my_var')){}


    * @author Keith Young
    * @since 2004-02-06
    */
    class SgChk{
      
      
    /**
      * Constructor - not used right now
      */
      
    function SgChk(){
        
      }
      
      
    /**
      * Main method of variable existence checking.  Uses eval() to accomplish this.
      * 
      * @access private
      * @param  string $scope scope to check for variable.  Allowed values: (get|cookie|post|request|server|session)
      * @return boolean $retval
      * @see SgChk::_valid_scope()
      */
      
    function _check($scope,$var){
        
    $scope=strtoupper('$_'.$scope);
        if(!
    SgChk::_valid_scope($scope)){
          
    $retval=false;
        }else{
          eval(
    '$retval=(isset('.$scope.'[$var]))?true:false;');
        }   
        return 
    $retval;
      }
      
      
    /**
      * Main method of variable existence and equality checking.  Uses eval() to accomplish this.
      * 
      * @access private
      * @param  string $scope scope to check for variable.  Allowed values: (get|cookie|post|request|server|session)
      * @return boolean $retval
      * @see SgChk::_valid_scope()
      */
      
    function _check_equal($scope,$var,$val){
        
    $scope=strtoupper('$_'.$scope);
        
        if(!
    SgChk::_valid_scope($scope)){
          
    $retval=false;
        }else{
          eval(
    '$retval=((isset('.$scope.'[$var]))&&(strcmp('.$scope.'[$var],$val)==0))?true:false;');
        }
        return 
    $retval;
      }

      
    /**
      * method to verify an "accepted" scope was passed into a check function
      * 
      * @access private
      * @param string $scope scope to be verified
      * @return boolean return true if a valid scope was passed in
      */
      
    function _valid_scope($scope){
        return (
    in_array($scope,array('$_GET','$_COOKIE','$_POST','$_REQUEST','$_SERVER','$_SESSION')))?true:false;
      }
      
      
    /**
      * wrapper function for $_GET scope check
      * 
      * @param string $vname variable name to check for in $_GET scope
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check()
      */
      
    function get($vname){
        return 
    SgChk::_check('get',$vname);
      }
      
      
    /**
      * wrapper function for $_POST scope check
      * 
      * @param string $vname variable name to check for in $_POST scope
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check()
      */
      
    function post($vname){
        return 
    SgChk::_check('post',$vname);
      }
      
      
    /**
      * wrapper function for $_REQUEST scope check
      * 
      * @param string $vname variable name to check for in $_REQUEST scope
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check()
      */
      
    function request($vname){
        return 
    SgChk::_check('request',$vname);
      }
      
      
    /**
      * wrapper function for $_SERVER scope check
      * 
      * @param string $vname variable name to check for in $_SERVER scope
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check()
      */
      
    function server($vname){
        return 
    SgChk::_check('server',$vname);
      }
      
      
    /**
      * wrapper function for $_SESSION scope check
      * 
      * @param string $vname variable name to check for in $_SESSION scope
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check()
      */
      
    function session($vname){
        return 
    SgChk::_check('session',$vname);
      }
      
      
    /**
      * wrapper function for $_COOKIE scope check
      * 
      * @param string $vname variable name to check for in $_COOKIE scope
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check()
      */
      
    function cookie($vname){
        return 
    SgChk::_check('cookie',$vname);
      }
      
      
    /**
      * wrapper function for $_GET scope and equality check
      * 
      * @param string $vname variable name to check for in $_GET scope
      * @param string $val value to do a string comparrison on
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check_equal()
      */
      
    function get_eq($vname,$val){
        return 
    SgChk::_check_equal('get',$vname,$val);
      }
      
      
    /**
      * wrapper function for $_POST scope and equality check
      * 
      * @param string $vname variable name to check for in $_POST scope
      * @param string $val value to do a string comparrison on
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check_equal()
      */
      
    function post_eq($vname,$val){
        return 
    SgChk::_check_equal('post',$vname,$val);
      }
      
      
    /**
      * wrapper function for $_REQUEST scope and equality check
      * 
      * @param string $vname variable name to check for in $_REQUEST scope
      * @param string $val value to do a string comparrison on
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check_equal()
      */
      
    function request_eq($vname,$val){
        return 
    SgChk::_check_equal('request',$vname,$val);
      }
      
      
    /**
      * wrapper function for $_SERVER scope and equality check
      * 
      * @param string $vname variable name to check for in $_SERVER scope
      * @param string $val value to do a string comparrison on
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check_equal()
      */
      
    function server_eq($vname,$val){
        return 
    SgChk::_check_equal('server',$vname,$val);
      }
      
      
    /**
      * wrapper function for $_SESSION scope and equality check
      * 
      * @param string $vname variable name to check for in $_SESSION scope
      * @param string $val value to do a string comparrison on
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check_equal()
      */
      
    function session_eq($vname,$val){
        return 
    SgChk::_check_equal('session',$vname,$val);
      }
      
      
    /**
      * wrapper function for $_COOKIE scope and equality check
      * 
      * @param string $vname variable name to check for in $_COOKIE scope
      * @param string $val value to do a string comparrison on
      * @return boolean returns true if variable exists, false if it doesn't
      * @see SgChk::_check_equal()
      */
      
    function cookie_eq($vname,$val){
        return 
    SgChk::_check_equal('cookie',$vname,$val);
      }
    }
    ?>

    Cheers,
    Keith.

  6. #31
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This was my attempt, way back when...
    http://www.zend.com/zend/art/art-sweat4.php#Heading12

  7. #32
    SitePoint Addict mgkimsal's Avatar
    Join Date
    Sep 1999
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only part of a solution

    All of the generously offered classes and concepts about 'checking' if something exists are only dealing with $_GET/$_POST-type stuff. You wouldn't be able to deal with variables which are local to a particular function or class - you're stuck with having isset()s all over the place to avoid NOTICE warnings. So, if you have to use isset() stuff anyway, there's not much need for a class to handle only part of the issue. Or is there - maybe I'm missing some other benefit.
    Michael Kimsal
    =============================
    groovymag.com - for groovy/grails developers
    jsmag.com - for javascript developers

  8. #33
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Which may be in part becuase you are not allowed to write a $_(GET|POST|etc.) superglobal as a variable variable (IIRC, from when I tackled a similar issue).
    Wouldn't this work?

    PHP Code:
    <?php
    function array_accessor(& $array$variable_name$default false)
    {
    return (isset(
    $array[$variable_name])) ? 
       
    $array[$variable_name] : $default;
    }

    function 
    array_eq(& $array$variable_name$compare)
    {
    return (
    array_accessor($array$variable_name) == $compare) ?
       
    true false;
    }
    (Probably bad function names, but...)

    This could then be expanded upon for any array, not just the superglobals:

    PHP Code:
    $value array_accessor($_GET'id'0);
    if (
    array_eq($_POST'submit_value''Yes'))
      echo 
    "Yes!";

    $test = array('key1'=>'value1''key2'=>'value2');
    $value2 array_accessor($test'key3''failed'); 
    This code is untested, but I can't really think of a reason to wrap that many lines of code (from the unnamed magazine) into a so called "class".
    My name is Steve, and I'm a super-villian.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •