When using PHP filter_input ()

1

For a long time I use $_POST[] in my applications, however I've seen that some colleagues use filter_input() instead of $_POST[] . I have already opened the PHP documentation , but I confess that I could not fully understand its use. My doubts are:

  • What is the filter_input() ;
  • If it is valid to change $_POST[] to filter_input() ;
  • When to use filter_input() ;
  • I saw in the documentation, it highlights FILTER_SANITIZE_SPECIAL_CHARS , FILTER_SANITIZE_ENCODED , FILTER_DEFAULT and FILTER_VALIDATE_EMAIL . This last use to validate email:

    $email = filter_input(INPUT_POST,"EmailVerificar",FILTER_VALIDATE_EMAIL);
    

    But only the one that I understood the use.

        
    asked by anonymous 02.12.2018 / 14:22

    1 answer

    1

    Then my dear, first good morning!

    As you've already visited PHP documentation , I'm going to give you just a quick brush stroke for every item you quoted in the topic.

    1 .- The filter_input() can be said to be a join of the variables already known by us PHP programmers ($ _POST, $ _GET and others) into a single function and "optionally filters (as quoted in the documentation). "

    2 .- Yes it is really valid for you to exchange a $ _ POST for one we assume filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING) or filter_input(INPUT_POST, 'username', FILTER_SPECIAL_CHARS) (being just a very basic example how it could be used)

    3 .- It could be used on too many occasions, for example in a $ _GET and checking if $ _GET is numeric. .. $foo = filter_input(INPUT_GET, 'foo', FILTER_SANITIZE_NUMBER_INT);

    But we have a however if the index does not exist ... On this occasion:

    $foo = filter_var($_GET['foo'], FILTER_SANITIZE_NUMBER_INT);

    Returns a string "" empty and generates:

      

    Notice: Undefined index: foo

    Our current one, following the parameters of the documentation, will only return a NULL result:

    $foo = filter_input(INPUT_GET, 'foo', FILTER_SANITIZE_NUMBER_INT);

    Returns only:

      

    NULL

    But basically the answer is: YES , you can make a simple exchange of your $_POST with filter_input() . (Including use in my projects! Kkk)

    Just adding the information (In thanks to @ Fox.11 for posting) if there are too many doubts about the two filter options.

    FILTER_SANITIZE

    Used to clear variables:

    link

    FILTER_VALIDATE

    Used to validate variables:

    link

        
    02.12.2018 / 14:51
    Updating of variants OnInit - Angular 2 ___ ___ erkimt Unable to build entity manager factory (JPA Hibernate) ______ qstntxt ___

    I am trying to make a connection with Hibernate and JPA, but when trying to create the connection or persist some data it executes the following error:

      

    Dec 04, 2018 2:02:45 PM org.hibernate.ejb.HibernatePersistence   WARN: logDeprecation HHH015016: Encountered to deprecated   javax.persistence.spi.PersistenceProvider   [org.hibernate.ejb.HibernatePersistence]; use   [org.hibernate.jpa.HibernatePersistenceProvider] instead. Dec 04, 2018   2:02:45 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN:   HHH015016: Encountered to deprecated   javax.persistence.spi.PersistenceProvider   [org.hibernate.ejb.HibernatePersistence]; use   [org.hibernate.jpa.HibernatePersistenceProvider] instead. Dec 04, 2018   2:02:45 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN:   HHH015016: Encountered to deprecated   javax.persistence.spi.PersistenceProvider   [org.hibernate.ejb.HibernatePersistence]; use   [org.hibernate.jpa.HibernatePersistenceProvider] instead. Dec 04, 2018   2:02:45 PM org.hibernate.jpa.internal.util.LogHelper   INFO: HHH000204: LogPersistenceUnitInformation   PersistenceUnitInfo [name: context ...] Dec 04, 2018 2:02:45 PM   INFO: HHH000412: hibernate Core   {4.3.0.Final} Dec 04, 2018 2:02:45 PM org.hibernate.cfg.Environment    INFO: HHH000206: hibernate.properties not found Dec 04, 2018   INFO: 2:02:45 PM org.hibernate.cfg.Environment buildBytecodeProvider   HHH000021: Bytecode provider name: javassist Dec 04, 2018 2:02:45 PM   org.hibernate.annotations.common.reflection.java.JavaReflectionManager    INFO: HCANN000001: Hibernate Commons Annotations   {4.0.4.Final} Dec 04, 2018 2:02:45 PM   org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl   configure INFO: HHH000402: Using Hibernate built-in connection pool   (not for production use!) Unable to build entity manager factory

    My pom.xml

    %pre%

    My persistence.xml

    %pre%

    My Main class

    %pre%     
    ______ azszpr348573 ___